C++学习-STL容器
2017-09-11 20:16
393 查看
STL中的vector:
/* vector --->向量 ---->线性容器 用标准模板,记得加上相应的头文件 */ #include <iostream> #include <vector> using namespace std; int main() { //向量容器 vector <int> v0; //初始化值 vector <int> v1{ 1,2,3,4,5,9,6 }; //设置向量容量 //初始化 v1[0] = 8; v1[1] = 8; v1[2] = 8; //声明迭代器 标准他属于那个模板 vector<int>::iterator v1_Iter; for (v1_Iter = v1.begin(); v1_Iter < v1.end(); v1_Iter++) { cout <<" "<< *v1_Iter; } cout << endl; //逆序迭代器 vector<int>::reverse_iterator v1_rIter; for (v1_rIter = v1.rbegin(); v1_rIter < v1.rend(); v1_rIter++) { cout << " " << *v1_rIter; } //新标准 cout << endl; for (int &i : v1) { cout << " " << i; } system("pause"); return 0; }
STL中的stack:
#include <iostream> #include<stack> #include <string> using namespace std; int main() { //stack 先进后出 stack<string> mystack; mystack.push("饭"); mystack.push("要吃"); mystack.push("我"); cout << mystack.size(); cout << endl; while (!mystack.empty()) { cout << mystack.top(); mystack.pop(); } cout << endl; system("pause"); return 0; }
STL中的queue:
#include <iostream> #include<queue> #include <list> #include <string> using namespace std; int main() { queue<string> qs; //嵌套 list<string> Ls; Ls.push_back("12123"); queue<list<string>> QS; QS.push(Ls); qs.push("我"); qs.push("想"); qs.push("你"); while (!qs.empty()) { cout << qs.front(); qs.pop(); } cout << endl; system("pause"); return 0; }
STL中的list:
#include<iostream> #include <list> //俗称 列表 using namespace std; int main() { list<int> L1; L1.push_back(3); L1.push_back(2); L1.push_back(1); for (int &v : L1) { cout << " " << v; } cout << endl; L1.push_front(4); list<int>::iterator L1_Iter; //-----> p->next!= NULL for (L1_Iter = L1.begin(); L1_Iter!=L1.end(); L1_Iter++) { cout << " " << *L1_Iter; } system("pause"); //防止闪屏 return 0; }
STL中的set:
#include <iostream> #include<set> #include <string> using namespace std; int main() { /*set <int> si;*/ multiset <int> si; //没有重复出现的 si.insert(1); si.insert(2); si.insert(2); si.insert(4); for (set<int>::const_iterator it = si.begin(); it != si.end(); it++) { cout << " " << *it; } cout << endl; set<string> bad_ip; bad_ip.insert("192.168.1.1"); bad_ip.insert("192.168.1.10"); string my_ip = "192.168.1.1"; //count使用 if (bad_ip.count(my_ip) != 0) { cout << "该ip属于黑名单" << endl; } system("pause"); return 0; }
STL中的map:
#include<iostream> #include <string> #include <map> using namespace std; int main() { //初始化方式 //----->字典 //first:键 second:值 //map<string,int> msi //pair:数对类型 //结构体+类,显式实例化 //msi.insert(pair<string, int>("张飞", 97)); ////make_pair() 函数隐式 //msi.insert(make_pair("赵云", 78)); //下标的方式进行访问 //msi["a"] = 100; //msi["A"] = 300; //msi["f"] = 40; //msi["d"] = 500; //msi["e"] = 1000; //map :1.注意:键唯一 // 2.有序,排序 大写大于小写,其他按字母 "A">"a" //typedef map<string, int>::iterator IT; //for (IT it = msi.begin(); it != msi.end(); ++it) //{ // cout << "名字:" << it->first << "\t战力:" << it->second<<endl; //} multimap<string, int> msi; msi.insert(make_pair("ZF", 95)); msi.insert(make_pair("ZL", 95)); typedef multimap<string, int>::iterator IT; for (IT it = msi.begin(); it != msi.end(); ++it) { cout << "名字:" << it->first << "\t战力:" << it->second<<endl; } system("pause"); return 0; }
相关文章推荐
- C++ STL学习笔记九 map映照容器
- [C++再学习系列] STL容器删除操作总结
- 【C++ STL学习之一】容器的共通能力和共通操作总结
- 【C++ STL学习之四】容器list深入学习
- C++ STL容器学习(一)
- C++ STL学习笔记十二 hash_map映照容器
- C++ 中STL学习-50条有效使用经验学习笔记(一):容器
- C++ STL学习笔记十二 hash_map映照容器
- 【C++ STL学习之一】容器的共通能力和共通操作总结
- C++ 学习笔记:STL 容器一些底层机制
- C++学习 STL之七:STL各种容器的使用时机详解
- 初学者学习C++STL之vector容器
- C++学习STL之关联容器 --- pair、map、set
- C++ STL容器的学习使用(vector、queue、list、set、map)
- C++ STL学习容器deque深入学习
- [C++再学习系列] STL容器删除操作总结
- C/C++语言函数学习(3)STL中map容器
- 【C++ STL学习之一】容器的共通能力和共通操作总结
- C++ STL容器学习总结
- 【学习C++】标准C++中的STL容器类简介