Chinaunix首页 | 论坛 | 博客
  • 博客访问: 142690
  • 博文数量: 58
  • 博客积分: 1584
  • 博客等级: 上尉
  • 技术积分: 605
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-12 10:06
文章分类

全部博文(58)

文章存档

2011年(7)

2010年(51)

我的朋友

分类: C/C++

2010-09-15 23:13:05

* 容器的迭代器还有几种: 
   + iterator:正常迭代器(常用) 
   + reverse_iterator:反向迭代器(有时也用) 
- rbegin(),rend()//返回反向迭代器 
   + const_iterator:常量迭代器 
   + const_reverse_iterator: 

C++代码 
  1. iterator find(数据){  
  2.     for( 从beg;!=end;it ++)  
  3.         if(*it==数据)  
  4.             return it;  
  5.     return end;//未找到,返回无效迭代器  
  6. }//查询  
  7. *it = new_data;//更新迭代器  

-------------------------------- 
所有的容器在插入数据时会自动加长,程序员不必关心空间问题. 
容器的分类: 
  + (sequence) 
- vector 
- list 
- deque 
序列式容器共性 
构造函数:constructor(int n);constructor(int n,T val) 
调整大小:resize(int n),resize(int n,T val)一般用来加长,不一定能缩短 
赋值:assign(n,val),放入n个相同的值 
     assign(区间(beg--end),val),把指定区间的内容放入容器 
