STL容器——案例版
生活随笔
收集整理的這篇文章主要介紹了
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;}cout << endl;system("pause");return 0; }stack (棧)
#include <iostream> #include<stack> #include <string> using namespace std; int main() {//stack 先進后出stack<string> mystack;mystack.push("飯");mystack.push("要吃");mystack.push("我");while (!mystack.empty()){cout << mystack.top();mystack.pop();}return 0; } //----------------------------------------- 讀取堆棧的棧頂元素 #include <stack> #include <iostream> using namespace std; int main() {// 創建堆棧對象stack<int> s;// 元素入棧s.push(3);s.push(19);s.push(23);s.push(36);s.push(50);s.push(4);// 元素依次出棧while(!s.empty()){// 打印棧頂元素,打印出:4 50 36 23 19 3cout << s.top() << endl;// 出棧s.pop();}return 0; }queue(隊列)
#include <iostream> #include<queue> #include <list> #include <string> using namespace std; int main() {queue<string> qs;qs.push("我");qs.push("想");qs.push("你");while (!qs.empty()){cout << qs.front(); //返回第一個元素qs.pop(); // 沒有pop會死循環。 //從隊頭移除第一個元素}return 0; }嵌套 //list<string> Ls; //Ls.push_back("12123"); //queue<list<string>> QS; //QS.push(Ls); #include <queue> #include <string> #include<iostream>using namespace std; class Person { public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}string m_Name;int m_Age; };void test01() {//創建隊列queue<Person> q;//準備數據Person p1("唐僧", 30);Person p2("孫悟空", 1000);Person p3("豬八戒", 900);Person p4("沙僧", 800);//向隊列中添加元素 入隊操作q.push(p1);q.push(p2);q.push(p3);q.push(p4);//隊列不提供迭代器,更不支持隨機訪問 while (!q.empty()) {//輸出隊頭元素cout << "隊頭元素-- 姓名: " << q.front().m_Name<< " 年齡: " << q.front().m_Age << endl;/* cout << "隊尾元素-- 姓名: " << q.back().m_Name<< " 年齡: " << q.back().m_Age << endl;*/cout << endl;//彈出隊頭元素q.pop();}cout << "隊列大小為:" << q.size() << endl; }int main() {test01();system("pause");return 0; }//push(elem); //往隊尾添加元素 //pop(); //從隊頭移除第一個元素 //back(); //返回最后一個元素 //front(); //返回第一個元素 // //empty(); //判斷堆棧是否為空 //size(); //返回棧的大小 // //queue& operator=(const queue& que); // 賦值操作--重載等號操作符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!= NULLfor (L1_Iter = L1.begin(); L1_Iter!=L1.end(); L1_Iter++){cout << " " << *L1_Iter;}system("pause"); //防止閃屏return 0; } /* 插入 list 鏈表元素 解釋:push_back 函數在 list 尾部添加元素;push_front 函數在 list 鏈首插入元素;iterator insert(iterator pos, const T& x) 在任意位置插入元素 */#include <list> #include <iostream> using namespace std; int main() {list<int> lInt;lInt.push_back(1);lInt.push_back(3);lInt.push_back(4);lInt.push_back(5);// 插入鏈表元素list<int>::iterator i, iend;i = lInt.begin();++i; // 因為是 鏈表結構,所以,只能通過指針的移位找到要插入的位置lInt.insert(i, 20); // 在第2個位置,插入元素 20lInt.push_front(0);// 打印鏈表元素iend = lInt.end();for (i = lInt.begin(); i != iend; ++i)cout << *i << ' ';cout << endl;return 0;}set(集合,鍵和值相同)
特有find()
#pragma warning(disable:4786) #include <set> #include <iostream> using namespace std; int main() {multiset<int> ms;ms.insert(10);ms.insert(13);ms.insert(11);ms.insert(19);ms.insert(13);ms.insert(19);ms.insert(19);// 打印數據multiset<int>::iterator i, iend;iend = ms.end();for (i = ms.begin(); i != iend; ++i)cout << *i << ' ';cout << endl;return 0; }map(單映射)
pair< frist(鍵),second(值) >
1、insert( pair<type,type> elem ) 插入鍵值
2、erase( iter ) 刪除元素
3、size() 容器中元素的個數
4、begin()
5、end()
總結
以上是生活随笔為你收集整理的STL容器——案例版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: macOS 上安装 PECL
- 下一篇: 图像处理那张熟悉的面孔——Lena