stdvector

Combine multiple vectors (results of function) into one with template

女生的网名这么多〃 提交于 2019-12-05 10:47:32
I'd like to have a templated function taking in a vector<T> v and a function op, mapping T to vector<U> and would like to concatenate the results of applying f to every element vector of v to return a vector<U> = [ Elements of op(v[0]), Elements of op(v[1]) ...]. A working option I found was adding an example in the function to allow for template deduction: template <typename Container> Container& concat(Container& c1, Container const& c2) { c1.insert(end(c1), begin(c2), end(c2)); return c1; } template <typename Container, typename UnaryOperation, typename U> inline auto to_vec_from_vectors

Is the capacity required to be preserved when moving a std::vector?

孤人 提交于 2019-12-05 10:31:25
问题 Consider the following code: std::vector vec; vec.reserve(500); size_t cap = vec.capacity(); std::vector newVec = std::move(vec); assert(cap == newVec.capacity()); In pretty much any implementation you run across, this will work. I don't care about what implementations do. I want to know what the standard requires . Will the moved-to vector have the same capacity as the original? Or will the assert trigger? 回答1: Looking at the standard, it appears that nothing is required from the move

How to store a vector of objects of an abstract class which are given by std::unique_ptr?

时光怂恿深爱的人放手 提交于 2019-12-05 09:18:24
I got a loop in which i use a function returning std::unique_ptr to an object of an abstract class. I want to store these objects into a std::vector via push_back. But since the objects are of abstract type i get the following error: error: cannot allocate an object of abstract type for the line cells.push_back(std::move(*cell)); where cells is a std::vector of the abstract type and cell is of type std::unique_ptr<AbstractType>&& cell (I actually pass cell to a handler class) I know that one can not instantiate an abstract type and as I'm understanding the std:move operator it need to

Rename std::vector to another class for overloading?

烈酒焚心 提交于 2019-12-05 08:39:14
Look at this code. #include <vector> template<class ...Args> using other_vector = std::vector<Args...>; template<class T> void f(std::vector<T>& ) {} template<class T> void f(other_vector<T>& ) {} int main() { other_vector<int> b; f(b); return 0; } It does not compile, because f is being redeclared. I totally understand the error. However, I need a second class that behaves like std::vector<T> , but will be seen as a different type, so that overloading, like in the above example, would be legal. What could I do? Let the new class have std::vector<T> as a base class. This might work, but one

Best way to delete a std::unique_ptr from a vector with a raw pointer?

一笑奈何 提交于 2019-12-05 06:42:42
So I have a vector like so: std::vector<std::unique_ptr<SomeClass>> myVector; Then I have another vector which contains raw pointers of SomeClass : std::vector<SomeClass*> myOtherVector; If there is an element inside myOtherVector it will also be inside myVector , so I want to go through each element in myOtherVector and remove the same element from myVector . Then clear out the vector. This is what I came up with: for(size_t i = 0; i < myOtherVector.size(); i++) { myVector.erase(std::remove(myVector.begin(), myVector.end(), myOtherVector[i]), myVector.end()); } myOtherVector.clear(); This

Difference between std::vector and std::array initializer lists

拟墨画扇 提交于 2019-12-05 05:46:47
This C++11 code works fine for me: #include <iostream> #include <vector> #include <array> using namespace std; struct str { int first, last; }; vector<str> fields { {1,2}, {3,4}, {5,6} }; int main() { for (str s : fields) cout << s.first << " " << s.last << endl; } It prints the six expected values. But if I change vector<str> to array<str,3> , gcc gives me this error: "too many initializers for ‘std::array’". If I change the initialization of fields thus: array<str,3> fields { str{1,2}, str{3,4}, str{5,6} }; Things work nicely. So why do I need str{1,2} when using std::array , but only {1,2}

Create a fixed size std::vector and write to the elements

北慕城南 提交于 2019-12-05 01:47:16
In C++ I wish to allocate a fixed-size (but size determined at runtime) std::vector then write to the elements in this vector. This is the code I am using: int b = 30; const std::vector<int> test(b); int &a = test[3]; However, this gives me a compiler (MSVC 2010 Pro) error: error C2440: 'initializing' : cannot convert from 'const int' to 'int &'. Conversion loses qualifiers. My understanding of const is that it makes all of the member variables of a class constant. For example, the following works fine: class myvec { public: myvec(int num) : ptr_m(new int[num]) {}; ~myvec() { delete ptr_m; }

Prettier syntax for “pointer to last element”, std::vector?

北战南征 提交于 2019-12-05 01:26:53
I'm wondering if there is prettier syntax for this to get a normal pointer (not an iterator) to the last element in a C++ vector std::vector<int> vec; int* ptrToLastOne = &(*(vec.end() - 1)) ; // the other way I could see was int* ptrToLastOne2 = &vec[ vec.size()-1 ] ; But these are both not very nice looking! int* ptrToLastOne = &vec.back(); // precondition: !vec.empty() int* ptrToLast = &(vec.back()); // Assuming the vector is not empty. Some more options: int* ptrToLast = &*vec.rbegin(); or int* ptrToLast = &*boost::prev(vec.end()); sharptooth Nothing much prettier for that, but you can

Destroy std::vector without releasing memory

爷,独闯天下 提交于 2019-12-05 01:20:27
Lets say I have a function to get data into an std vector: void getData(std::vector<int> &toBeFilled) { // Push data into "toBeFilled" } Now I want to send this data to another function, that should free the data when finished: void useData(int* data) { // Do something with the data... delete[] data; } Both functions (getData and useData) are fixed and cannot be changed. This works fine when copying the data once: { std::vector<int> data; getData(data); int *heapData = new int[data.size()]; memcpy(heapData, data.data(), data.size()*sizeof(int)); useData(heapData); data.clear(); } However, this

C++ std::vector<>::iterator is not a pointer, why?

◇◆丶佛笑我妖孽 提交于 2019-12-04 23:59:55
Just a little introduction, with simple words. In C++, iterators are "things" on which you can write at least the dereference operator *it , the increment operator ++it , and for more advanced bidirectional iterators, the decrement --it , and last but not least, for random access iterators we need operator index it[] and possibly addition and subtraction. Such "things" in C++ are objects of types with the according operator overloads, or plain and simple pointers. std::vector<> is a container class that wraps a continuous array, so pointer as iterator makes sense. On the nets, and in some