C++STL (vector,list,map)

//////////////////////////////////////////////////////////////////////////
///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;
}

 


 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。