1. 程式人生 > >c++之map函數/叠代器

c++之map函數/叠代器

() upper c++ 數組 map函數 begin equal code html

參考文獻:https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
//刪除某個元素  erase():
    map<int, string> mapStu;
    mapStu.insert(map<int, string>::value_type(1, "stu1"));
    mapStu.insert(map
<int, string>::value_type(2, "stu2")); mapStu.insert(pair<int, string>(3, "stu3")); map<int, string>::iterator itor; itor = mapStu.find(1); mapStu.erase(2); //根據鍵值刪除某個元素 //mapStu.erase(itor); //根據叠代器刪除 //mapStu.erase(mapStu.begin(), mapStu.end()); //刪除一個範圍內的
for (itor = mapStu.begin(); itor != mapStu.end(); itor++) cout << itor->second <<endl; //find函數:傳入的參數是要查找的key。 //用find函數來定位數據出現位置,它返回的一個叠代器,當數據出現時, //它返回數據所在位置的叠代器,如果map中沒有要查找的數據,它返回的叠代器等於end函數返回的叠代器。 /* map<int, string> mapStu; mapStu.insert(map<int, string>::value_type(1, "stu1")); mapStu.insert(map<int, string>::value_type(2, "stu2")); mapStu.insert(pair<int, string>(3, "stu3")); map<int, string>::iterator itor; itor = mapStu.find(3); if (itor != mapStu.end()) cout << itor->second << endl; //stu3 itor = mapStu.lower_bound(1); if (itor != mapStu.end()) cout << itor->second << endl; //stu1 itor = mapStu.upper_bound(1); if (itor != mapStu.end()) cout << itor->second << endl; //stu2 itor = mapStu.lower_bound(2); if (itor != mapStu.end()) cout << itor->second << endl; //stu2 itor = mapStu.upper_bound(2); if (itor != mapStu.end()) cout << itor->second << endl; //stu3 pair<map<int, string>, map<int, string>> pairStu; pairStu = mapStu.equal_range(2); //equal_range(2) if (pairStu.first == pairStu.second) cout << "不存在" << endl;
*/ //數組形式遍歷 /*map<int, string> mapStu; mapStu.insert(map<int, string>::value_type(1, "stu1")); mapStu.insert(map<int, string>::value_type(2, "stu2")); mapStu.insert(pair<int, string>(3, "stu3")); int nsize = mapStu.size(); for (int size = 1; size <= nsize; size++) cout << mapStu[size] << endl;*/ //反向遍歷 //map<int, string> mapStu; //mapStu.insert(map<int, string>::value_type(1, "stu1")); //mapStu.insert(map<int, string>::value_type(2, "stu2")); //mapStu.insert(pair<int, string>(3, "stu3")); //pair<map<int, string>::iterator, bool> pair_insert; //pair_insert = mapStu.insert(pair<int, string>(3, "stu4")); //if (pair_insert.second == true) // cout << "success" << endl; //else // cout << "fail" << endl; //map<int, string>::reverse_iterator itor; //for (itor = mapStu.rbegin(); itor != mapStu.rend(); itor++) // cout << itor->first << " " << itor->second << endl; //int size = mapStu.size(); //cout << size << endl; //關於是否能夠替換:數組方式2 //map<int, string> mapStu; //mapStu.insert(map<int, string>::value_type(1, "stu1")); //mapStu.insert(map<int, string>::value_type(2, "stu2")); //mapStu.insert(pair<int, string>(3, "stu3")); //pair<map<int, string>::iterator, bool> pair_insert; //pair_insert = mapStu.insert(pair<int, string>(3, "stu4")); //if (pair_insert.second == true) // cout << "success" << endl; //else // cout << "fail" << endl; //map<int, string>::iterator itor; //for (itor = mapStu.begin(); itor != mapStu.end(); itor++) // cout << itor->first << " " << itor->second <<endl; //int size = mapStu.size(); //cout << size << endl; //關於是否能夠替換:數組方式1 //map<int, string> mapStu; //mapStu[1] = "stu0"; //mapStu[2] = "stu1"; //mapStu[3] = "stu2"; //mapStu[3] = "stu3"; //輸出替換了stu2 //map<int, string>::iterator itor; //for (itor = mapStu.begin(); itor != mapStu.end(); itor++) // cout << itor->second << endl; //方法3 //map<int, string> mapStu; //mapStu[1] = "stu0"; //mapStu[2] = "stu1"; //mapStu[3] = "stu2"; //map<int, string>::iterator itor; //for (itor = mapStu.begin(); itor != mapStu.end(); itor++) // cout << itor->first << endl; //方法2 // map<int, string> mapStu; // mapStu.insert(map<int,string>::value_type(1, "stu1")); // mapStu.insert(map<int,string>::value_type(2, "stu2")); // mapStu.insert(map<int, string>::value_type(3, "stu3")); // map<int, string>::iterator iter; // for (iter = mapStu.begin(); iter != mapStu.end(); iter++) // cout << iter->second << endl; //方法1 /*map<int, string> mapStu; mapStu.insert(pair<int, string>(1, "stu1")); mapStu.insert(pair<int, string>(2, "stu2")); mapStu.insert(pair<int, string>(3, "stu3")); map<int, string>::iterator inter; for (inter = mapStu.begin(); inter != mapStu.end(); inter++) cout << inter->first << endl;*/ system("pause"); return 0; }

c++之map函數/叠代器