STL(vector, map, stack)

来源:互联网 发布:勇士vs国王季前赛数据 编辑:程序博客网 时间:2024/06/06 17:19

Standard Template Library: 容器、迭代器、算法


***容器***
顺序容器:vector、deque、list
vector 从后面快速插入删除,直接访问任何元素
deque 从前面或后面快速的插入删除,直接访问任何元素
list 双链表,从任何地方快速插入删除

关联容器:map、set、multiset、multimap
是一种非线性的树结构,一种比较高效的特殊的平衡检索二叉树--红黑树。

容器适配器:stack、queue、priority_queue
是容器的接口,本身不能直接保存元素,其保存元素的机制是调用另一种顺序容器来实现。


***迭代器***:iterator

对容器中的对象进行访问,如同一个指针。
eg. vector<int>::iterator iter;
eg. list<string>::iterator iter;
eg. 使用迭代器遍历容器
for(vector<string>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
{
std::cout<< *iter <<std::endl;
}


***算法***
头文件 algorithm



代码1:vector.cpp

  1 /*  2  * File: vector.cpp  3  * ----------------  4  * DATE: 20170817  5  *  6  */  7   8 #include <iostream>  9 #include <vector> 10 #include <algorithm> 11  12 void display(std::vector<int> &vec) 13 { 14         //迭代器 15         for(std::vector<int>::iterator iter = vec.begin(); iter != vec.end(); iter++) 16         { 17                 std::cout<< *iter <<std::endl; 18         }        19 }        20  21 //谓词 大于60 22 bool operator_biger60(int a) 23 { 24         return a > 60; 25 }        26  27 //谓词 降序排列 28 bool sort_bigger(int a,int b) 29 { 30         return a > b; 31 }        32  33 int main() 34 { 35         int array[] = {50,65,80,92,93,53,78}; 36         std::vector<int> vec(array,array+6);    //容器 vector<int> 37         std::cout<< "before sort:" <<std::endl;  38          39         display(vec); 40          41         //谓词 42         std::cout<< "大于60个数:" << count_if(vec.begin(),vec.end(),operator_biger60) <<std::endl; 43          44         //排序 谓词 45         sort(vec.begin(),vec.end(),sort_bigger); 46         std::cout<< "after sort:" <<std::endl; 47         display(vec); 48  49         //删除 50         for(std::vector<int>::iterator iter = vec.begin();iter != vec.end();iter++) 51         { 52                 if(*iter < 60) 53                 { 54                         vec.erase(iter);        //容器长度的改变将使迭代器失效 55                         iter = vec.begin();     //重新赋值 迭代器 56                 } 57         } 58         std::cout<< "after delete:" <<std::endl; 59         display(vec); 60         return 0; 61 }


代码2:map.cpp

  1 /*  2  * FILE: map.cpp  3  * -------------  4  * DATE: 20170817  5  *  6  */  7   8 #include <iostream>  9 #include <map> 10 #include <string> 11 #include <algorithm> 12  13 //map<key,value> 14 //迭代器 map<int,string>::iterator iter 15 void display(std::map<int,std::string> &m) 16 { 17         for(std::map<int,std::string>::iterator iter = m.begin();iter!=m.end();iter++) 18         { 19                 std::cout<< iter->first << "," << iter->second <<std::endl; 20         } 21 } 22  23 int main() 24 { 25         std::map<int,std::string> m; 26         m.insert(std::make_pair(1000,"张三"));  //使用make_pair 添加元素 27         m.insert(std::pair<int,std::string>(1001,"李四"));      //使用pair<int,string>( , ) 添加元素 28         m[1002]="王五"; //使用下标添加元素 29         std::cout<< "before: " <<std::endl; 30         display(m); 31  32         //若存在,则修改 33         if(m.count(1002))       //count 计数 34         { 35                 m[1002] = "麻五"; 36         } 37         std::cout<< "after: " <<std::endl; 38         display(m); 39  40         return 0; 41 }


代码3:stack.cpp

  1 /*  2  * FILE: stack.cpp  3  * ---------------  4  * DATE:2017081e  5  *  6  */  7   8 #include <iostream>  9 #include <stack> 10 #include <string> 11 #include <list> 12  13 int main() 14 { 15         /* stack 栈_后进先出 */ 16  17         //栈 默认基于set实现 18         std::stack<int> num;    //定义 stack<int> num 19  20         //修改 栈的基础容器,改为基于 list实现 21         //stack<string, list<string> >  注意后面需要空格 22         std::stack<std::string,std::list<std::string> >name;    //定义 stack<string,list<string> > name 23         num.push(10);   //push()向栈中压入元素 24         num.push(20); 25         num.push(30); 26  27         name.push("张三");      //push()向栈中压入元素 28         name.push("李四"); 29         name.push("马五"); 30  31         std::cout<< "num size:" << num.size() <<std::endl;      //size()栈中元素的个数 32         while(!num.empty())     //empty()栈是否为空 33         { 34                 std::cout<< num.top() <<std::endl;      //top()栈的顶部元素,即最后压入的元素 35                 num.pop();      //pop()弹出 36         } 37  38         std::cout<< "name size:" << name.size() <<std::endl; 39         while(!name.empty()) 40         { 41                 std::cout<< name.top() <<std::endl; 42                 name.pop(); 43         } 44  45         return 0; 46 }


阅读全文
0 0
原创粉丝点击