Create std::vector in-place from raw data

Given a raw array of elements, how to create a std::vector that takes ownership of the raw array without reallocate & copy? For example having the raw array: int* elems = new int[33] how to create a std::vector of size 33 pointing to elems? I...
more »

2017-11-11 10:11 (5) Answers

compile order eclipse c++

I am trying Unit test using Gtest. I am creating a another project that has file from the original project (By linking) that is to be tested. In the new project I then create Fake Implementation and Mocks of the additional dependencies of the functi...
more »

2017-11-08 13:11 (1) Answers

Shifting Objects Up in an Array

I'm creating a program that creates an array of objects in random positions in an array size 8. Once created, I need them to sort so that all the objects in the array are shifted up to the top, so no gaps exist between them. I'm almost there, but I c...
more »

2017-11-07 07:11 (4) Answers

Can POSIX timers safely modify C++ STL objects?

I'm attempting to write a C++ "wrapper" for the POSIX timer system on Linux, so that my C++ program can set timeouts for things (such as waiting for a message to arrive over the network) using the system clock, without dealing with POSIX's ugly C int...
more »

2017-11-06 23:11 (1) Answers

Using STL algorithm to find all matches in a set

I have a set containing Song objects. I want to search for all occurrences of a Songs that have a particular title. I am required to use a STL algorithm. The problem is when it finds the first occurrence it stops searching. I am trying to fix it by...
more »

2017-11-06 17:11 (2) Answers

C++ find_if member variable of another class

So I have a class called Song, and another class Called SongLibrary. The Songlibrary just contains a set of all songs and appropriate methods. I am currently trying to make a function to search the song library and check if a song has a particular t...
more »

2017-11-06 15:11 (2) Answers

Is a log(n) std::find_if possible?

I will define a class and use a container (possibly std::set) with a custom comparator function (basically, raw integer comparison on a class member variable). I wish to know if I can somehow keep this set std::sorted and use std::find_if with logari...
more »

2017-11-06 11:11 (1) Answers

how to make 2 = .01 and .01 = 2?

Okay so the title may not make the most sense but I will explain it better here.. what i'm trying to do is take the value of 2 and make it equal to .01 and make .01 = 2, well I tried to hardcode it like this if (Value = 2) Value = 0; else if (V...
more »

2017-11-05 05:11 (1) Answers

deleting array from heap

My question is about deleting an array from heap memory. I read a book and this blog and other resources like this one, and all of them said that for deleting an array from heap we must use the [] after the delete function so if we write our codes w...
more »

2017-11-04 14:11 (2) Answers

Rcpp isn't finding Boost headers

I have a file called src/windows.cpp (windowing functions for vectors, nothing to do with MS Windows!) in an R package project. The top of the file looks like this: // [[Rcpp::depends(BH)]] // [[Rcpp::plugins(cpp11)]] #include <Rcpp.h> #incl...
more »

2017-11-01 22:11 (1) Answers

Can I call a std::function from C?

I have some C++ code that returns a std::function. I would like to call this from some C code. Is this possible? As an example I have the following code: typedef std::function<int(int)> AdderFunction; AdderFunction makeAdder(int amount) { ...
more »

2017-11-01 21:11 (5) Answers