序列式容器
std::cout<<"c2 front : "<<c2.front()<<std::endl;
std::cout<<"c2 back : "<<c2.back()<<std::endl;
vector<int> c;
vector<int> c2(c1);
vector<int> c(n);//利用元素default构造函数生成一个大小为n的vector
vector<int> c(n,elem);
vector<int> c(beg,end);
c.size();
c.empty();
c.max_size();
c.capacity();
c.reserve(num);
c1 = c2;
c.assign(c,elem);
c.assign(beg,end);
c1.swap(c2);
swap(c1,c2);//全局函数
c.at(idx);//返回索引idx所标示的元素。如果idx越界,抛出out_of_range
c[idx];//不进行范围检查
c.front();//不检查第一个元素是否存在
c.back();//不检查最后一个元素是否存在
c.begin();
c.end();
c.rbegin();
c.rend();
c.insert(pos,elem);
c.insert(pos,n,elem);
c.insert(pos,beg,end);
c.push_back(elem);
c.pop_back();//移除最后一个元素,但是不回传
c.erase(pos);//移除pos上的元素,返回下一元素的位置
c.erase(beg,end);//移除...,返回下一元素的位置
c.resize(num);//将元素数量改为num
c.resize(num,elem);//将元素数量改为num(如果size()变大了,多出来的新元素都是elem的副本)
c.clear();
2、deque
deque<int> c;
deque<int> c2(c1);
deque<int> c(n);//利用元素default构造函数生成一个大小为n的vector
dequer<int> c(n,elem);
deque<int> c(beg,end);
c.size();
c.empty();
c.max_size();
c1 = c2;
c.assign(c,elem);
c.assign(beg,end);
c1.swap(c2);
swap(c1,c2);//全局函数
c.at(idx);//返回索引idx所标示的元素。如果idx越界,抛出out_of_range
c[idx];//不进行范围检查
c.front();//不检查第一个元素是否存在
c.back();//不检查最后一个元素是否存在
c.begin();
c.end();
c.rbegin();
c.rend();
c.insert(pos,elem);
c.insert(pos,n,elem);
c.insert(pos,beg,end);
c.push_back(elem);
c.pop_back();//移除最后一个元素,但是不回传
c.push_front(elem);
c.pop_front();//移除头部元素,但是不回传
c.erase(pos);//移除pos上的元素,返回下一元素的位置
c.erase(beg,end);//移除...,返回下一元素的位置
c.resize(num);//将元素数量改为num
c.resize(num,elem);//将元素数量改为num(如果size()变大了,多出来的新元素都是elem的副本)
c.clear();
//不提供以下
//c.capacity();
//c.reserve(num);
3、Lists
list<int> c;
list<int> c2(c1);
list<int> c(beg,end);
c.size();
c.empty();
c.max_size();
c1 = c2;
c.assign(c,elem);
c.assign(beg,end);
c1.swap(c2);
swap(c1,c2);//全局函数
c.front();//不检查第一个元素是否存在
c.back();//不检查最后一个元素是否存在
c.begin();
c.end();
c.rbegin();
c.rend();
c.insert(pos,elem);
c.insert(pos,n,elem);
c.insert(pos,beg,end);
c.push_back(elem);
c.pop_back();//移除最后一个元素,但是不回传
c.push_front(elem);
c.pop_front();//移除头部元素,但是不回传
c.remove(val);//移除所有值为val的元素
c.remove_if(op); //移除所有"造成op(elem)结果为true"的元素
c.erase(pos);//移除pos上的元素,返回下一元素的位置
c.erase(beg,end);//移除...,返回下一元素的位置
c.resize(num);//将元素数量改为num
c.resize(num,elem);//将元素数量改为num(如果size()变大了,多出来的新元素都是elem的副本)
c.clear();
///
c.unique();
c.unique(op);
c1.splice(pos,c2);
c1.splice(pos,c2,c2pos);
c1.splice(pos,c2,c2beg,c2end);
c.sort();
c.sort(op);
c1.merge(c2);
c1.merge(c2,op);
c.reverse();//将所有元素反序
/
关联式容器
1、set
set<int> c;
set<int> c(op);
set<int> c1(c2);
set<int> c(beg,end,op);
c.size();
c.empty();
c.max_size();
c.count(elem);
c.find(elem);
c.lower_bound(elem);
c.upper_bound(elem);
c.equal_range(elem);
c.swap(c1)或全局函数swap(c,c1);
c.begin();
c.end();
c.rbegin();
c.rend();
c.insert(elem);
c.insert(pos,elem);
c.insert(beg,end);
c.erase(elem);//返回移除元素个数
c.erase(pos);//无返回值
c.erase(beg,end);//无返回值
c.clear();
//set提供如下接口
pair<iterator, bool> insert(const value_type &elem);
iterator insert(iterator pos_hint, const value_type &elem);
//multiset提供如下接口
iterator insert(const value_type &elem);
iterator insert(iterator pos_hint, const value_type &elem);
//
typedef set<int, greater<int> > IntSet;
typedef multiset<int, greater<int> > IntSet;
2、map或multimap
map<int> c;
map<int> c(op);
map<int> c1(c2);
map<int> c(beg,end,op);
c.empty();
c.max_size();
c.count(key);
c.find(key);
c.lower_bound(key);
c.upper_bound(key);
c.equal_range(key);
c1.swap(c2)或全局函数swap(c1,c2);
c.begin();
c.end();
c.rbegin();
c.rend();
c.insert(elem);
c.insert(pos, elem);
c.insert(beg,end);
c.erase(elem);//返回移除元素个数
c.erase(pos);//无返回值
c.erase(beg,end);//无返回值
c.clear();
c[key];//指向键值为key的元素,如果不存在,就安插该元素。
①为了避免隐式型别转换,可以用value_type传递正确型别。
std::map<std::string, float> coll;
coll.insert(std::map<std::string, float>::value_type("otto", 22.3));
②应用pair<>
std::map<std::string, float> coll;
coll.insert(std::pair<std::string, float>("otto", 22.3));
coll.insert(std::pair<const std::string, float>("otto", 22.3));
③应用make_pair()
std::map<std::string, float> coll;
coll.insert(std::make_pair("otto", 22.3));
//
std::map<std::string, float> coll;
if (coll.insert(std::make_pair("otto", 22.3)).second) {
std::cout<<"OK, could insert otto/22.3"<<std::endl;
} else {
std::cout<<"Oops, could not insert otto/22.3"
<<"(lkey otto already exists)"<<std::endl;
}
//
StringFloatMap coll;
StringFloatMap::iterator pos = coll.find(key);
if (pos != coll.end()) {
coll.erase(pos);
}
///
typedef std::map<float, std::string, std::greater<float> > coll;
typedef std::map<std::string, float, std::greater<std::string> > StringFloatMap;
///
//下面是移除"迭代器所指元素"的正确做法:
typedef std::map<std::string, float> StringFloatMap;
StringFloatMap coll;
StringFloatMap::iterator pos;
//...
//remove all elements having a certain value
for (pos = coll.begin(); pos != coll.end(); ++ pos) {
if (pos->second == value) {
coll.erase(pos ++);
} else {
++ pos;
}
}
===========
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
queue<string> q;
q.push("these ");
q.push("are ");
q.push("more than ...");
//read and print
cout << q.front();
q.pop();
cout<<q.front();
q.pop();
//insert two new elements
q.push("four ");
q.push("words! ");
q.pop();
//skip and print
cout<<q.front();
q.pop();
cout<<q.front()<<endl;
q.pop();
//print number of elements in the queue
cout<<"number of elements in the queue: "<<q.size()<<endl;
return 0;
}
==================
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
stack<int> st;
st.push(1);
st.push(2);
st.push(3);
cout<<st.top()<<" ";
st.pop();
cout<<st.top()<<" ";
st.pop();
//modify top elements
st.top() = 77;
st.push(4);
st.push(5);
cout<<"\nsize:"<<st.size()<<"\n";
st.pop();
while ( !st.empty() ) {
cout<<st.top()<<" ";
st.pop();
}
cout<<endl;
return 0;
}
==========================
1、vector
#include <iostream>
#include <vector>
#include <algorithm> //std::swap(c1,c2);
void print(std::vector<int> v)
{
std::cout<<"item as:\n";
std::vector<int>::iterator iter;
for (iter = v.begin(); iter != v.end(); ++ iter) {
std::cout<<*iter<<"\n";
}
}
void print_reverse(std::vector<int> v)
{
std::cout<<"item as:\n";
std::vector<int>::reverse_iterator iter;
for (iter = v.rbegin(); iter != v.rend(); ++ iter) {
std::cout<<*iter<<"\n";
}
}
int main(int argc, char **argv)
{
int a[] = {1,3,5,7,9,0,8,6,4,2};
int len = sizeof(a) / sizeof(a[0]);
std::vector<int> c(10);
print(c);
std::vector<int> c1(10, 5);
print(c1);
std::vector<int> c2(&a[0], &a[len]);
print(c2);
std::vector<int> c3(&c1[1], &c1[3]);
print(c3);
std::cout<<"c.size = "<<c.size()<<std::endl;
std::cout<<"c2.size = "<<c2.size()<<std::endl;
c2.swap(c);
std::cout<<"c2 ";
print(c2);
std::cout<<"c ";
print(c);
std::swap(c2,c);
std::cout<<"c2 ";
print(c2);
std::cout<<"c ";
print(c);
c.insert(c.begin()+1, 120);
c.insert(c.end()-1, 100);
std::cout<<"c ";
print(c);
c.insert(c.end(), 10, 69);
std::cout<<"c ";
print(c);
c.push_back(700);
std::cout<<"c ";
print(c);
c.pop_back();
std::cout<<"c ";
print(c);
c.erase(c.begin());
std::cout<<"c ";
print(c);
//delete several elements
c.erase(c.begin() + 1, c.end() - 3);
std::cout<<"c ";
print(c);
//resize default 0
c.resize(10);
std::cout<<"c ";
print(c);
//resize defalut 5
c.resize(20, 5);
std::cout<<"c ";
print(c);
//reverse print
c.resize(20, 5);
std::cout<<"reverse c ";
print_reverse(c);
c.reserve(120);
std::cout<<"c ";
print(c);
std::cout<<"c.size = "<<c.size()<<std::endl;
std::cout<<"c.max_size = "<<c.max_size()<<std::endl;
std::cout<<"c.capacity = "<<c.capacity()<<std::endl;
if (c.empty()) {
std::cout<<"Yes, C is empty."<<std::endl;
} else {
std::cout<<"No, C is not empty."<<std::endl;
}
c.clear();
if (c.empty()) {
std::cout<<"Yes, C is empty."<<std::endl;
} else {
std::cout<<"No, C is not empty."<<std::endl;
}
return 0;
}
2、deque
#include <iostream>
#include <deque>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char **argv)
{
//create empty deque of strings
deque<string> coll;
//insert several elements
coll.assign(3, string("string"));
coll.push_back("last string");
coll.push_front("first string");
//print elements separated by newlines
copy(coll.begin(), coll.end(), ostream_iterator<string>(cout, "\n"));
cout<<endl;
//remove the first and last element
coll.pop_front();
coll.pop_back();
//print elements separated by newlines
copy(coll.begin(), coll.end(), ostream_iterator<string>(cout, "\n"));
cout<<endl;
//insert "anthor" into every element but the first
for (int i = 1; i < coll.size(); ++ i) {
coll[i] = "anthor " + coll[i];
}
//change size to four element
coll.resize(4, "resized sring");
//print elements separated by newlines
copy(coll.begin(), coll.end(), ostream_iterator<string>(cout, "\n"));
cout<<endl;
return 0;
}
3、list
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
void printLists(const list<int> &list1, const list<int> &list2)
{
cout<<"list1 : ";
copy(list1.begin(), list1.end(), ostream_iterator<int>(cout, " "));
cout<<"list2 : ";
copy(list2.begin(), list2.end(), ostream_iterator<int>(cout, " "));
cout<<endl<<endl;
}
int main(int argc, char **argv)
{
//create two empty lists
list<int> list1, list2;
//fill boths lists with elements
for (int i= 0 ; i < 6; ++ i) {
list1.push_back(i);
list2.push_back(i);
}
printLists(list1, list2);
//move first element to the end
list2.splice(list2.end(), list2, list2.begin());
printLists(list1, list2);
//sort second list, assign to list1 and remove duplicates
list2.sort();
list1 = list2;
list2.unique();
printLists(list1, list2);
//list1 merge with list2
//then list2 is empty
list1.merge(list2);
printLists(list1, list2);
list1.reverse();
printLists(list1, list2);
//filter repeate element, but first we need to sort
list1.unique();
printLists(list1, list2);
return 0;
}
4、set
#include <iostream>
#include <set>
using namespace std;
int main(int argc, char **argv)
{
//type of the collection : sets
//no duplicates
//elements are integral values
// descending order
typedef set<int, greater<int> > IntSet;
//empty set container
IntSet coll;
//insert elements in random order
coll.insert(4);
coll.insert(3);
coll.insert(5);
coll.insert(1);
coll.insert(6);
coll.insert(2);
coll.insert(5);
//iterate over all elements and print them
IntSet::iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++ pos) {
cout<< *pos<<"\n";
}
//insert 4 again and process return value
pair<IntSet::iterator, bool> status = coll.insert(4);
if (status.second) {
cout<<"4 inserted as element "
<<distance(coll.begin(), status.first) + 1
<<endl;
} else {
cout<<"4 already exists"<<endl;
}
copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
cout<<endl;
//assign elements to anthor set with ascending order
//set<int> coll2(coll.begin(), coll.end());
//copy(coll2.begin(), coll2.end(), ostream_iterator<int>(cout, " "));
//remove all the elements up to element with 3
coll.erase(coll.begin(), coll.find(3));
copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
cout<<endl;
//remove all elements with value 5
int num = coll.erase(2);
cout<<num<<" elements removed "<<endl;
copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
cout<<endl;
cout<<endl;
return 0;
}
5、map
①为了避免隐式型别转换,可以用value_type传递正确型别。
std::map<std::string, float> coll;
coll.insert(std::map<std::string, float>::value_type("otto", 22.3));
②应用pair<>
std::map<std::string, float> coll;
coll.insert(std::pair<std::string, float>("otto", 22.3));
coll.insert(std::pair<const std::string, float>("otto", 22.3));
③应用make_pair()
std::map<std::string, float> coll;
coll.insert(std::make_pair("otto", 22.3));
std::map<std::string, float> coll;
if (coll.insert(std::make_pair("otto", 22.3)).second) {
std::cout<<"OK, could insert otto/22.3"<<std::endl;
} else {
std::cout<<"Oops, could not insert otto/22.3"
<<"(lkey otto already exists)"<<std::endl;
}
StringFloatMap coll;
StringFloatMap::iterator pos = coll.find(key);
if (pos != coll.end()) {
coll.erase(pos);
}
///
typedef std::map<float, std::string, std::greater<float> > coll;
typedef std::map<std::string, float, std::greater<std::string> > StringFloatMap;
///
typedef std::map<std::string, float> StringFloatMap;
StringFloatMap coll;
StringFloatMap::iterator pos;
//...
//remove all elements having a certain value
for (pos = coll.begin(); pos != coll.end(); ++ pos) {
if (pos->second == value) {
coll.erase(pos ++);
} else {
++ pos;
}
}