vector
(constructor)
#include<vector>
std::vector<int> first; // empty vector of ints
std::vector<int> second (4,100); // four ints with value 100
std::vector<int> third (second.begin(),second.end()); // iterating through second
std::vector<int> fourth (third); // a copy of third
Iterators:
name | usage |
---|---|
begin | Return iterator to beginning |
end | Return iterator to end |
rbegin | Return reverse iterator to reverse beginning |
rend | Return reverse iterator to reverse end |
std::vector<int>::iterator it = myvector.begin()
Capacity:
name | usage |
---|---|
size | Return size |
resize | Change size |
empty | Test whether vector is empty |
std::cout << "0. size: " << myints.size() << '\n';
myvector.resize(8,100);
while (!myvector.empty());
Element access:
name | usage |
---|---|
operator[] | Access element |
front | Access first element |
back | Access last element |
myvector.front() -= myvector.back();
Modifiers:
name | usage |
---|---|
assign | Assign vector content |
push_back | Add element at the end |
pop_back | Delete last element |
insert | Insert elements |
erase | Erase elements |
swap | Swap content |
clear | Clear content |
myvector.assign(7,100);
myvector.push_back(myint);
myvector.pop_back();
it = myvector.begin();
myvector.insert(it+2,anothervector.begin(),anothervector.end());
myvector.insert(it,2,300);
myvector.insert(it,300);
myvector.erase(myvector.begin(),myvector.begin()+3);
myvector.swap(anothervector);
myvector.clear();
queue
(constructor)
#include <queue>
std::deque<int> mydeck (3,100); // deque with 3 elements
std::queue<int> first; // empty queue
std::queue<int> second (mydeck);
name | usage |
---|---|
empty | Test whether container is empty |
size | Return size |
front | Access next element |
back | Access last element |
push | Insert element |
pop | Remove next element |
swap | Swap contents |
while (!myqueue.empty());
std::cout << "0. size: " << myints.size() << '\n';
myqueue.front() -= myqueue.back();
myqueue.push (myint);
myqueue.pop();
stack
(constructor)
#include <stack>
std::deque<int> mydeck (3,100); // deque with 3 elements
std::stack<int> first; // empty stack
std::stack<int> second (mydeque);
name | usage |
---|---|
empty | Test whether container is empty |
size | Return size |
top | Access next element |
push | Insert element |
pop | Remove top element |
swap | Swap contents |
std::cout << "0. size: " << myints.size() << '\n';
mystack.push(myint);
while (!mystack.empty())
{
std::cout << ' ' << mystack.top();
mystack.pop();
}
set
(constructor)
#include <set>
std::set<int> first; // empty set of ints
int myints[]= {10,20,30,40,50};
std::set<int> second (myints,myints+5); // range
std::set<int> third (second); // a copy of second
std::set<int> fourth (second.begin(), second.end()); // iterator ctor.
Iterators:
name | usage |
---|---|
begin | Return iterator to beginning |
end | Return iterator to end |
rbegin | Return reverse iterator to reverse beginning |
rend | Return reverse iterator to reverse end |
std::set<int>::iterator it=myset.begin();
Capacity:
name | usage |
---|---|
size | Return container size |
empty | Test whether container is empty |
while (!myset.empty());
std::cout << "1. size: " << myints.size() << '\n';
Modifiers:
name | usage |
---|---|
insert | Insert elements |
erase | Erase elements |
swap | Swap content |
clear | Clear content |
myset.clear();
myset.insert (1101);
std::set<int>::iterator it = myset.begin();
myset.erase (it);
myset.erase (40);
Operations:
name | usage |
---|---|
find | Get iterator to element |
count | Count elements with a specific value |
lower_bound | Return iterator to lower bound |
upper_bound | Return iterator to upper bound |
equal_range | Get range of equal elements |
myset.erase (myset.find(40));
if (myset.count(i)!=0)
std::cout << " is an element of myset.\n";
std::set<int>::iterator itlow,itup;
itlow=myset.lower_bound (30);
itup=myset.upper_bound (60);
std::pair<std::set<int>::const_iterator,std::set<int>::const_iterator> ret;
ret = myset.equal_range(30);
std::cout << "the lower bound points to: " << *ret.first << '\n';
std::cout << "the upper bound points to: " << *ret.second << '\n';
map
(constructor)
#include <map>
std::map<char,int> first;
first['a']=10;
struct classcomp {
bool operator() (const char& lhs, const char& rhs) const
{return lhs<rhs;}
};
std::map<char,int,classcomp> fourth;
Iterators:
name | usage |
---|---|
begin | Return iterator to beginning |
end | Return iterator to end |
rbegin | Return reverse iterator to reverse beginning |
rend | Return reverse iterator to reverse end |
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
Capacity:
name | usage |
---|---|
size | Return container size |
empty | Test whether container is empty |
while (!mymap.empty()){
std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n';
mymap.erase(mymap.begin());
}
std::cout << "mymap.size() is " << mymap.size() << '\n';
Modifiers:
name | usage |
---|---|
insert | Insert elements |
erase | Erase elements |
swap | Swap content |
clear | Clear content |
mymap.clear();
mymap.insert ( std::pair<char,int>('a',100) );
mymap.erase('c');
Operations:
name | usage |
---|---|
find | Get iterator to element |
count | Count elements with a specific value |
lower_bound | Return iterator to lower bound |
upper_bound | Return iterator to upper bound |
equal_range | Get range of equal elements |
std::cout << "a => " << mymap.find('a')->second << '\n';
if (mymap.count('c')>0)
std::cout << " is an element of mymap.\n";
itlow=mymap.lower_bound ('b'); // itlow points to b
itup=mymap.upper_bound ('d'); // itup points to e (not d!)
mymap.erase(itlow,itup);