Correct way to work with vector of arrays

You cannot store arrays in a vector or any other container. The type of the elements to be stored in a container (called the container’s value type) must be both copy constructible and assignable. Arrays are neither. You can, however, use an array class template, like the one provided by Boost, TR1, and C++0x: (You’ll … Read more

Linked List vs Vector

Vector is another name for dynamic arrays. It is the name used for the dynamic array data structure in C++. If you have experience in Java you may know them with the name ArrayList. (Java also has an old collection class called Vector that is not used nowadays because of problems in how it was … Read more

How to initialize a vector of pointers

A zero-size vector of pointers: A vector of NULL pointers: A vector of pointers to newly allocated objects (not really initialization though): Initializing a vector of pointers to newly allocated objects (needs C++11): A smarter version of #3:

Appending a vector to a vector

or The second variant is a more generically applicable solution, as b could also be an array. However, it requires C++11. If you want to work with user-defined types, use ADL:

How to sum up elements of a C++ vector?

Actually there are quite a few methods. C++03 Classic for loop: for(std::vector<int>::iterator it = vector.begin(); it != vector.end(); ++it) sum_of_elems += *it; Using a standard algorithm: #include <numeric> sum_of_elems = std::accumulate(vector.begin(), vector.end(), 0); Important Note: The last argument’s type is used not just for the initial value, but for the type of the result as well. If you … Read more