.at() vs. []
[] reads and writes vector values.
[] are unchecked.
std::vector<char> cvec;
std::vector<std::vector<int> > ivec;
Note the space in > >.
std::vector<int &> irefvec;
typedefs help keep things neat and flexible.
typedef std::vector<int> ivector; ivector ivec; std::vector<ivector> ivecvec;
std::vector<T> tvec;
v.
std::vector<T> tvec(n, v);
v is optional if T has a default value.
std::vector<int> tvec(n);
std::vector<T> tvec(n);
T needs a default value (default constructor).
std::vector<char> line2(line1);
operator[] to access elements.
line2[0] = line1[10];
[] is unsafe.
std::vector<T> tvec(10); T t1, t2; t1 = tvec[100]; // compiles and executes, but is undefined t2 = tvec[-1]; // ditto
.at() provides checked index access to vectors.
std::vector<T> tvec(10); T t1, t2; t1 = tvec.at(100); // run-time exception thrown. t2 = tvec.at(-1); // ditto
.at() vs. [].at() to [] unless you're really sure.
std::cin >> i // Terrible v[i] = ... // Good v.at(i) = ... // Much better. if (0 <= i) and (i < v.size()) v[i] = ... else std::cerr << "Invalid index... // Not a problem. for i = 0; i < v.size(); i++ v[i] = ...
size() member function gives the number of elements in a vector.
std::vector<T> tvec(10); assert(tvec.size() == 10); // it's true!
v.push_back(e) sets v[n] to e and n to
n + 1.
std::vector<int> v; assert(v.size() == 0); // it's true! int i = v[0] // it's undefined! v.push_back(100); assert(v.size() == 1); // it's true! i = v[0] // it's defined, and i == 100.
v.pop_back() removes v[n - 1] and set n to
n - 1.
std::vector<int> v(1, 321); assert(v.size() == 1); // it's true! int i = v[0] // it's defined and i == 321 v.pop_back(); assert(v.size() == 0); // it's true! i = v[0] // it's undefined!
Is it
std::vector<int> ivec; for (i = 0; i < 1000000; i++) ivec.push_back(i);
v.capacity() and v.size().
v.reserve() and v.resize().
v.capacity() returns the total number of elements v can hold
without resizing.
v.capacity() does not return the amount of empty space left in
v.
v.size() returns the number of elements actually in v.
size() is supported by all containers.
v.size() <= v.capacity() is an invariant for any vector v.
v.size() == v.capacity(), the next push back causes a
reallocation.
v.resize(n) forces v to have n elements.
v.resize(n), v.size() == n.
v has n elements, v.resize(n) does nothing.
v.reserve(n) insures v can hold at least n elements
without a reallocation.
v.reserve(n), v.capacity() >= n.
v.size() remains unchanged.
char str1[14] = "Hello World!"; char str2[14] = "Hello World!"; // prints "true" or "false"? std::cout << (str1 == str2) << "\n";
std::vector<int> ivec1; for (int i = 0; i < 5; i++) ivec1.push_back(i); std::vector<int> ivec2(ivec1); // prints "true" or "false"? std::cout << (ivec1 == ivec2) << "\n";
v1 == v2 is true if and only if
v1.size() == v2.size()
for (int i = 0; i < v1.size(); i++) v1[i] == v2[i]
< is the lexicographic order over vectors.
<=, !=, >, and
>=) can be defined in terms of == and <.
v1 < v2 if
v1 is less than the same element of v2,
v1[i] < v2[i]
v1 are equal to the elements in v2.
for (j = 0; j < i; j++) v1[j] == v2[j]
v1 are equal to the elements of v2,
for (i = 0; i < v1.size(); i++) v1[i] == v2[i]
v1 is shorter than v2.
v1.size() < v2.size()
std::vector<T> defines several types.
std::vector<T>::value_type is the type of an element.
T.
std::vector<T>::pointer is the type of an element pointer.
T * (usually).
std::vector<T>::difference_type is the type of a value holding the
distance between any two elements of a vector.
std::vector<T>::size_type is the type of a value holding a vector's
size.
v1 = v2 copies elements, it doesn't share them.
v1 and v2 sizes need not match.
v.empty() returns true iff v has no elements.
c.empty() instead of c.size() == 0.
.empty() is cheaper to call than is
.size().
v.clear() removes all elements from a vector.
v.front() returns the first element of v.
v may be empty; behavior is undefined.
v.back() is defined similarly.
std::swap(v1, v2) exchanges the elements of v1 and v2.
v2.
.at().
[] when index values are certain.
This page last modified on 6 February 2003.