C++常见的stl容器与相关操作 示例解析
目录
- sort排序
- vector
- map
- unordered_map
- set
- queue
- stack
- 创建容器时指定排序规则
sort排序
针对含有迭代器的容器,可以用#include<algorithm>
中的sort函数进行排序。
默认排序是从小到大,可以自己写仿函数,也可以用greater<int>()
或者less<int>()
。
#include <vector> #include <algorithm> #include <iostream> using namespace std; bool compfunc(const int &a, const int &b) { return a > b; } struct compstru { bool operator()(int a, int b) { return a > b; } }; int main() { vector<int> myVec1 = {1, 4, 9, 2}; sort(myVec1.begin(), myVec1.end(), compstru()); sort(myVec1.begin(), myVec1.end(), compfunc); sort(myVec1.begin(), myVec1.end()); sort(myVec1.begin(), myVec1.end(), less<int>()); sort(myVec1.begin(), myVec1.end(), greater<int>()); }
vector
void printVec(const vector<int> &vec) { for (auto i : vec) { cout << i << " "; } cout << endl; } int main() { vector<int> myVec1 = {1, 4, 9, 2}; reverse(myVec1.begin(), myVec1.end()); // 反转vector printVec(myVec1); if(find(myVec1.begin(), myVec1.end(), 4)!=myVec1.end()){ //查找4是不是在vector里面 cout<<"找到了"<<endl; } int row = 5; int col = 10; vector<vector<int>> myVec2(row, vector<int>(col, 0)); // 初始化全0的二维数组 myVec1.push_back(10); myVec1.pop_back(); }
map
红黑树实现是有序容器,按照key值从小到大排序,插入pair<type1,type2>(data1,data2)
void printMap(const map<int, char> &myMap) { for (auto it : myMap) { cout << it.second << " "; } cout << endl; } int main() { map<int, char> myMap = {{3, 'c'}, {2, 'b'}, {1, 'a'}}; printMap(myMap); // a b c myMap.insert({4, 'd'}); printMap(myMap); // a b c d myMap.insert(pair<int, char>(0, 'e')); // e a b c d printMap(myMap); myMap[6] = 'g'; //有覆盖数据的危险 printMap(myMap); // e a b c d g cout << myMap.count(3) << endl; // map的查找,返回1或0 auto it = myMap.find(3); cout << it->second << endl; // c if (myMap.find(3) != myMap.end()) { cout << myMap[3] << endl; // c } }
unordered_map
无序容器,操作和map类似
新增元素传送门
有insert和emplace
void printMap(const unordered_map<int, char> &myMap) { for (auto it : myMap) { cout << it.second << " "; } cout << endl; } int main() { unordered_map<int,char> myUnorderMap={{1,'a'},{2,'b'},{3,'d'}}; printMap(myUnorderMap); myUnorderMap.emplace(4,'e'); myUnorderMap.insert({5,'d'}); printMap(myUnorderMap); for(auto it=myUnorderMap.begin();it!=myUnorderMap.end();){ if(it->first==1){ myUnorderMap.erase(it++); //删除要使用it++,避免迭代器实效 } else{ it++; //迭代器递增 } } printMap(myUnorderMap); }
set
有序容器,会自动排序,默认从小到大
void printSet(const set<int> &mySet) { for (auto it = mySet.begin(); it != mySet.end(); it++) { cout << *it << " "; } cout << endl; } int main() { set<int> mySet = {1, 1, 2, 3}; printSet(mySet); mySet.insert(0); printSet(mySet); }
queue
队列
int main() { queue<int> myQue; //默认使用deque作容器适配器 for (int i = 0; i < 3; i++) { myQue.push(i); } int top = myQue.front(); myQue.pop(); }
stack
栈
int main() { stack<int> mystack; for(int i=0;i<4;i++){ mystack.push(i); } int top = mystack.top(); mystack.pop(); }
创建容器时指定排序规则
针对有序容器使用,如map,set。vector和unordered_map则不行
struct compstru { bool operator()(int a, int b) { return a > b; } }; template <class T1, class T2> void printSet(const set<T1, T2> &mySet) { for (auto it = mySet.begin(); it != mySet.end(); it++) { cout << *it << " "; } cout << endl; } template <class T1, class T2, class T3> void printMap(const map<T1, T2, T3> &mySet) { for (auto it = mySet.begin(); it != mySet.end(); it++) { cout << it->second << " "; } cout << endl; } int main() { set<int, compstru> mySet = {1, 1, 2, 3}; printSet<int, compstru>(mySet); // 3 2 1 map<int, char, compstru> myMap = {{1, 'c'}, {2, 'b'}, {3, 'd'}}; printMap<int, char, compstru>(myMap); // d b c }
到此这篇关于C++常见的stl容器与相关操作 示例解析的文章就介绍到这了,更多相关C++ stl容器内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)