天天看點

【C++程式設計語言】C++的 Map和Multimap容器

作者:煩人的星辰

#挑戰30天在頭條寫日記#

1.map容器基本概念

簡介:

  • map中所有元素都是pair
  • pair中第一個為key(鍵值),起到索引作用,第二個為value(實值)
  • 所有元素都會根據元素的key值自動排序

本質:

  • map/multimap屬于關聯式容器,底層結構是用二叉樹實作

優點:

  • 可以根據key值快速找到value值

map/multimap差別:

  • map不允許容器中有重複key值元素
  • multimap允許容器中有重複key值元素

2.map容器的構造和指派

功能描述:

對map容器進行構造和指派操作

注意:map中所有元素都是成對出現,插入資料時候要使用對組

/*
    函數原型
        構造
            map<T1,T2> mp  map 預設構造函數
            map(const map &mp)  拷貝構造函數
        指派
            map& operator = (const map &mp) 重載等号操作符
*/
void printMap(map<int, int>& m) {
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
        cout << "key = " << (*it).first << "value = " << (*it).second << endl;
    }
    cout << endl;
}
//map容器 構造和指派
void test01() {

    //預設構造
    map<int, int> m;
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(4, 40));
    printMap(m);

    //拷貝構造
    map<int, int>m2(m);
    printMap(m2);

    //指派
    map<int, int>m3;
    m3 = m2;
    printMap(m3);

}
int main() {
    test01();
    system("pause");
    return 0;
}           

3.map容器大小和交換

功能描述:

統計map容器大小以及交換map容器

/*
    函數原型
        size()   傳回容器中元素數目
        empty()  判斷容器是否為空
        swap(st)   交換兩個集合容器
*/
void printMap(map<int, int>& m) {
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
        cout << "key = " << (*it).first << "value = " << (*it).second << endl;
    }
    cout << endl;
}
//map容器 構造和指派
void test01() {

    //構造
    map<int, int> m;

    //插入資料
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(4, 40));
    
    //map 的為空  和  大小判斷
    if (m.empty()) {
        cout << "m為空" << endl;
    }else {
        cout << "m不為空" << endl;
        cout<<  "m的大小為:"<< m.size() << endl;
    }

    //map交換
    map<int, int>m2;
    m2.insert(pair<int, int>(5, 50));
    m2.insert(pair<int, int>(6, 60));
    m2.insert(pair<int, int>(7, 70));
    m2.insert(pair<int, int>(8, 80));

    cout << "交換前:" << endl;
    printMap(m);
    printMap(m2);

    cout << "交換後:" << endl;
    m.swap(m2);
    printMap(m);
    printMap(m2);

}
int main() {
    test01();
    system("pause");
    return 0;
}           

4.map插入和删除

功能描述:map容器進行插入資料和删除資料

/*
    函數原型
        insert(elem)  在容器中插入元素
        clear()     清除所有元素
        erase(pos)    删除pos疊代器所指的元素,傳回下一個元素的疊代器
        erase(beg,end) 删除區間[beg,end]的所有元素,傳回下一個元素的疊代器
        erase(key)  删除容器中值為key的元素
*/
void printMap(map<int, int>& m) {
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
        cout << "key = " << (*it).first << "value = " << (*it).second << endl;
    }
    cout << endl;
}
//map容器 構造和指派
void test01() {

    //構造
    map<int, int> m;

    //插入資料
    //第一種
    m.insert(pair<int, int>(1, 10));
    
    //第二種
    m.insert(make_pair(2, 20));

    //第三種
    m.insert(map<int,int>::value_type(3,30));

    //第四種
    //[]不建議使用,用途是通過key通路到value
    m[4] = 40;
    
    printMap(m);

    //删除
    m.erase(m.begin());
    printMap(m);

    m.erase(3);//删除key值為3 的;
    //清空
    m.clear();
    m.erase(m.begin(), m.end());

}
int main() {
    test01();
    system("pause");
    return 0;
}           

5.map查找和統計

功能描述:對map容器進行查找資料以及統計資料

/*
    函數原型
        find(key)  查找key是否存在,若存在,傳回該鍵的元素的疊代器,若不存在,傳回set.end();
        count(key) 統計key元素的個數
*/
void printMap(map<int, int>& m) {
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
        cout << "key = " << (*it).first << "value = " << (*it).second << endl;
    }
    cout << endl;
}
//map容器 構造和指派
void test01() {

    //構造
    map<int, int> m;

    //插入資料
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(4, 40));

    //查找
    map<int, int>::iterator pos = m.find(3);
    if (pos != m.end()) {
        cout << "查找到了元素 key = " << (*pos).first << " value = " << pos->second << endl;
    }
    else {
        cout << "未找到元素" << endl;
    }

    //統計
    //map不允許插入重複可以元素  是以傳回值隻有0和1
    int num = m.count(3);
    cout << "num = " << num << endl;
}
int main() {
    test01();
    system("pause");
    return 0;
}           

6.map容器排序

class MyCompare {
public:
    bool operator()(int v1,int v2) const  {
        return v1 > v2;
    }
};

void test01() {

    //構造  并修改排序規則
    map<int, int, MyCompare> m;

    //插入資料
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(4, 40));

    for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
        cout << "key = " << (*it).first << "value = " << (*it).second << endl;
    }
    cout << endl;
}
int main() {
    test01();
    system("pause");
    return 0;
}           

繼續閱讀