码迷,mamicode.com
首页 > 编程语言 > 详细

C++STL (vector,list,map)

时间:2014-09-15 22:56:29      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:stl   map   iterator   list   vecotr   

//////////////////////////////////////////////////////////////////////////
///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)

标签:stl   map   iterator   list   vecotr   

原文地址:http://blog.csdn.net/yc7369/article/details/39299039

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!