Namespaces
Variants
Actions

Difference between revisions of "intro/vector"

From cppreference.com
(Created page with " == Vector == The vector container, declared in ''<vector>'', allows for keeping a variable sequence of elements of arbitrary data type. These are its essential properties: ...")
 
m (replace pre tag with source template)
Line 6: Line 6:
 
* The data type of the elements in the vector can be defined as a template argument:
 
* The data type of the elements in the vector can be defined as a template argument:
  
<pre>std::vector<int> v1; // a vector of integers
+
{{source|1=
 +
std::vector<int> v1; // a vector of integers
 
std::vector<int*> v2; // a vector of pointers to integer(s)
 
std::vector<int*> v2; // a vector of pointers to integer(s)
 
std::vector<std::string> v3; // a vector of strings
 
std::vector<std::string> v3; // a vector of strings
 
std::vector<std::vector<int> > v3; // a vector of vectors of int's
 
std::vector<std::vector<int> > v3; // a vector of vectors of int's
</pre>
+
}}
  
 
* Vectors have a variable number of elements, which can be inserted, removed and accessed in runtime:
 
* Vectors have a variable number of elements, which can be inserted, removed and accessed in runtime:
  
<pre>using namespace std;
+
{{source|1=
 +
using namespace std;
 
vector<int> vec = {1,2,3}; // you can initialize vectors with an initializer list since C++11
 
vector<int> vec = {1,2,3}; // you can initialize vectors with an initializer list since C++11
 
cout << vec.size() << endl; // will print 3  
 
cout << vec.size() << endl; // will print 3  
Line 22: Line 24:
 
cout << vec.front() << endl; // first element, will print 1  
 
cout << vec.front() << endl; // first element, will print 1  
 
cout << vec.size() << endl; // will print 4  
 
cout << vec.size() << endl; // will print 4  
</pre>
+
}}
  
 
* Elements in a vector are contiguous like in an array, so it provides fast read/write operations on any element. Internally, these are kept in a dynamically allocated array of larger (or equal) size, so that it can grow larger. If this array is full before inserting another element, the vector will relocate every single element currently in it into a larger array. This means two things: there is no theoretical limit on the number of elements of a vector, but inserting many elements at a time will force a capacity increase of the vector, which can become a heavy operation. You can use the capacity() member function in order to find to what size can the vector grow to without an expansion.
 
* Elements in a vector are contiguous like in an array, so it provides fast read/write operations on any element. Internally, these are kept in a dynamically allocated array of larger (or equal) size, so that it can grow larger. If this array is full before inserting another element, the vector will relocate every single element currently in it into a larger array. This means two things: there is no theoretical limit on the number of elements of a vector, but inserting many elements at a time will force a capacity increase of the vector, which can become a heavy operation. You can use the capacity() member function in order to find to what size can the vector grow to without an expansion.
<pre>
+
 
 +
{{source|1=
 
vector<int> vec = {0, 3, 4, 6};
 
vector<int> vec = {0, 3, 4, 6};
 
cout << vec.capacity() << endl; // will print a number greater or equal to 4
 
cout << vec.capacity() << endl; // will print a number greater or equal to 4
Line 32: Line 35:
 
vec.shrink_to_fit(); // since C++11, you can contract the vector to the minimum capacity needed
 
vec.shrink_to_fit(); // since C++11, you can contract the vector to the minimum capacity needed
 
cout << vec.capacity() << endl; // will print 4
 
cout << vec.capacity() << endl; // will print 4
</pre>
+
}}
  
 
* Access to an element out of the vector's range (using a bad index for example) will not necessarily cause an error with the array suffix operator[]. On the other hand, the at() function includes bounds checking.
 
* Access to an element out of the vector's range (using a bad index for example) will not necessarily cause an error with the array suffix operator[]. On the other hand, the at() function includes bounds checking.
<pre>vector<float> vec (3, 2.0f); // initialize a vector with 3 elements set to 2.0f
+
 
 +
{{source|1=
 +
vector<float> vec (3, 2.0f); // initialize a vector with 3 elements set to 2.0f
 
float myfloat1 = vec[2]; // OK
 
float myfloat1 = vec[2]; // OK
 
float myfloat2 = vec[3]; // undefined behavior!
 
float myfloat2 = vec[3]; // undefined behavior!
Line 41: Line 46:
 
float myfloat3 = vec.at(1); // OK
 
float myfloat3 = vec.at(1); // OK
 
vec.at(6) = 1.0f; // will throw std::out_of_range exception!
 
vec.at(6) = 1.0f; // will throw std::out_of_range exception!
</pre>
+
}}
  
 
* Like any other container, they can be iterated using a standard iterator or a for-each statement:
 
