Container: 用來放資料的容器
container-type<element-type> name;
東西放在container.begin()到container.end()-1
Sequence container: list, vector,
deque.
Associative container: map, set,
multi-map, multi-set.
Container adaptor: queue, stack,
priority queue
Sequence Container
list
is good at unknown number of data, and random insertion or deletion.
vector
is good at random access.
deque
is good at head insertion or deletion.
Vector: A dynamic array container.
#include <vector>
using
namespace std;
List: A linked-list container.
#include <list>
using
namespace std;
Deque: A double-ended queue.
#include <deque>
using
namespace std;
Example:
#include<vector>
using namespace std;
int main()
{
vector<int>
ivec;
for(int
ix=0;ix<24;++ix)
{
ivec.push_back(ix);
}
}
Iterator: Pointer to
container.
container-type<element-type>::iterartor name;
Example:
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<int> ivec;
for(int ix=0;ix<10;++ix)
{
ivec.push_back(ix);
}
vector<int>::iterator iter=ivec.begin();
vector<int>::iterator iter_end=ivec.end();
for(;iter!=iter_end;iter++)
{
cout<<*iter<<” ”;
}
}
Output:
0 1 2 3 4 5 6 7 8 9
Operations on sequence
containers
insert:
Insert data in front of place.
Container.insert(place, simple_value);
Container.insert(place, pointer1, pointer2);
Container.onsert(place, simple_value, time);
Container.push_back(value);
erase:
Delete data.
Container.erase(place);
Container.erase(pointer1, pointer2);
Container.pop_back();
assign
Container1 = Container2;
swap
Container1.swap(Container2);
find
Container.find(pointer1, pointer2,value);
Container.find(value);
Pair: A template for
heterogeneous pairs of values.
#include <utility>
pair <class T1, class T2> name;
Example:
#include<utility>
using namespace std;
int main()
{
pair<char,int> pai;
pai.first=’A’;
pai.second=3;
}
Associative Container
Map: Access to non-key values using
unique keys.
#include <map>
template <class Key, class T, class Compare =
less<Key> class Allocator = allocator<pair<const Key, T>>
> class map;
count(keyvalue): return 1 if keyvalue exist, else 0.
find(keyvalue): return an iterator pointing to the position
when succeed, return an iterator pointing to end().
erase(keyvalue): erase the element with key=keyvalue
Example:
#include<map>
#include<iostream>
using namespace std;
int main()
{
map<char,int> pai;
pai['A']=3;
pai['A']=4;
pai.insert(map<char,int>::value_type('B',5));
cout<<pai['A']<<" "<<pai['B'];
}
Output:
4 5
Set: An associative container that
supports unique keys.
#include <set>
template <class Key, class Compare = less<Key>, class
Allocator = allocator<Key> > class set ;
Example:
#include<set>
#include<iostream>
using namespace std;
int main()
{
set<char> pai;
pai.insert('A');
pai.insert('A');
pai.insert('B');
cout<<pai.size();
}
Output:
2
Multiset: An associative container that
allows fast access to stored key values. Storage of duplicate keys is allowed.
#include <set>
multiset<type> multisetName;
Multimap: An associative container that
gives access to non-key values using keys. Multimap keys are not required to be
unique.
#include <map>
multimap<key_type, value_type> multimapName;
multiset and multimap both guarantees
that elements with the same key will be stored continuously
Example:
#include<map>
#include<utility>
#include<iostream>
using namespace std;
void main()
{
multimap<string, string> authors;
typedef multimap<string,string>::value_type valType;
authors.insert(valType("advprog","aa"));
authors.insert(valType("advprog","bb"));
typedef multimap<string,string>::iterator iter;
pair<iter, iter> pos;
pos=authors.equal_range("advprog");
for(;pos.first!=pos.second;pos.first++)
{
cout<<(*pos.first).second<<" ";
}
}Output:
aa bb
Container Adaptor
Stack: A container adapter that behaves
like a stack.
#include <stack>
stack<type[,Container]> stackName;
empty();size();pop();top();push(item);
Queue: A container adaptor that behaves
like a queue.
#include <queue>
queue<type[,Container]> queueName;
empty();size();pop();front();back();push(item);
Priority-queue: A container adaptor
that behaves like a priority-queue.
#include <queue>
queue<type[,Container]> queueName;
empty();size();pop();top();push(item);
Iterators
input
iterator: Read only, forward moving
output
iterator: Write only, forward moving
forward
iterator: Both read and write, forward moving
bidirectional
iterator: Read and write, forward and backward moving
random
access iterator: Read and write, random access