本文共 9946 字,大约阅读时间需要 33 分钟。
示例:
1 #include2 #include 3 #include 4 #include
5 6 using namespace std; 7 int main() 8 { 9 vector a;10 list b;11 deque c;12 13 vector ::size_type a1;14 vector ::iterator a2;15 vector ::const_iterator a3;16 vector ::reverse_iterator a4;17 vector ::const_reverse_iterator a5;18 vector ::difference_type a6;19 vector ::value_type a7;20 vector ::reference a8;21 vector ::const_reference a9;22 23 list ::size_type b1;24 list ::iterator b2;25 list ::const_iterator b3;26 list ::reverse_iterator b4;27 list ::reverse_iterator b4;28 list ::const_reverse_iterator b5;29 list ::difference_type b6;30 list ::value_type b7;31 list ::reference b8;32 list ::const_reference b9;33 34 deque ::size_type c1;35 deque ::iterator c2;36 deque ::const_iterator c3;37 deque ::reverse_iterator c4;38 deque ::const_reverse_iterator c5;39 deque ::difference_type c6;40 deque ::value_type c7;41 deque ::reference c8;42 deque ::const_reference c9;43 44 a.push_back(10);45 a.push_back(20);46 a.push_back(30);47 a.push_back(40);48 for(vector ::size_type i=0; i
二、顺序容器的拆入操作:
1、容器元素中拆入的都是副本,不会影响原数据;
2、插入操作可能会使得迭代器失效;
3、避免存储end操作返回的迭代器;
迭代器失效示例代码: 1 vector ivec; 2 ivec.push_back(10): 3 ivec.push_back(20): 4 ivec.push_back(30): 5 ivec.push_back(40): 6 7 vector ::iterator first = ivec.begin()++; 8 vector ::iterator last= ivec.end(); 9 10 while(first != last){ //error, ok:while(first != ivec.end(){ 11 first = ivec.insert(first, 42);12 ++first;13 }
顺序容器拆入操作示例代码:
#include#include #include //链表#include
//双端数组#include using namespace std;int main(){ vector svec; list slist; deque sdeque; //末端拆入 svec.push_back("Bill"); svec.push_back("Tom"); svec.push_back("Mary"); slist.push_back("Bill"); slist.push_back("Tom"); slist.push_back("Mary"); sdeque.push_back("Bill"); sdeque.push_back("Tom"); sdeque.push_back("Mary"); //前端拆入,向量vector没有push_front slist.push_front("111"); slist.push_front("222"); slist.push_front("333"); sdeque.push_front("111"); sdeque.push_front("222"); sdeque.push_front("333"); for(std::vector ::iterator it=svec.begin(); it!=svec.end(); ++it){ cout << *it <<" "; } cout << std::endl; std::vector ::iterator it=svec.begin(); it++; svec.insert(it, "Hello"); svec.insert(it, 10, "Hello"); for(std::vector ::iterator it=svec.begin(); it!=svec.end(); ++it){ cout << *it <<" "; } cout << std::endl; string arr_str[] = { "hello", "8999", "888", "3444"}; it=svec.begin(); it++; svec.insert(it, arr_str+1, arr_str+3); for(std::vector ::iterator it=svec.begin(); it!=svec.end(); ++it){ cout << *it <<" "; } cout << std::endl;、 return 0;}
1、比较的容器必须具有相同的容器类型;
2、容器的比较是基于容器内元素的比较;
3、容器内元素必须有相应的关系运算符;(对象的话,必须在类中实现>, < 等符号的重载);
2 #include3 #include 4 5 class Dog 6 { 7 //成员省略 8 //必须>, >=, <, <=, ==, != 9 };10 11 int main()12 {13 vector ivec1;14 vector ivec2;15 vector ivec3;16 vector ivec4;17 vector ivec5;18 19 ivec1.push_back(1);20 ivec1.push_back(3);21 ivec1.push_back(5);22 ivec1.push_back(7);23 ivec1.push_back(9);24 ivec1.push_back(12);25 26 ivec1.push_back(0);27 ivec1.push_back(2);28 ivec1.push_back(4);29 ivec1.push_back(6);30 ivec1.push_back(8);31 ivec1.push_back(11);32 33 /* 同类型之间进行比较34 * 两个向量比较大小,如果向量中每个数都相等(个数要相等),这个两个向量才相等,否则不等;则从第一个依次开始比较,遇到不等则区分大小;35 */36 if(ivec1 > ivec2)37 {38 cout << "ivec1 大!"<< endl;39 }40 ivec4.push_back(1);41 ivec4.push_back(3);42 ivec4.push_back(5);43 ivec1.push_back(7);44 45 vector dogA;46 vector dogB; //对象中必须重载关系运算符47 dogA.push()48 49 return 0;50 }
1、c.size();
2、c.max_size();
3、c.empty();
4、c.resize(n);
5、c.resize(n, t);
注意:resize操作可能会使迭代器失效
2 #include3 #include 4 #include 5 #include
6 7 using namespace std; 8 int main() 9 { 10 list ilist; 11 ilist.push_back(10); 12 ilist.push_back(20); 13 ilist.push_back(30); 14 15 cout <<"容器里数据的个数:" << ilist.size(); 16 17 list ::size_type count = ilist.size(); 18 cout <<"容器里数据的个数:" << ilist.size(); 19 20 cout <<"容器的max_size:" << ilist.max_size(); 21 if(ilist.empty()){ 22 cout << "容器是空的" ; 23 }else{ 24 cout << "容器是空的" ; 25 } 26 27 ilist.resize(10); //增加了元素默认值为0; 28 for(list ::iterator it=ilist.begin(); it!=ilist.end();++it){ 29 cout << *it << std::endl; 30 } 31 ilist.resize(20, -1); //新增的值为-1 32 for(list ::iterator it=ilist.begin(); it!=ilist.end();++it){ 33 cout << *it << std::endl; 34 } 35 36 //ilist.resize(0); //清空容器37 ilist.resize(10); //缩小容器,删除了元素;38 for(list ::iterator it=ilist.begin(); it!=ilist.end();++it){39 cout << *it << std::endl;40 }41 return 0;42 }
五、访问容器中的元素
c.back();
c.front();
c[n]; //仅适用于vector, deque
c.at(n); //仅适用于vector, deque
1 #include2 #include
3 #include 4 #include //捕获异常时候,必须包含的文件 5 6 using namespace std; 7 int main() 8 { 9 vector ivec;10 ivec.push_back(10);11 ivec.push_back(20);12 ivec.push_back(30);13 14 cout << ivec.front() << endl;15 cout << ivec.back() << endl;16 vector ::reference a = ivec.front();17 vector ::reference b = ivec.back(); //返回值为容器内元素的引用类型 18 19 cout << a << endl;20 cout << b << endl;21 22 cout << *ivec.begin() << endl; //通过迭代器获取数据23 b = *ivec.begin(); //begin()返回值为迭代器,即指针 24 cout << b << endl;25 26 cout << ivec[0] <
删除元素:
c.erase(p);
c.erase(b, e); //删除区间为左闭右开区间,即b删除,e不删除
c.clear();
c.pop_back(); //从后面取走元素
c.pop_front(); //从前面取走元素,不适用于vector,list,deque容器适用;
赋值与交换
c1 = c2 //将容器c2中的数据拷贝到c1容器中:
c1.swap(c2) //将容器c2中的数据相互交换c1的数据
c.assign(b, e) //赋值
c.assign(n, t) //赋值
使用assign:类型兼容即可
使用swap: 类型必须相同
2 #include3 #include 4 #include
5 #include 6 #include 7 8 using namespace std; 9 int main()10 {11 vector a;12 vector b;13 vector c;14 vector svec;15 list slist;16 17 a.push_back(10);18 a.push_back(20);19 a.push_back(30);20 a.push_back(40);21 22 b.push_back(100);23 b.push_back(200);24 b.push_back(300);25 26 c.push_back(1000);27 c.push_back(2000);28 c.push_back(3000);29 c.push_back(4000);30 c.push_back(5000);31 c.push_back(6000);32 33 34 a.swap(b);35 for(vector ::iterator it = a.begin(); it!=a.end();it++){36 cout << *it << endl;37 }38 39 a = b;40 for(vector ::iterator it = a.begin(); it!=a.end();it++){41 cout << *it << endl;42 }43 44 vector ::iterator f = c.begin();45 vector ::iterator e = c.end();46 f++;47 f++;48 e--;49 50 a.assign(f, e);51 for(vector ::iterator it = a.begin(); it!=a.end();it++){52 cout << *it << endl;53 }54 55 svec.push_back("apple");56 svec.push_back("big");57 svec.push_back("cat");58 59 slist.push_back("c");60 slist.push_back("c++");61 slist.push_back("java");62 slist.push_back("c#");63 64 slist.assign(svec.begin(), svec.end()); //将字符指针转成string65 for(list ::iterator it1 = slist.begin(); it1!= slist.end();it1++){66 cout << *it1<< endl;67 }68 slist.assign(10, "APP"); //将字符指针转成string69 for(list ::iterator it1 = slist.begin(); it1!= slist.end();it1++){70 cout << *it1<< endl;71 }72 return 0;73 }
转载地址:http://umcpa.baihongyu.com/