容器 迭代器

2021-08-08 07:17:34 字數 988 閱讀 1947

stl包括:   容器(資料結構)  迭代器(遍歷資料)    演算法

順序容器:  vector(向量)  list(鍊錶)  deque(雙端佇列)

關聯容器:set(集合) multist  ,  map(對映),multimap

迭代器:類似於指標 用來訪問容器中的單個資料項

迭代器由類iterator來宣告。

#include

#include

#include

using namespace std;

struct student

;bool comper(student s1, student s2)

int main()

, s2, s3, \

s4, s5, s6;

vectorvt;

vt.push_back(s1);

vt.push_back(s2);

vt.push_back(s3);

vector::iterator vi;

vi = vt.begin();

vi = vt.insert(vi,s4);

vi = vt.insert(vi,s5);

vt.insert(vi,s6);

for (vi = vt.begin(); vi != vt.end(); vi++)

cout << vi->numer << '\t' << vi->name <<'\t'cout << "----------排序後-------------" << endl;

sort(vt.begin(),vt.end(),comper);

for (vi = vt.begin(); vi != vt.end(); vi++)

cout << vi->numer << '\t' << vi->name << '\t' << vi->score << endl;

system("pause");

return 0;

}

c 容器演算法迭代器初識 容器演算法迭代器

c 容器演算法迭代器初識 容器演算法迭代器 stl中最常用的為迭代器vector,可以理解為陣列,下面我們將學習如何向這個容器 中插入資料 並遍歷這個容器 容器 vector 演算法 for each 迭代器 vector iteator include include include using ...

容器操作 管理迭代器

include include include include include include include using namespace std 在容器內查詢s1,並將s2插入到s1後面 void test and insert forward list sflst,const string ...

c 容器迭代器問題

一 迭代器失效問題 對於節點式容器 map,list,forward list,set 元素的刪除,插入操作會導致指向該元素的迭代器,指標,引用失效,但是其他元素迭代器,指標,引用不受影響 對於順序式容器 vector,string 元素的插入肯定會導致指向該元素以及後面的元素迭代器,指標,引用失效...