Professional Documents
Culture Documents
Container types (and algorithms, functions and all STL as well) are defined
not in global namespace, but in special namespace called std." Add the
following line after your #include and before the code begins:
using namespace std;
- vector<int> v(20);
-
v[i] = i+1;
v.resize(25);
v[i] = i*2;
}
Note that if you use push_back() after resize(), it will add elements
AFTER the newly allocated size, but not INTO it. In the example
above the size of the resulting vector is 25, while if we use
push_back() in a second loop, it would be 30.
vector<int> v(20);
for(int i = 0; i < 20; i++) {
v[i] = i+1;
}
v.resize(25);
for(int i = 20; i < 25; i++) {
v.push_back(i*2); // Writes to elements with indices [25..30), not
[20..25) ! <
}
int N,M;
// ...
vector< vector<int> > Matrix(N, vector<int>(M));
So why use iterators? First, they're a flexible way to access the data in containers that
don't have obvious means of accessing all of the data (for instance, maps [to be
discussed later]). They're also quite flexible -- if you change the underlying container,
it's easy to change the associated iterator so long as you only use features associated
with the iterator supported by both classes. Finally, the STL algorithms defined in
<algorithm> (to be discussed later) use iterators.
PAIRS
template<typename T1, typename T2> struct pair {
T1 first;
T2 second;
};
To create an iterator object, we must specify its type. The type of iterator
can be constructed by a type of container by appending ::iterator,
::const_iterator, ::reverse_iterator or ::const_reverse_iterator to it.
Thus, vector can be traversed in the following way:
vector<int> v;
// ...
Dont use <= while using vectors. i.e dont use comparison
operators
To request an iterator appropriate for a particular STL templated class, you use the syntax
std::class_name<template_parameters>::iterator name
Iterators are distinguished by whether you can use them for reading or writing data in the
container. Some types of iterators allow for both reading and writing behavior, though not
necessarily at the same time.
STL MAPS
Suppose that you're working with some data that has values associated with strings -- for
instance, you might have student usernames and you want to assign them grades. How would you
go about storing this in C++? One option would be to write your own hash table. This will require
writing a hash function and handling collisions, and lots of testing to make sure you got it right. On
the other hand, the standard template library (STL) includes a templated class to handle just this
sort of situation: the STL map class, which conceptually you can think of as an "associative array"
-- key names are associated with particular values (e.g., you might use a student name as a key,
and the student's grade as the data).
std::map <key_type, data_type, [comparison_function]>
We don't need to specify a comparison function for so-called primitive types such as int, char, or
even for the string class.