STL7-基础
1、容器可以嵌套容器
2、容器分為序列式容器和關(guān)聯(lián)式容器
序列式容器:容器的元素的位置是由進(jìn)入容器時(shí)機(jī)和地點(diǎn)來(lái)決定
關(guān)聯(lián)式容器:容器已經(jīng)有規(guī)則,進(jìn)入容器的元素的位置不是由進(jìn)入容器時(shí)機(jī)和地點(diǎn)來(lái)決定
? ? ? ? ? ? ? ? ? ? ? 只與此容器的排列規(guī)則有關(guān)
3、迭代器 理解為指針。實(shí)際迭代器是一個(gè)類。這個(gè)類封裝一個(gè)指針
1、stl算法和數(shù)據(jù)結(jié)構(gòu)分離
#include<iostream> using namespace std;//算法 負(fù)責(zé)統(tǒng)計(jì)某個(gè)數(shù)出現(xiàn)次數(shù) 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<<"出現(xiàn)次數(shù):"<< mycount(pBegin, pEnd, value) << endl; }2、stl helloworld
#include<iostream> #include<vector> #include<algorithm> using namespace std;//STL 基本容器 void printVector(int v) { //傳進(jìn)來(lái)容器元素cout << v << " "; } void test01() {//定義一個(gè)容器 并且指定這個(gè)容器存放的元素的數(shù)據(jù)類型是int//vector是隊(duì)列式存儲(chǔ),先進(jìn)先出vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);//通過(guò)STL提供的for_each算法//容器提供迭代器//vector<int>::iterator 所定義的迭代器類型vector<int>::iterator pBegin = v.begin();vector<int>::iterator pEnd = v.end();//容器中可能存放基礎(chǔ)數(shù)據(jù)類型,也可能存放自定義數(shù)據(jù)類型for_each(pBegin, pEnd, printVector);cout << endl; }//容器中存放自定義數(shù)據(jù)類型 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才能取到指針中的數(shù)據(jù) {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循環(huán)輸出:" << endl;for_each(pBegin, pEnd, PrintPerson);cout << "for循環(huán)輸出:" << endl;for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++) {//*it 存放的是一個(gè)Person類型的指針,*(*t) 存放的是Person類型指針中的數(shù)據(jù)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; }運(yùn)行結(jié)果:
總結(jié)
- 上一篇: unittest-常见问题解决方案记录
- 下一篇: opencv源码查看