容器操作 管理迭代器

2021-07-06 10:55:54 字數 3908 閱讀 5193

#include 

#include

#include

#include

#include

#include

#include

using namespace std;

//////

//////

//////

//////

//////

//////

//////

//////

//////

//////

//////

////////

//在容器內查詢s1,並將s2插入到s1後面

void test_and_insert(forward_list&sflst, const

string &s1, const

string &s2)

else

if (!inserted)

}int main(int argc, char **argv)

; auto it = lst.begin();

while (it != lst.end())

it = lst.begin();

cout << endl;

while (it != lst.end())

cout << endl;//刪除容器元素結束;

//////

//////

//////

//////

//////

//////

//////

//////

//////

//////

//////

////////

int ia = ;

vectoriv;

listil;

iv.assign(ia, ia + 10);//資料拷貝

il.assign(ia, ia + 10);

auto iiv = iv.begin();

while (iiv != iv.end())

if (!(*iiv & 1))

iiv = iv.erase(iiv);

else

iiv++;

auto iil = il.begin();

while (iil != il.end())

if (*iil & 1)

iil = il.erase(iil);

else

iil++;

for (iiv = iv.begin(); iiv != iv.end(); iiv++)

cout << *iiv << " ";

cout << endl;

for (iil = il.begin(); iil != il.end(); iil++)

cout << *iil << " ";

cout << endl;//刪除奇偶數

//////

//////

//////

//////

//////

//////

//////

//////

//////

//////

//////

////////

//forward_list 特殊插入刪除操作

forward_listforlist;

auto prev = forlist.before_begin();

auto fbegin = forlist.begin();

while (fbegin != forlist.end())

if (!(*fbegin % 2))//看是否為偶數或者使用 (!(*fbegin&1))

fbegin=forlist.erase_after(prev);

else

fbegin = forlist.begin();

while (fbegin != forlist.end())

cout << *fbegin++ << " ";

cout << endl;

/////////

//////

//////

//////

//////

//////

//////

//////

//////

//////

//////

/////

//呼叫函式test_and_insert

forward_listflist;

test_and_insert(flist, "hello", "heihei1");

auto fllist = flist.begin();

cout << *(++fllist) << endl;

/////////

//////

//////

//////

//////

//////

//////

//////

//////

//////

//////

/////

//list和forward_list物件的刪除和插入操作主要是鏈式儲存

listilst;

auto curr = ilst.begin();

while (curr!=ilst.end())

else

curr = ilst.erase(curr);

}for (curr = ilst.begin(); curr != ilst.end();curr++)

cout << endl;

/////////

//////

//////

//////

//////

//////

//////

//////

//////

//////

//////

/////

//vector是如何增長的

vector words;

for (int i = 0; i < 258;++i)

cout << words.capacity() << " "

<< words.size() << endl;//316 258

cout << words.capacity() -words.size() << endl;//58

words={};

for (int i = 0; i <258; ++i)

cout << words.capacity() << " "

<< words.size() << endl;//316 258

cout << words.capacity() - words.size() << endl;//58

for (int i = 0; i < 258; ++i)

cout << words.capacity() << " "

<< words.size() << endl;//711 517

cout << words.capacity()- words.size() << endl;//194

for (int i = 0; i <258; ++i)

cout << words.capacity() << " "

<< words.size() << endl;//1066 775

cout << words.capacity()-words.size() << endl;//291

//總結是成倍線性增長的空間,但是也會有些許誤差

容器 迭代器

stl包括 容器 資料結構 迭代器 遍歷資料 演算法 順序容器 vector 向量 list 鍊錶 deque 雙端佇列 關聯容器 set 集合 multist map 對映 multimap 迭代器 類似於指標 用來訪問容器中的單個資料項 迭代器由類iterator來宣告。include incl...

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

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

c 容器迭代器問題

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