首页 > 代码库 > 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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。