C++ list增刪改查遍歷反轉(zhuǎn)排序

int main()

{

? ? list<int> test;

? ? //插入

? ? test.push_back(1);

? ? test.push_back(1);

? ? test.push_front(0);

? ? test.push_back(2);


? ? auto it = test.begin();

? ? for (int count = 0; it != test.end() && count < 2; it++, count++);

? ? test.insert(it, 3);//在迭代器之前插入

? ? //排序

? ? test.sort();

? ? //遍歷

? ? for (auto &i : test)

? ? ? ? cout << i << endl;

? ? //刪除

? ? for (auto it = test.begin(); it != test.end();){

? ? ? ? if (*it != 2)

? ? ? ? ? ? it++;

? ? ? ? else

? ? ? ? ? ? it=test.erase(it);//erase會銷毀迭代器,并更新迭代器的值

? ? }

? ? test.push_back(INT_MAX);

? ? test.push_front(INT_MIN);

? ? test.pop_back();

? ? test.pop_front();

? ? test.erase(remove(test.begin(), test.end(), 1), test.end());//remove非成員函數(shù),并不能真正刪除,只是把要刪除的數(shù)據(jù)扔到后面并返回尾迭代器的值

? ? //不支持隨機訪問

? ? //cout << test[0] << endl;

? ? //查找

? ? if (find(test.begin(), test.end(), 3) != test.end())

? ? ? ? cout << "find it" << endl;

? ? //反轉(zhuǎn)

? ? reverse(test.begin(), test.end());

? ? return 0;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容