C++_STL 各种容器
2015-03-14 11:47
344 查看
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <vector> #include <deque> #include <list> #include <set> #include <map> using namespace std; // sequence container // std::vector // std::deque // std::list // relevance container // std::set // std::multiset // std::map // std::multimap void test1() { // **********init a vector to leave int **********// cout << "**********init a vector to leave int**********" << endl; std::vector<int> vecInteger; // push_back value or insert value vecInteger.push_back(10); vecInteger.push_back(12); vecInteger.insert(vecInteger.begin(), 8); vecInteger.insert(vecInteger.end(), 14); // vector size int size = vecInteger.size(); cout << "size:" << size << endl; // vector loop std::vector<int>::iterator iterInt; for (iterInt = vecInteger.begin(); iterInt != vecInteger.end(); iterInt++) { cout << "*iterInt:" << *iterInt << endl; } // visit value int int_0 = vecInteger[0]; cout << "int_0:" << int_0 << endl; int int_1 = vecInteger.at(1); cout << "int_1:" << int_1 << endl; // remove value vecInteger.pop_back(); size = vecInteger.size(); cout << "size:" << size << endl; } void test2() { // **********init a vector to leave string **********// cout << "**********init a vector to leave string**********" << endl; std::vector<string> vecString; // push_back value or insert value vecString.push_back("Hello"); vecString.push_back("World"); vecString.insert(vecString.begin(), "Hi"); vecString.insert(vecString.end(), "Welcome"); // vector size int size = vecString.size(); cout << "size:" << size << endl; // vector loop std::vector<string>::iterator iterStr; for (iterStr = vecString.begin(); iterStr != vecString.end(); iterStr++) { cout << "iterStr:" << iterStr->c_str() << endl; } // visit value string str_0 = vecString[0]; cout << "str_0:" << str_0.c_str() << endl; string str_1 = vecString.at(1); cout << "str_1" << str_1.c_str() << endl; // remove value vecString.pop_back(); size = vecString.size(); cout << "size:" << size << endl; } void test3() { // **********init a deque to leave int **********// cout << "**********init a deque to leave int **********" << endl; //puah_back value or push_front value or insert value std::deque<int> deqInteger; deqInteger.push_back(10); deqInteger.push_back(12); deqInteger.push_front(8); deqInteger.push_front(6); deqInteger.insert(deqInteger.begin(), 4); deqInteger.insert(deqInteger.end(), 14); // deque size int size = deqInteger.size(); cout << "size:" << size << endl; // deque loop std::deque<int>::iterator iterInt; for (iterInt = deqInteger.begin(); iterInt != deqInteger.end(); iterInt++) { cout << "*iterInt:" << *iterInt << endl; } // visit value int int_0 = deqInteger[0]; cout << "int_0:" << int_0 << endl; int int_1 = deqInteger.at(1); cout << "int_1:" << int_1 << endl; // remove value deqInteger.pop_back(); size = deqInteger.size(); cout << "size:" << size << endl; deqInteger.pop_front(); size = deqInteger.size(); cout << "size:" << size << endl; } void test4() { // **********init a deque to leave string **********// cout << "**********init a deque to leave string **********" << endl; //puah_back value or push_front value or insert value std::deque<string> deqString; deqString.push_back("Hello"); deqString.push_back("World"); deqString.push_front("Hi"); deqString.push_front("Hi"); deqString.insert(deqString.begin(), "Oh"); deqString.insert(deqString.end(), "Welcome"); // deque size int size = deqString.size(); cout << "size:" << size << endl; // deque loop std::deque<string>::iterator iterStr; for (iterStr = deqString.begin(); iterStr != deqString.end(); iterStr++) { cout << "iterStr:" << iterStr->c_str() << endl; } // visit value string str_0 = deqString[0]; cout << "str_0:" << str_0.c_str() << endl; string str_1 = deqString.at(1); cout << "str_1:" << str_1.c_str() << endl; // remove value deqString.pop_back(); size = deqString.size(); cout << "size:" << size << endl; deqString.pop_front(); size = deqString.size(); cout << "size:" << size << endl; } void test5() { // **********init a list to leave int **********// cout << "**********init a list to leave int **********" << endl; std::list<int> litInt; // push_back value or push_front value or insert value litInt.push_back(10); litInt.push_back(12); litInt.push_front(8); litInt.push_front(6); litInt.insert(litInt.begin(), 4); litInt.insert(litInt.end(), 14); // list size int size = litInt.size(); cout << "size:" << size << endl; // list loop std::list<int>::iterator iterInt; for (iterInt = litInt.begin(); iterInt != litInt.end(); iterInt++) { cout << "*iterInt:" << *iterInt << endl; } // list reverse litInt.reverse(); // remove value litInt.pop_back(); size = litInt.size(); cout << "size:" << size << endl; litInt.pop_front(); size = litInt.size(); cout << "size:" << size << endl; } void test6() { // **********init a list to leave string **********// cout << "**********init a list to leave int **********" << endl; std::list<string> litStr; // push_back value or push_front value or insert value litStr.push_back("Hello"); litStr.push_back("World"); litStr.push_front("Hi"); litStr.push_front("Oh"); litStr.insert(litStr.begin(), "I"); litStr.insert(litStr.end(), "Welcome"); // list size int size = litStr.size(); cout << "size:" << size << endl; // list loop std::list<string>::iterator iterStr; for (iterStr = litStr.begin(); iterStr != litStr.end(); iterStr++) { cout << "iterInt:" << iterStr->c_str() << endl; } // list reverse litStr.reverse(); // remove value litStr.pop_back(); size = litStr.size(); cout << "size:" << size << endl; litStr.pop_front(); size = litStr.size(); cout << "size:" << size << endl; } void test7() { // **********init a set to leave int **********// cout << "**********init a set to leave int **********" << endl; std::set<int> SetInt; // insert value SetInt.insert(8); SetInt.insert(10); // set size int size = SetInt.size(); cout << "size:" << size << endl; // set loop std::set<int>::iterator iterInt; for (iterInt = SetInt.begin(); iterInt != SetInt.end(); iterInt++) { cout << "*iterInt:" << *iterInt << endl; } // remove value SetInt.erase(8); size = SetInt.size(); cout << "size:" << size << endl; } void test8() { // **********init a set to leave string **********// cout << "**********init a set to leave string **********" << endl; std::set<string> SetStr; // insert value SetStr.insert("Hello"); SetStr.insert("World"); // set size int size = SetStr.size(); cout << "size:" << size << endl; // set loop std::set<string>::iterator iterStr; for (iterStr = SetStr.begin(); iterStr != SetStr.end(); iterStr++) { cout << "iterStr:" << iterStr->c_str() << endl; } // remove value SetStr.erase("Hello"); size = SetStr.size(); cout << "size:" << size << endl; } void test9() { // **********init a multiset to leave int ********** // cout << "**********init a multiset to leave int **********" << endl; std::multiset<int> mSetInt; // insert value mSetInt.insert(10); mSetInt.insert(12); // multiset size int size = mSetInt.size(); cout << "size:" << size << endl; // multiset loop std::multiset<int>::iterator iterInt; for (iterInt = mSetInt.begin(); iterInt != mSetInt.end(); iterInt++) { cout << "iterInt:" << *iterInt << endl; } // remove value mSetInt.erase(10); size = mSetInt.size(); cout << "size:" << size << endl; } void test10() { // **********init a multiset to leave string ********** // cout << "**********init a multiset to leave string **********" << endl; std::multiset<string> mSetStr; // insert value mSetStr.insert("Hello"); mSetStr.insert("World"); // multiset size int size = mSetStr.size(); cout << "size:" << size << endl; // multiset loop std::multiset<string>::iterator iterStr; for (iterStr = mSetStr.begin(); iterStr != mSetStr.end(); iterStr++) { cout << "iterStr:" << iterStr->c_str() << endl; } // remove value mSetStr.erase("Hello"); size = mSetStr.size(); cout << "size:" << size << endl; } void test11() { // **********init a map to leave int ********** // cout << "**********init a map to leave int **********" << endl; std::map<string, int> mapInt; // insert value mapInt.insert(std::pair<string, int>("one", 10)); mapInt.insert(std::pair<string, int>("two", 12)); // map size int size = mapInt.size(); cout << "size:" << size << endl; // map loop std::map<string, int>::iterator iterInt; for (iterInt = mapInt.begin(); iterInt != mapInt.end(); iterInt++) { cout << "iterInt first:" << iterInt->first.c_str() << endl; cout << "iterInt secone:" << iterInt->second << endl; } // remove value mapInt.erase("one"); size = mapInt.size(); cout << "size:" << size << endl; } void test12() { // **********init a map to leave string ********** // cout << "**********init a map to leave string **********" << endl; std::map<string, string> mapStr; // insert value mapStr.insert(std::pair<string, string>("one", "Hello")); mapStr.insert(std::pair<string, string>("two", "World")); // map size int size = mapStr.size(); cout << "size:" << size << endl; // map loop std::map<string, string>::iterator iterStr; for (iterStr = mapStr.begin(); iterStr != mapStr.end(); iterStr++) { cout << "iterStr first:" << iterStr->first.c_str() << endl; cout << "iterStr secone:" << iterStr->second.c_str() << endl; } // remove value mapStr.erase("one"); size = mapStr.size(); cout << "size:" << size << endl; } void test13() { // **********init a multimap to leave int ********** // cout << "**********init a multimap to leave int **********" << endl; std::multimap<string, int> mMapInt; // insert value mMapInt.insert(std::pair<string, int>("one", 10)); mMapInt.insert(std::pair<string, int>("two", 12)); // map size int size = mMapInt.size(); cout << "size:" << size << endl; // map loop std::multimap<string, int>::iterator iterInt; for (iterInt = mMapInt.begin(); iterInt != mMapInt.end(); iterInt++) { cout << "iterInt first:" << iterInt->first.c_str() << endl; cout << "iterInt secone:" << iterInt->second << endl; } // remove value mMapInt.erase("one"); size = mMapInt.size(); cout << "size:" << size << endl; } void test14() { // **********init a multimap to leave string ********** // cout << "**********init a multimap to leave string **********" << endl; std::multimap<string, string> mMapStr; // insert value mMapStr.insert(std::pair<string, string>("one", "Hello")); mMapStr.insert(std::pair<string, string>("two", "World")); // map size int size = mMapStr.size(); cout << "size:" << size << endl; // map loop std::multimap<string, string>::iterator iterStr; for (iterStr = mMapStr.begin(); iterStr != mMapStr.end(); iterStr++) { cout << "iterStr first:" << iterStr->first.c_str() << endl; cout << "iterStr secone:" << iterStr->second.c_str() << endl; } // remove value mMapStr.erase("one"); size = mMapStr.size(); cout << "size:" << size << endl; } int main(const int argc, const char *argv[]) { test14(); system("pause"); return 0; }
相关文章推荐
- C++学习 STL之七:STL各种容器的使用时机详解
- C++内存管理变革(7):基于ScopeAlloc的STL容器
- C++ 之高效使用STL (STL 容器的选择)
- 注意C++ STL容器中erase的使用
- [C++再学习系列] 函数声明与STL容器构造
- C++ STL基本容器比较
- Ineffective C++(1):STL容器不仅仅可以保存指针
- C++ STL基本容器比较
- C/C++ STL容器之 stringstream 字符串流
- An Example about the use of map Container in C++ STL / c++ map容器的用法
- (转)C++ STL的各种实现版本介绍
- C++ STL基本容器比较
- C++ STL基本容器比较
- C++ STL基本容器string,vector,list,deque,map..[转]
- C++STL容器
- C++内存管理变革(7):基于ScopeAlloc的STL容器
- [C++再学习系列] STL容器删除操作总结
- 标准C++中string类及STL容器类简介
- 小词典 程序 C++ STL (容器部分)练习
- (转)C++ STL的各种实现版本介绍