C++STL (vector,list,map)
2014-09-15 21:45
357 查看
////////////////////////////////////////////////////////////////////////// ///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、deque、map、stack、queue)
- C++中防止STL中迭代器失效__map/set等关联容器vector/list/deque等序列容器_如何防止迭代器失效_即erase()的使用
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- C++ STL基本容器string,vector,list,deque,map..[转]
- C++中防止STL中迭代器失效__map/set等关联容器vector/list/deque等序列容器_如何防止迭代器失效_即erase()的使用
- c++几种常见STL容器比较和分析 hashmap, map, vector, list
- C++ STL中vector、list、deque和map的区别
- C++STL(vector,map,set,list)成员函数整理
- C++ STL容器的使用方法(vector、queue、list、set、map)
- C/C++--STL中list,vector,deque,map,set区别、联系和使用场景
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- C/C++--STL中list,vector,deque,map,set区别、联系和使用场景
- C++ STL容器的学习使用(vector、queue、list、set、map)
- STL 基本容器 : string、vector、list、deque、set、map 的区别
- STL基本容器: string、vector、list、deque、set、map
- STL中基本容器string,vector,list,deque,set,map之间的区别
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)
- C++ 标准库 vector list map 基本使用方法 示例程序