首页 > 代码库 > C++STL (vector,list,map)

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

 


 

C++STL (vector,list,map)