C++標準模版庫(STL)之vector容器詳解
vector容器介紹
容器顧名思義就是存放數(shù)據(jù)的東西,和我們的水桶用于裝水是一個原理。vector的功能和水桶一樣,就是用來裝東西的,我們的水桶可以用于裝水,也可以用來裝米飯,稻子,汽油等。我們的容器也是可以裝各種數(shù)據(jù),基本類型的數(shù)據(jù),自定義的數(shù)據(jù)類型,并且vector還提供了迭代器來很方便的訪問這些數(shù)據(jù),下面就讓我們一起看下如何使用C++的vector
vector 的使用
在我們使用vector之前,我們要先引入vector的頭文件,就和我們使用Java的類要先import對應的包一樣
#include<vector>
1. vector存放內置數(shù)據(jù)類型數(shù)據(jù)
vector 存放內置數(shù)據(jù)類型數(shù)據(jù)其實就是存放基本數(shù)據(jù)類型的數(shù)據(jù),如int,float,double,string 等,接下來請看存放內置數(shù)據(jù)類型數(shù)據(jù)的方法:
創(chuàng)建vector并插入數(shù)據(jù)
創(chuàng)建vector并且插入數(shù)據(jù):
// 創(chuàng)建了一個vector 容器,數(shù)組 vector<int> v; // 向容器中插入數(shù)據(jù) v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40);
遍歷vector
遍歷vector主要是依靠vector的begin()和end()方法,begin()方法代表是vector的開始,end代表的是vector的結束,遍歷vector有三種方式
方式1:使用While循環(huán)的方式:
// 起始迭代器,指向容器中第一個元素 vector<int>::iterator itBegin = v.begin(); // 結束迭代器 vector<int>::iterator itEnd = v.end(); while(itBegin != itEnd){ cout<<*itBegin<<endl; tBegin++; }
方式2:使用for循環(huán)
for(vector<int>::iterator it = v.begin();it !=v.end(); it++){ cout<< *it << endl; }
方式3:利用STL提供的遍歷算法 首先我們提供一個打印的函數(shù),
void myPrint(int val){ cout << val << endl; }
然后把函數(shù)名傳遞到for_each()函數(shù)中,如下所示:
// 第三種方式,利用STL提供的遍歷算法 for_each(v.begin(),v.end(),myPrint);
2.vector存放自定義數(shù)據(jù)類型
vector 也可以存放自定義的類型,例如我們自定義了一個Person類:
class Person { public: Person(string name,int age) { this->mName = name; this->mAge = age; } string mName; int mAge; };
然后創(chuàng)建vector并添加Person類型的數(shù)據(jù)
vector<Person> v; Person p1("aa",10); Person p2("bb",20); Person p3("cc",30); Person p4("dd",40); // 添加數(shù)據(jù) v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4);
遍歷數(shù)據(jù)的時候我們需要注意的是我們拿到的元素是Person引用,如下面代碼中的it,代表的是Person的引用,我們解引用后得到的是Person,如果要訪問姓名可以使用(*it).mName,或者是it->mName來訪問
// 遍歷數(shù)據(jù) for(vector<Person>::iterator it = v.begin(); it != v.end();it ++){ //cout<<"姓名: " << (*it).mName << // " ,age:" << (*it).mAge << endl; cout<<"姓名: " << it->mName << " ,age:" << it->mAge << endl; }
所有測試代碼
#include<iostream> #include<vector> #include<string> using namespace std; void myPrint(int val){ cout << val << endl; } void test01() { // 創(chuàng)建了一個vector 容器,數(shù)組 vector<int> v; // 向容器中插入數(shù)據(jù) v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); // // 通過迭代去訪問容器中的數(shù)據(jù) // vector<int>::iterator itBegin // = v.begin();//起始迭代器,指向容器中第一個元素 // vector<int>::iterator itEnd = v.end();//結束迭代器 // // 第一種遍歷方式 // while(itBegin != itEnd){ // cout<<*itBegin<<endl; // itBegin++; // } // 第二種遍歷方式 for(vector<int>::iterator it = v.begin(); it !=v.end(); it++){ cout<< *it << endl; } // 第三種方式,利用STL提供的遍歷算法 for_each(v.begin(),v.end(),myPrint); } // vector 存放自定義類型 class Person { public: Person(string name,int age) { this->mName = name; this->mAge = age; } string mName; int mAge; }; void test02(){ vector<Person> v; Person p1("aa",10); Person p2("bb",20); Person p3("cc",30); Person p4("dd",40); // 添加數(shù)據(jù) v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4); // 遍歷數(shù)據(jù) for(vector<Person>::iterator it = v.begin(); it != v.end();it ++){ //cout<<"姓名: " << (*it).mName << // " ,age:" << (*it).mAge << endl; cout<<"姓名: " << it->mName << " ,age:" << it->mAge << endl; } } // 存放自定義數(shù)據(jù)類型 指針 void test03() { vector<Person*> v; Person p1("aa",10); Person p2("bb",20); Person p3("cc",30); Person p4("dd",40); // 添加數(shù)據(jù) v.push_back(&p1); v.push_back(&p2); v.push_back(&p3); v.push_back(&p4); // 遍歷數(shù)據(jù) for(vector<Person *>::iterator it = v.begin(); it != v.end();it ++){ cout<<"::姓名: " << (*it)->mName << " ,::age:" << (*it)->mAge << endl; } } int main() { test03(); return 0; }
3.vector 容器嵌套容器
容器嵌套容器意思就是在vector中存放vector,把vector作為vector的元素存放起來,我們一起來看下具體怎么做 首先我們創(chuàng)建一個vector容器,用于存放vector類型的數(shù)據(jù)
vector< vector<int> > v;
然后我們創(chuàng)建4個小容器,這四個小容器作為我們開頭創(chuàng)建的大容器的元素,并為每個小容器存放一些值用于測試
// 創(chuàng)建小容器 vector<int> v1; vector<int> v2; vector<int> v3; vector<int> v4; // 向小容器中添加數(shù)據(jù) for(int i = 0;i<4;i++){ v1.push_back(i+1); v2.push_back(i+2); v3.push_back(i+3); v4.push_back(i+4); }
然后我們將小容器存放到大容器里面
// 將小容器插入到大容器 v.push_back(v1); v.push_back(v2); v.push_back(v3); v.push_back(v4);
最后是遍歷容器,第一次拿到的it是一個容器,還需要遍歷it,然后才是值,所以需要使用兩個for循環(huán)
for(vector< vector<int> >:: iterator it = v.begin(); it != v.end();it ++){ // (*it) 是一個容器 for(vector<int>::iterator vit = (*it).begin();vit != (*it).end();vit ++){ cout<< *vit << " "; } cout<< endl; }
通過大容器把所有數(shù)據(jù)遍歷一遍,我們看遍歷得到每一個元素的類型可以參考<>里面的類型,<>中是啥(*it)就是啥,如果是vector<int> 那么拿到的(*it)就是整型數(shù)據(jù),如果是vector<vector<int>> 拿到的就是整型類型的容器
總結
本文我們主要簡單的介紹了C++ STL中的vector容器,包括容器存放內置數(shù)據(jù)類型,自定義數(shù)據(jù)類型,以及集合迭代器iterator遍歷容器里面的數(shù)據(jù)??赐炅瞬┛鸵欢ㄒ嗉泳毩?,這樣才能學以致用
到此這篇關于C++標準模版庫(STL)之vector容器詳解的文章就介紹到這了,更多相關C++ vector容器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++與QML進行數(shù)據(jù)交互實現(xiàn)方式介紹
迫于無奈開始寫android的程序,以前使用QWidget的方式試過,雖然界面可以實現(xiàn),但是最后調用攝像頭時,未能成功,再沒有繼續(xù)。這幾天開始使用qml進行嘗試,在使用的過程中,其中的一個難點,就是在qml與c++中數(shù)據(jù)的交互2022-09-09C++?基本數(shù)據(jù)類型中int、long等整數(shù)類型取值范圍及原理分析
這篇文章主要介紹了C++?基本數(shù)據(jù)類型中int、long等整數(shù)類型取值范圍及原理分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11C++實現(xiàn)LeetCode(168.求Excel表列名稱)
這篇文章主要介紹了C++實現(xiàn)LeetCode(168.求Excel表列名稱),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08C++中引用、內聯(lián)函數(shù)、auto關鍵字和范圍for循環(huán)詳解
本文主要梳理了C++當中一些瑣碎的知識點,包括有命名空間,缺省參數(shù),引用,auto關鍵字和內聯(lián)函數(shù),文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2023-02-02