* Like any other container, they can be iterated using a standard iterator or a for-each statement:
  
<pre>vector<int> vec = {5, 3, 1};
+
{{source|1=
 +
vector<int> vec = {5, 3, 1};
 
for (auto it = begin(vec) ; it != end(vec) ; it++) {
 
for (auto it = begin(vec) ; it != end(vec) ; it++) {
 
  cout << *it << endl; // print each element
 
  cout << *it << endl; // print each element
Line 52: Line 58:
 
for (int& e : vec) {
 
for (int& e : vec) {
 
  e++; // increment each element by one
 
  e++; // increment each element by one
}</pre>
+
}
 +
}}
  
 
Vectors are preferred when keeping a sequence of elements for random access due to its ''O(1)'' complexity. Once created, it can be accessed in a similar fashion to an array. If the needed capacity for the vector is unknown, it can still be used without major issues, thanks to the various element insertion and removal functions available.
 
Vectors are preferred when keeping a sequence of elements for random access due to its ''O(1)'' complexity. Once created, it can be accessed in a similar fashion to an array. If the needed capacity for the vector is unknown, it can still be used without major issues, thanks to the various element insertion and removal functions available.

Revision as of 17:04, 8 February 2014

Vector

The vector container, declared in <vector>, allows for keeping a variable sequence of elements of arbitrary data type. These are its essential properties:

  • The data type of the elements in the vector can be defined as a template argument:
std::vector<int> v1; // a vector of integers
std::vector<int*> v2; // a vector of pointers to integer(s)
std::vector<std::string> v3; // a vector of strings
std::vector<std::vector<int> > v3; // a vector of vectors of int's
  • Vectors have a variable number of elements, which can be inserted, removed and accessed in runtime:
using namespace std;
vector<int> vec = {1,2,3}; // you can initialize vectors with an initializer list since C++11
cout << vec.size() << endl; // will print 3 
vec.push_back(5); // insert 5 at the end of the vector
cout << vec[3] << endl; // will print 5 
vec[1] = 10; // change the second element to 10
cout << vec.front() << endl; // first element, will print 1 
cout << vec.size() << endl; // will print 4
  • Elements in a vector are contiguous like in an array, so it provides fast read/write operations on any element. Internally, these are kept in a dynamically allocated array of larger (or equal) size, so that it can grow larger. If this array is full before inserting another element, the vector will relocate every single element currently in it into a larger array. This means two things: there is no theoretical limit on the number of elements of a vector, but inserting many elements at a time will force a capacity increase of the vector, which can become a heavy operation. You can use the capacity() member function in order to find to what size can the vector grow to without an expansion.
vector<int> vec = {0, 3, 4, 6};
cout << vec.capacity() << endl; // will print a number greater or equal to 4
vec.reserve(10); // you can also force an expansion of the vector
cout << vec.capacity() << endl; // will print 10
vec.shrink_to_fit(); // since C++11, you can contract the vector to the minimum capacity needed
cout << vec.capacity() << endl; // will print 4
  • Access to an element out of the vector's range (using a bad index for example) will not necessarily cause an error with the array suffix operator[]. On the other hand, the at() function includes bounds checking.
vector<float> vec (3, 2.0f); // initialize a vector with 3 elements set to 2.0f
float myfloat1 = vec[2]; // OK
float myfloat2 = vec[3]; // undefined behavior!
vec[6] = myfloat1; // undefined behavior!
float myfloat3 = vec.at(1); // OK
vec.at(6) = 1.0f; // will throw std::out_of_range exception!
  • Like any other container, they can be iterated using a standard iterator or a for-each statement:
vector<int> vec = {5, 3, 1};
for (auto it = begin(vec) ; it != end(vec) ; it++) {
 cout << *it << endl; // print each element
}
 
for (int& e : vec) {
 e++; // increment each element by one
}

Vectors are preferred when keeping a sequence of elements for random access due to its O(1) complexity. Once created, it can be accessed in a similar fashion to an array. If the needed capacity for the vector is unknown, it can still be used without major issues, thanks to the various element insertion and removal functions available.