插入:insert(pos/*迭代器*/,n,val),insert(pos,区间) 
毛插:push_back(val);//在末尾插入新数据 
取得首尾元素:front(),back() 
尾删:pop_back();//删除尾部元素 
----------------------------------- 
序列式容器不同点 
vector 类似于数组,也支持'[]',不做越界检查,但更安全;能自动增长. 
vector.at(下标);//如果越界会throw异常,返回的是引用 
vector.reserve(n)//保留空间(没必要用) 
vector.capacity()//目前容量(没必要用),用size() 
vector只适合在末尾插入和删除数据 
vector的查找效率高而list低 
list对于频繁的插入与删除做了优化. 
凡是标准库抛出的异常都可以用exception类来catch 
C++代码 
  1. //vector例子  
  2. #include   
  3. using namespace std;  
  4. #include   
  5.   
  6. int main()  
  7. {  
  8.         vector<int> vi;  
  9.         cout << "Input scores,end by -1:";  
  10.         int s;  
  11.         int m=0;  
  12.         for(;;){  
  13.                 cin >> s;  
  14.                 if(s==-1) break;  
  15.                 vi.push_back(s);  
  16.                 if(s>m)  
  17.                         m = s;  
  18.         }  
  19.         int inc = 100-m;  
  20.         for(int i=0; i
  21.                 vi[i] += inc;  
  22.         for(int i=0; i
  23.                 cout << vi[i] << ' ';  
  24.         cout << endl;  
  25.         try{  
  26.         cout << "vi[1000]=" << vi[1000] << endl;  
  27.         cout << "vi.at(1000)=" << vi.at(1000) << endl;  
  28.         }catch(exception& e){  
  29.                 cout << e.what() << endl;  
  30.         }  
  31. }  

------------------------------------ 
list支持push_front(),push_back(),pop_front(),pop_back() 
list不支持'[]'和at(),因为它不是连续存放的. 
标准模板库里比大小都用<,比等于用== 
C++代码 
  1. c1.splice(pos,c2)//转移,就是挪走了  
  2. c1.splice(pos,c2,it)//把c2中it位置的元素转移到c1的pos处  
  3. c1.splice(pos,c2,区间)//把c2里的区间转移到c1的pos  
  4. c1.merge(c2)//自动归并排序到合适的位置  
  5. remove(val)//删除指定值的元素  

C++代码 
  1. //list.cc  
  2. #include   
  3. using namespace std;  
  4. #include   
  5.   
  6. int main()  
  7. {  
  8.         int cpp[5] = {3,5,6,8,9};  
  9.         int java[8] = {1,2,3,4,5,6,7,8};  
  10.         int Unix[4] = {3,4,5,6};  
  11.         list<int> li;  
  12.         li.insert(li.begin(),cpp,cpp+5);  
  13.         li.insert(li.begin(),java,java+8);  
  14.         li.insert(li.begin(),unix,unix+4);  
  15.         li.sort();//排序  
  16.         li.unique();//删除相邻的重复的元素,只留一份  
  17.         list<int>::iterator it = li.begin();  
  18.         while(it!=li.end())  
  19.                 cout << *it++ << ' ';  
  20.         cout << endl;  
  21. }  

--------------------------------- 
C++代码 
  1. deque []  .at()  
  2. deque.push_front(val);  
  3. deque.pop_front();  

--------------------------------- 
  + 关联式 
关联式迭代器的共性: 
- 插入不用指定位置:insert(val),会自动排序 
- 查找一个指定的数据:find(val)返回指向元素的迭代器 
  如未找到会返回end()无效迭代器 
multimap,multiset有一个统计函数:count(val)//返回val的个数 
以下两个函数只对允许重复才会有意义 
ib = lower_bound(val)//返回val在容器中的第一个位置 
ie = upper_bound(val)//返回val在容器中的最后一个位置的下一个位置 
while(ib!=ie) 
cout << *ib++; 
还有一些人喜欢另一种方式:equal_range(val)//返回一个pair<迭,迭> 

重复   元素    不同点 
----------------------------------------------------- 
map   N    pair    支持[key] 
multimap   Y    pair      N 
set   N key     N 
multiset   Y key     N 
----------------------------------------------------- 
C++代码 
  1. //map.cc  
  2. #include   
  3. #include   
  4. using namespace std;  
  5. #include   
  6.   
  7. int main()  
  8. {  
  9.     map<int, string> m;  
  10.     m.insert(make_pair(1001,"李明"));  
  11.     m.insert(make_pair(1002,"王国"));  
  12.     m.insert(make_pair(1005,"失小"));  
  13.     m[1006] = "刘华";  
  14.     m.insert(make_pair(1001,"娜娜"));  
  15.     map::iterator it;  
  16.     it = m.begin();  
  17.     while(it!=it.end()){  
  18.         cout << it->first << ':' << it->second << endl;  
  19.         ++ it;//会比it++性能高  
  20.     }  
  21. }  
  22.   
  23. 对于set,map来说重复的插入被忽略.  
  24.   
  25. //multimap  
  26. //一切操作都是对key而言的  
  27. //key一定要支持小于运算符,不然是不能进行排序的  
  28. #include   
  29. using namespace std;  
  30. #include   
  31. #include   
  32.   
  33. int main()  
  34. {  
  35.         typedef multimap M;  
  36.         M mss;  
  37.         M::iterator ib,ie;  
  38.         mss.insert(make_pair("a","b"));  
  39.         mss.insert(make_pair("c","d"));  
  40.         mss.insert(make_pair("e","f"));  
  41.         mss.insert(make_pair("c","p"));  
  42.         mss.insert(make_pair("a","m"));  
  43.         mss.insert(make_pair("c","d"));  
  44.         mss.insert(make_pair("a","p"));  
  45.         ib = mss.begin();  
  46.         ie = mss.end();  
  47.         while(ib!=ie){  
  48.                 cout << ib->first <<',' << ib->second << endl;  
  49.                 ++ ib;  
  50.         }//end while  
  51.         cout << "a count : " << mss.count("a") << endl;  
  52.         cout << "c count : " << mss.count("c") << endl;  
  53.         ib = mss.lower_bound("c");  
  54.         ie = mss.upper_bound("c");  
  55.         while(ib!=ie){  
  56.                 cout << ib->first <<',' << ib->second << endl;  
  57.                 ++ ib;  
  58.         }//end while  
  59.   
  60. }//end main  
  61.   
  62. //set  
  63. //同样的插入同样被忽略  
  64. #include   
  65. using namespace std;  
  66. #include   
  67.   
  68. int main()  
  69. {  
  70.         set<int> si;  
  71.         int userid[5] = {1,3,3,4,5};  
  72.         for(int i=0;i<5; i++)  
  73.                 si.insert(userid[i]);  
  74.         set<int>::iterator it;  
  75.         it = si.begin();  
  76.         while(it!=si.end())  
  77.                 cout << *it++ << ' ';  
  78.         cout << endl;  
  79.         cout << "user 3 : " << (si.find(3)!=si.end()) << endl;  
  80.         cout << "user 9 : " << (si.find(9)!=si.end()) << endl;  
  81. }  
  82.   
  83. //multiset  
  84. //用的是平衡二叉树,所以它的查找效率是相当高的.  
  85. #include   
  86. using namespace std;  
  87. #include   
  88.   
  89. template<typename Iter>  
  90. void show(Iter ib, Iter ie)  
  91. {  
  92.         while(ib!=ie)  
  93.                 cout << *ib++ << ' ';  
  94.         cout << endl;  
  95. }  
  96. int main()  
  97. {  
  98.         int a[5] = {5,1,7,5,1};  
  99.         multiset<int> pids(a,a+5);  
  100.         show(pids.begin(),pids.end());  
  101.         pids.insert(7);  
  102.         pids.insert(7);  
  103.         pids.insert(7);  
  104.         pids.erase(pids.find(5));  
  105.         show(pids.begin(),pids.end());  
  106.         cout << "end process 7..." << endl;  
  107.         pids.erase(7);  
  108.         show(pids.begin(),pids.end());  
  109. }//end main  

================================================= 
* STL Algorithms 

  + 几乎所用的算法都工作在某个区间 
  + Search:for_each(beg,end,fun),find(beg,end,data),find_first_of 

(),find_end()... 
  + Sort:sort(),reverse()... 
  + Copy:copy(),copy_backward()... 
  + Modify:replace(beg,end,oldv,newv),merge(),remove()/*假删除,与erase() 

一起用来实现真正的删除*/,c.erase(remove(...),c.end());//都这么用 
  + Numeric:min(,max(),count(),swap(),accumulate() 


容器适配器 
  + stack<类型>(push,pop,size,empty,clear,top) 
  + queue<类型>(push,pop,size,empty,clear,front,back) 
  + priority_queue<类型>(优先队列) 
阅读(1662) | 评论(1) | 转发(0) |
0

上一篇:C++连接数据库

下一篇:vector使用范例

给主人留下些什么吧!~~

chinaunix网友2010-09-19 09:44:17

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com