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; }運行結果:
總結
- 上一篇: unittest-常见问题解决方案记录
- 下一篇: opencv源码查看