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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

STL7-基础

發布時間:2025/3/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL7-基础 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、容器可以嵌套容器

2、容器分為序列式容器和關聯式容器

序列式容器:容器的元素的位置是由進入容器時機和地點來決定

關聯式容器:容器已經有規則,進入容器的元素的位置不是由進入容器時機和地點來決定

? ? ? ? ? ? ? ? ? ? ? 只與此容器的排列規則有關

3、迭代器 理解為指針。實際迭代器是一個類。這個類封裝一個指針

1、stl算法和數據結構分離

#include<iostream> using namespace std;//算法 負責統計某個數出現次數 int mycount1(int* a, int n,int value){int count = 0;for (int i = 0; i < n; i++){if (a[i] == value)count++;}return count; }int main1() {int a[] = { 0,4,7,23,1,9,4,5,15 };int n = sizeof(a) / sizeof(a[0]);int value = 4;cout << mycount1(a, n, value) << endl; }int mycount(int* start, int* end, int value) {int count = 0;/*for (; start != end; start++) {if (*start == value)count++;}*/while (start != end){if (*start == value)count++;start++;}return count; }int main() {int arr[] = { 0,4,7,23,1,9,4,5,15 };int* pBegin = arr;int* pEnd = &(arr[sizeof(arr) / sizeof(int)]);int value = 4;cout <<value<<"出現次數:"<< mycount(pBegin, pEnd, value) << endl; }

2、stl helloworld

#include<iostream> #include<vector> #include<algorithm> using namespace std;//STL 基本容器 void printVector(int v) { //傳進來容器元素cout << v << " "; } void test01() {//定義一個容器 并且指定這個容器存放的元素的數據類型是int//vector是隊列式存儲,先進先出vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);//通過STL提供的for_each算法//容器提供迭代器//vector<int>::iterator 所定義的迭代器類型vector<int>::iterator pBegin = v.begin();vector<int>::iterator pEnd = v.end();//容器中可能存放基礎數據類型,也可能存放自定義數據類型for_each(pBegin, pEnd, printVector);cout << endl; }//容器中存放自定義數據類型 class Person { public:Person(int Age, int Id) :age(Age), id(Id) {} public:int age;int id; };void test02() {vector<Person> v;Person p1(10, 1), p2(20, 2), p3(30, 3);v.push_back(p1);v.push_back(p2);v.push_back(p3);for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout << (*it).age << " " << (*it).id << " " << endl;}cout << endl;} void PrintPerson(Person* v) //Person* v 意思是Person類型的指針v,只有用*v才能取到指針中的數據 {cout <<(*v).age<<" "<<(*v).id<< endl; }//容器中存放Person類型指針 并且利用for_each打印 void test03() {vector<Person*> v;Person p1(10, 1), p2(20, 2), p3(30, 3);v.push_back(&p1);v.push_back(&p2);v.push_back(&p3);vector<Person*>::iterator pBegin = v.begin();vector<Person*>::iterator pEnd = v.end();cout << "for_each循環輸出:" << endl;for_each(pBegin, pEnd, PrintPerson);cout << "for循環輸出:" << endl;for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++) {//*it 存放的是一個Person類型的指針,*(*t) 存放的是Person類型指針中的數據cout << (**it).age << " " << (**it).id << " " << endl; }cout << endl;}//容器中嵌套容器 void test04() {vector<vector<int>> v;vector<int> subv1,subv2;subv1.push_back(11);subv1.push_back(22);subv1.push_back(33);subv1.push_back(44);subv2.push_back(111);subv2.push_back(222);subv2.push_back(333);subv2.push_back(444);v.push_back(subv1);v.push_back(subv2);for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++) {for (vector<int>::iterator itt = (*it).begin(); itt != (*it).end(); itt++) {cout << *itt << " ";}cout << endl;}} int main() {cout << "-------------test01----------" << endl;test01();cout << "-------------test02----------" << endl;test02();cout << "-------------test03----------" << endl;test03();cout << "-------------test04----------" << endl;test04();return 0; }

運行結果:

總結

以上是生活随笔為你收集整理的STL7-基础的全部內容,希望文章能夠幫你解決所遇到的問題。

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