c++ 顺序容器list的一个例子
生活随笔
收集整理的這篇文章主要介紹了
c++ 顺序容器list的一个例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
程序功能:刪除一個list容器內的偶數元素,且復制奇數元素。?
#include <iostream> #include <string> #include <vector> #include <deque> #include <list> #include <forward_list> #include <string> #include <array> using namespace std; #define LENGTH 10 typedef int TYPE; typedef vector<TYPE> VECTORTYPE; typedef deque<TYPE> DEQUETYPE; typedef list<TYPE> LISTTYPE; typedef forward_list<TYPE> FORWARD_LISTTYPE; typedef array<TYPE,LENGTH> ARRAYTYPE; void print(VECTORTYPE & ); void print(DEQUETYPE & ); void print(LISTTYPE & ); void print(FORWARD_LISTTYPE & ); void print(ARRAYTYPE & ); void print(string &); void pluralize(size_t ,string &); void print(list<string> &); void print(vector<string> &); int main() {list<int> lint{1,2,3,4,5,6,7,8,9,9,10,10,11,12,12};print(lint);list<int>::iterator begin = lint.begin();while(begin != lint.end()){if(*begin % 2){begin = lint.insert(begin,*begin);++ begin;++ begin;}else{begin = lint.erase(begin);}}print(lint);return 0; } void print(VECTORTYPE &vec) {for(VECTORTYPE::iterator i = vec.begin() ; i != vec.end() ; ++i){ cout << *i << " ";}cout << endl;return ; }void print(DEQUETYPE &vec) {for(DEQUETYPE::iterator i = vec.begin() ; i != vec.end() ; ++i){ cout << *i << " ";}cout << endl;return ; }void print(LISTTYPE &vec) {for(LISTTYPE::iterator i = vec.begin() ; i != vec.end() ; ++i){ cout << *i << " ";}cout << endl;return ; }void print(FORWARD_LISTTYPE &vec) {for(FORWARD_LISTTYPE::iterator i = vec.begin() ; i != vec.end() ; ++i){ cout << *i << " ";}cout << endl;return ; }void print(ARRAYTYPE &vec) {for(ARRAYTYPE::iterator i = vec.begin() ; i != vec.end() ; ++i){ cout << *i << " ";}cout << endl;return ; }void print(string & s_vec) {for(string::iterator i = s_vec.begin(); i != s_vec.end() ; ++i) {cout << *i << " ";}cout << endl; } void pluralize(size_t cnt,string &word) {if(cnt > 1)word.push_back('s');//equal to word += 's' }void print(vector<string> & s_vec) {for(auto &i :s_vec)cout << i << " ";cout << endl; } void print(list<string> & s_list) {for(auto & i: s_list)cout << i << " ";cout << endl; }注意:
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的c++ 顺序容器list的一个例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++primer第五版,p134页第2
- 下一篇: c++ Segmentation fau