STL MAP 整理

根據網上資料初步整理

 

/*容器map*/
///2015/05/22 20:16第一次整理
#include <iostream>
#include <map>
#include <iterator>
#include <cstdio>

using namespace std;

typedef struct StudentInfo
{
    int ID;
    string strName;
    /*重載小於號*/
    bool operator <(StudentInfo const & A) const
    {
        //制定排序策略,按ID排序,如果ID相等,按strName排序
        if(ID < A.ID) return true;
        if(ID == A.ID) return (strName.compare(A.strName) < 0);//compare函數,當第一個小於第二個返回負數,相等返回0,否則返回正數
        return false;
    }
}Stu,*PStu;

int main()
{
    map<string,int> k;
    pair<map<string,int>::iterator,bool> flag;//對組,bool值用來判斷insert操作是否成功
    map<string,int> ::iterator ite;//前向迭代器

///插入(3種)
    flag = k.insert(pair<string,int>("wl",1));//第一種插入方式
    if(flag.second == true)//insert成功
        cout << "ok" << endl;
    else
        cout << "no"<< endl;

    flag = k.insert(map<string,int>::value_type("wl",2));//第二種插入方式
    if(flag.second == true)
        cout << "ok" << endl;
    else
        cout << "no"<< endl;
    /*輸出結果:
    ok
    no
    */
//insert操作不插入主鍵值相同的元素


    k["wl"] = 3;//將主鍵值爲"wl"的元素第二鍵值改爲3;
    k["o"] = 3;//不存在主鍵值爲“o”的元素,將會自動創建這個元素;相當於插入這個元素;
///遍歷(3種)
    for(ite = k.begin(); ite != k.end(); ++ite) //記得加括號,前向遍歷
    {
        cout << ite -> first <<" " << ite -> second << endl;
    }

    map<string,int>::reverse_iterator iter; //反相迭代器
    for(iter = k.rbegin(); iter != k.rend(); ++iter)
    {
        cout << iter -> first <<" " << iter -> second << endl;//反相遍歷,和前向迭代器的輸出效果剛好相反
    }
    //迭代器貌似只能自加
    //還可以用數組的方法來遍歷容器,但是主鍵值必須連續

///查找(3種)
/*2015.05.22 20:40 第二次整理  完*/
    cout << k.count("wl") << endl;//count返回值爲1則存在這個元素
    cout << k.count("w") << endl;//0代表不存在

    ite = k.find("wl");
    if(ite != k.end())//如果找到,則返回值爲該元素存在的位置的迭代器,否則爲end()返回的迭代器
    {
        cout <<"find:" << ite -> second << endl;//注意沒有括號
    }
    else
    {
        cout << "don't find!" << endl;
    }


    map<int,string> l;
    map<int,string> ::iterator ite1;
    pair<map<int,string>::iterator,bool> flag1;
    l[1] = "a";
    l[7] = "c";
    l[5] = "b";

    //調用lower_bound之前必須確定序列爲有序序列,
    //lower_bound(key) 返回一個 iterator 指向指向鍵值>= key的第一個元素
    //upper_bound(key) 返回一個 iterator,指向鍵值> key的第一個元素
    ite1 = l.lower_bound(5);
    cout << ite1 -> first << endl;//輸出5
    ite1 = l.upper_bound(5);
    cout << ite1 -> first << endl;//輸出7
    //所以若 lower_bound(key) == upper_bound(key)則不存在key元素
    pair<map<int,string>::iterator,map<int ,string>::iterator> mapPair;
    mapPair = l.equal_range(3);//返回值即爲pair<lower_bound(key),upper_bound(key)>
    if(mapPair.first == mapPair.second)
    {
        cout <<"do not find"<< endl;
    }
    else
    {
        cout << "find" << endl;
    }


///清空、判空
    l.clear();
    if(l.empty())
        cout << "empty!"<<endl;
    else
        cout << "clear fail"<<endl;
///刪除
    l[1] = "example";
    ite1 = l.find(1);
    l.erase(ite1);

    l[1] = "example";
    int nFlag = l.erase(1);//只有形參爲主鍵值時erase函數纔有返回值
    cout << nFlag << endl;//1代表刪除,否則返回0

    l[1] = "example";
    l.erase(l.begin(),l.end());//前閉後開

///排序(默認從小到大)
    //結構體排序(必須重載小於運算符,不重載insert函數編譯不會通過)

    int nsize;
    map<Stu,int>mapStu;
    map<Stu,int>::iterator ite2;
    Stu sStu;
    sStu.ID = 1;
    sStu.strName = "stu_one";
    mapStu.insert(pair<Stu,int>(sStu,95));
    sStu.ID = 1;
    sStu.strName = "stu_two";
    mapStu.insert(pair<Stu,int>(sStu,80));
    for(ite2 = mapStu.begin();ite2 != mapStu.end();ite2++)
        cout << ite2->first.strName << " " << ite2->second << endl;//注意指針操作符和結構體成員運算符的區別

    return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章