To initialize a std::vector class template, you can use any of the following methods, but you are not restricted to only these:
- Initialize from an initialization list:
std::vector<int> v1 { 1, 2, 3, 4, 5 };
- Initialize from a C-like array:
int arr[] = { 1, 2, 3, 4, 5 };
std::vector<int> v2(arr, arr + 5); // { 1, 2, 3, 4, 5 }
- Initialize from another container:
std::list<int> l{ 1, 2, 3, 4, 5 };
std::vector<int> v3(l.begin(), l.end()); //{ 1, 2, 3, 4, 5 }
- Initialize from a count and a value:
std::vector<int> v4(5, 1); // {1, 1, 1, 1, 1}
To modify the content of std::vector, use any of the following methods, but you are not restricted to only these:
- Add an element at the end of the vector with push_back():
std::vector<int> v1{ 1, 2, 3, 4, 5 };
v1.push_back(6); // v1 = { 1, 2, 3, 4, 5, 6 }
- Remove an element from the end of the vector with pop_back():
v1.pop_back();
- Insert anywhere in the vector with insert():
int arr[] = { 1, 2, 3, 4, 5 };
std::vector<int> v2;
v2.insert(v2.begin(), arr, arr + 5); // v2 = { 1, 2, 3, 4, 5 }
- Add an element by creating it at the end of the vector with emplace_back():
struct foo
{
int a;
double b;
std::string c;
foo(int a, double b, std::string const & c) :
a(a), b(b), c(c) {}
};
std::vector<foo> v3;
v3.emplace_back(1, 1.0, "one"s);
// v3 = { foo{1, 1.0, "one"} }
- Insert an element by creating it anywhere in the vector with emplace():
v3.emplace(v3.begin(), 2, 2.0, "two"s);
// v3 = { foo{2, 2.0, "two"}, foo{1, 1.0, "one"} }
To modify the whole content of the vector, use any of the following methods, but you are not restricted to only these:
- Assign from another vector with operator=; this replaces the content of the container:
std::vector<int> v1{ 1, 2, 3, 4, 5 };
std::vector<int> v2{ 10, 20, 30 };
v2 = v1; // v1 = { 1, 2, 3, 4, 5 }
- Assign from another sequence defined by a begin and end iterator with the assign() method; this replaces the content of the container:
int arr[] = { 1, 2, 3, 4, 5 };
std::vector<int> v3;
v3.assign(arr, arr + 5); // v3 = { 1, 2, 3, 4, 5 }
- Swap the content of two vectors with the swap() method:
std::vector<int> v4{ 1, 2, 3, 4, 5 };
std::vector<int> v5{ 10, 20, 30 };
v4.swap(v5); // v4 = { 10, 20, 30 }, v5 = { 1, 2, 3, 4, 5 }
- Remove all the elements with the clear() method:
std::vector<int> v6{ 1, 2, 3, 4, 5 };
v6.clear(); // v6 = { }
- Remove one or more elements with the erase() method (which requires either an iterator or a pair of iterators that define the range of elements from the vector to be removed):
std::vector<int> v7{ 1, 2, 3, 4, 5 };
v7.erase(v7.begin() + 2, v7.begin() + 4); // v7 = { 1, 2, 5 }
To get the address of the first element in a vector, usually to pass the content of a vector to a C-like API, use any of the following methods:
- Use the data() method, which returns a pointer to the first element, providing direct access to the underlying contiguous sequence of memory where the vector elements are stored; this is only available since C++11:
void process(int const * const arr, int const size)
{ /* do something */ }
std::vector<int> v{ 1, 2, 3, 4, 5 };
process(v.data(), static_cast<int>(v.size()));
- Get the address of the first element:
process(&v[0], static_cast<int>(v.size()));
- Get the address of the element referred by the front() method:
process(&v.front(), static_cast<int>(v.size()));
- Get the address of the element pointed by the iterator returned from begin():
process(&*v.begin(), static_cast<int>(v.size()));