find(start, end, item)
- look for item
in the container
part delimited by start
and end
.
[]
<class T>
T
gives the type of the values stored in
the vector
vector<char> cvec;
vector<vector<char> > cvecs;
typedef
s are helpful
typedef vector<char> cvector;
cvector cvec;
vector<cvector> cvecs;
vector<T> tvec;
vector<T> tvec(n, v);
v
is an optional parameter - the default value
is the constructor for v
's type
vector<T> tvec(n);
vector<int> tvec(n);
vector<char> line2(line1);
[]
to access vector values
line2[0] = line1[10]
n
-element vector, the valid indices are 0 through n-1
inclusive
[]
is unsafe
vector<T> tvec(10); T t1, t2; t1 = tvec[100]; // compiles and executes, but is undefined t2 = tvec[-1]; // ditto
size()
member function gives the number of values held by a
vector
vectortvec(10); assert(tvec.size() == 10); // it's true!
n
-element vector v
, the call v.push_back(e)
adds the element e
as the n
+1-element of v
vector<int> tvec(10); assert(tvec.size() == 10); // it's true! tvec.push_back(100); assert(tvec.size() == 11); // it's true!
n
-element vector v
, n
> 0, the call
v.pop_back()
removes and throws away the n
th element
of v
vector<int> tvec(10); assert(tvec.size() == 10); // it's true! tvec.pop_back(); assert(tvec.size() == 9); // it's true!
char str1[14] = "Hello World!"; char str2[14] = "Hello World!"; cout << (str1 == str2) << "\n"; // prints "true" or "false"?
vectorivec1; for (int i = 0; i < 5; i++) ivec1.push_back(i); vector ivec2(ivec1); cout << (ivec1 == ivec2) << "\n"; // prints "true" or "false"?
==
is defined as: v1 == v2
is true
if and only if
v1.size() == v2.size()
and
for (int i = 0; i < v1.size(); i++) v1[i] == v2[i]
<
is defined as: v1 < v2
is true
if and only if
v1.size() < v2.size()
and
for (int i = 0; i < v1.size(); i++) v1[i] < v2[i]
<=
, !=
, >
, and
>=
) can be defined in terms of ==
and <
*
operator follows an iterator to its value
x = *(itr + 5)
+=
and -=
for jumping
around
itr += 5
cnt = end_itr - start_itr - 1;
<
operator - does one iterator refer to a value earlier in the
sequence than another iterator
(start, end)
,
start
and end
refers to values within the
same container
vector<T>::iterator
vector<T>::const_iterator
begin()
- return an iterator to the first element in a container
vector<int> ivec; vector<int>::iterator start = ivec.begin();
end()
- return an iterator to the last element in a container
vector<int>::iterator five = find(ivec.start(), ivec.end(), 5);
This page last modified on 6 April 2001.