C++STL (vector,list,map),stlvector

來源:互聯網
上載者:User

C++STL (vector,list,map),stlvector

/////////////////////////////////////////////////////////////////////////////author:Jeson Yang///Date:2014.9.15//////////////////////////////////////////////////////////////////////////#include <vector>#include <list>#include <iostream>#include <map>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ ////////////////////////////////////////////////////////////////////////// //vector vector<int> *vecInteger = new vector<int>(); int num[10] = {1,2,3,4,5,6,7,8,9,10}; vecInteger->push_back(num[0]); vecInteger->push_back(num[1]); std::cout<<"iterator use way"<<endl; for (vector<int>::iterator it = vecInteger->begin(); it != vecInteger->end(); ++it) {  std::cout<<*it<<endl; } cout<<"for int i"<<endl; for (int i = 0; i < vecInteger->size(); i++) {  cout<<*(vecInteger->begin() + i)<<endl;  cout<<(*vecInteger)[i]<<endl; } vecInteger->pop_back(); vecInteger->clear(); vecInteger->clear(); delete vecInteger; ////////////////////////////////////////////////////////////////////////// //map map<int, int> *mapInteger = new map<int, int>(); mapInteger->insert(pair<int, int>( 1, num[0])); mapInteger->insert(pair<int, int>(2, num[1])); mapInteger->insert(map<int, int>::value_type(3, num[2])); for (map<int, int>::iterator it = mapInteger->begin(); it != mapInteger->end(); it++) {  cout<<"it->first = "<<it->first << "it->second = " <<it->second<<endl; } for (int i = 0; i < mapInteger->size(); i++) {  cout<<(&mapInteger)[i]<<endl;  } map<int, int>::iterator iter = mapInteger->find(1);   if(iter != mapInteger->end())   {    cout<<"Find, the value is "<<iter->second<<endl;   }   else   {    cout<<"Do not Find"<<endl;   }   mapInteger->clear(); delete mapInteger; ////////////////////////////////////////////////////////////////////////// //list list<int> *listInteger = new list<int>(); listInteger->push_back(num[0]); listInteger->push_back(num[1]); listInteger->push_front(num[2]); //list不要插入兩個相同的元素,否則根據insert的描述可能出現問題  //listInteger->insert() for (list<int>::iterator it = listInteger->begin(); it != listInteger->end(); it++) {  cout<<*it<<endl; } //reinterpret_cast<list<int>::iterator>(); for (int i = 0; i < listInteger->size(); i++) {  //cout<<*(listInteger->begin() + i)<<endl; } listInteger->pop_back(); listInteger->pop_front(); listInteger->remove(num[1]); listInteger->clear(); delete listInteger; return 0;} 


 


怎在遍曆list,vector,map時刪除合格元素

int main(){ int a[]={1,2,3,4,5,6,7,8,9,10}; list<int> b=list<int>(); b.assign(a,a+10); vector<int> d; d.assign(a,a+10); map<int,int> c=map<int,int>(); for(list<int>::iterator it=b.begin();it!=b.end();it++){ c[*it]=*it; } //list中刪除8,9 for(list<int>::iterator it=b.begin();it!=b.end();){ if(*it>=8&&*it<10){ it=b.erase(it); }else{ it++; } } //list中刪除5,6,注意這種寫法只適應於list,不適應於vector,因為vector沒有remove_if方法 b.erase(remove_if(b.begin(),b.end(),Pred()),b.end()); //vector中刪除3,4 for(vector<int>::iterator it=d.begin();it!=d.end();){ if(*it>=3&&*it<=4){ it=d.erase(it); }else{ it++; } } ostream_iterator<int> os(cout," "); copy(b.begin(),b.end(),os); cout<<endl; copy(d.begin(),d.end(),os); cout<<endl; //map中刪除5,6 for(map<int,int>::iterator it=c.begin();it!=c.end();){ if((*it).first>=5&&(*it).first<=6){ c.erase(it++); } else{ it++; } } for_each(c.begin(),c.end(),f);}
 
STL 中的哪種結構是連續形式的儲存: A map B set C list D vector

應該是 vector
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.