vector<int> ivec; // vector of int's vector<char> cvec; // vector of char's vector<Critter> crvec; // vector of Critter's
vector | list |
int size() const -- returns the number of elements | |
void clear() -- resets the list to an empty list | |
bool empty() const -- returns true if the list is empty, or false otherwise | |
void push_back(const Object& x) -- adds an element (x) to the end of the list | |
void pop_back() -- removes the last element in the list |
|
const Object& front() const -- returns the first element in the list (but does not remove it) | |
const Object& back() const -- returns the last element in the list (but does not remove it) | |
Object& operator[](int index) -- returns the element at the parameter index position (0-based), with no bounds-checking |
|
Object& at(int index) -- returns the element at the parameter index position (0-based), with bounds-checking |
|
void push_front(const Object& x) -- add the element x in front of the list |
|
void pop_front() -- removes the first element in the list |
#include <iostream> #include <vector> // STL vector class #include <list> // STL list class using namespace std; int main() { vector<int> vec; // a vector of int's, initially empty list<char> lst; // a list of char's, initially empty // populate the vector and list for (int i = 0; i < 4; i++) vec.push_back(i*2); // vec contains int's lst.push_back('a'); // lst contains char's lst.push_front('9'); // vector has two ways to access elements by index. cout << vec[1]; // operator[] with index 1 (i.e., vec.operator[](1)) cout << vec.at(3); // the method at() with index 3 // size() is defined for both containers cout << vec.size(); cout << lst.size(); // try removing all elements from vec until empty while (!vec.empty()) vec.pop_back();
+-------+ +-------+ +-------+ +-------+ ..-->| 5 | o---->| 3 | o---->| 1 | o-----| 5 | o---->... +-------+ +-------+ +-------+ +-------+ ^ | +-|-+ it | o | +---+
vector<int>::iterator vi; vector<char>::iterator vc; list<int>::iterator li; list<Critter>::iterator lc;
Range: [ beg = vec.begin(), end = vec.end() )
+-----------+ v | 5 | 9 | 3 | +-----------+ ^ ^ | | beg end
cout << *beg; // returns the element in the list pointed by 'beg'
vector<int> vec; //..Assume some values are added in vec. vector<int>::iterator it; for (it = vec.begin(); it != vec.end(); it++) cout << *it;
vector<int> vec; /** Add some elements in the vector */ vec.push_back(7); // [7] vec.push_back(3); // [7, 3] vec.push_back(5); // [7, 3, 5] /** Insert and elements using iterators */ vector<int>::iterator it; it = vec.begin(); it++; // it points to 3 vec.insert(it, 1); // [7, 1, 3, 5] it = vec.begin(); // reset it to point to the beginning it++; it++; // it points 3 vector<int>::it2, it3; it2 = vec.erase(it); // delete 3, it2 points to 5 it3 = vec.erase(it2); // delete 5, it3 points to the end of the list if (it3 == vec.end()) cout << "End of the list\n"; else cout << "ERROR!!\n";
int main() { vector<int> vec; //.. Assume some values are added in vec. printVector(vec); ... } void printVector(const vector<int> & v) // v is a const object { vector<int>::const_iterator cit; // const_iterator, NOT iterator for (cit = v.begin(); cit != v.end(); cit++) cout << *cit << " "; }
vector<int>::iterator it; for (it = vec.begin(); it < vec.end(); it++) // < instead of != works, ONLY for vector cout << *it;
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<char> v(5); // Assume some char's are inserted in v vector<char>::iterator it; it = find( v.begin(), v.end(), 'e' ); // search 'e' between the two iterators if (it != v.end()) cout << "Element found!!\n"; else cout << "Element not found..\n"; reverse( v.begin(), v.end() ); // reverse elements in the specified range sort( v.begin(), v.end() ); // sort elements in the specified range
EXERCISES:
vector<char>::iterator myVectorFind(vector<char>::iterator start, vector<char>::iterator end, char x);