日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL容器——案例版

發布時間:2024/3/13 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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()

#pragma warning(disable:4786)#include <map>#include <iostream>using namespace std;int main(){// 創建 map 容器對象 mmap<const char*, float> m;// 插入元素(水果名,單價),水果名位鍵值,單價為映照數據m["apple"] = 3.6f;m["orange"] = 3.2f;m["banana"] = 1.8f;m["pear"] = 2.3f;m["lichee"] = 6.3f;m.insert(pair<const char*, float>("曾果粒", 100.001f));// 打印元素cout << "蘋果價格:" << m["apple"] << "元/斤 \n";cout << "桔子價格:" << m["orange"] << "元/斤 \n";cout << "香蕉價格:" << m["banana"] << "元/斤 \n";cout << "雪梨價格:" << m["pear"] << "元/斤 \n";cout << "荔枝價格:" << m["lichee"] << "元/斤 \n";cout << "pair插入 " << m["曾果粒"] << "元/斤\n";return 0; }

總結

以上是生活随笔為你收集整理的STL容器——案例版的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。