Container

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:

nameusage
beginReturn iterator to beginning
endReturn iterator to end
rbeginReturn reverse iterator to reverse beginning
rendReturn reverse iterator to reverse end
std::vector<int>::iterator it = myvector.begin()

Capacity:

nameusage
sizeReturn size
resizeChange size
emptyTest whether vector is empty
std::cout << "0. size: " << myints.size() << '\n';
myvector.resize(8,100);
while (!myvector.empty());

Element access:

nameusage
operator[]Access element
frontAccess first element
backAccess last element
myvector.front() -= myvector.back();

Modifiers:

nameusage
assignAssign vector content
push_backAdd element at the end
pop_backDelete last element
insertInsert elements
eraseErase elements
swapSwap content
clearClear 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);
nameusage
emptyTest whether container is empty
sizeReturn size
frontAccess next element
backAccess last element
pushInsert element
popRemove next element
swapSwap 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);
nameusage
emptyTest whether container is empty
sizeReturn size
topAccess next element
pushInsert element
popRemove top element
swapSwap 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:

nameusage
beginReturn iterator to beginning
endReturn iterator to end
rbeginReturn reverse iterator to reverse beginning
rendReturn reverse iterator to reverse end
std::set<int>::iterator it=myset.begin();

Capacity:

nameusage
sizeReturn container size
emptyTest whether container is empty
while (!myset.empty());
std::cout << "1. size: " << myints.size() << '\n';

Modifiers:

nameusage
insertInsert elements
eraseErase elements
swapSwap content
clearClear content
myset.clear();
myset.insert (1101);
std::set<int>::iterator it = myset.begin();
myset.erase (it);
myset.erase (40);

Operations:

nameusage
findGet iterator to element
countCount elements with a specific value
lower_boundReturn iterator to lower bound
upper_boundReturn iterator to upper bound
equal_rangeGet 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:

nameusage
beginReturn iterator to beginning
endReturn iterator to end
rbeginReturn reverse iterator to reverse beginning
rendReturn 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:

nameusage
sizeReturn container size
emptyTest 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:

nameusage
insertInsert elements
eraseErase elements
swapSwap content
clearClear content
mymap.clear();
mymap.insert ( std::pair<char,int>('a',100) );
mymap.erase('c');

Operations:

nameusage
findGet iterator to element
countCount elements with a specific value
lower_boundReturn iterator to lower bound
upper_boundReturn iterator to upper bound
equal_rangeGet 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); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值