0
点赞
收藏
分享

微信扫一扫

C++ 容器浅析<二>

十里一走马 2021-09-28 阅读 53
技术

map容器

  • 键值对容器
#include "map"
#include "iostream"

using namespace std;

map<string, int> mapVal;
mapVal.insert(pair<string, int>("一", 1));//key , value

//遍历
for (map<string, int>::iterator iter = mapVal.begin(); iter != mapVal.end(); iter++) {
        cout << iter->first.c_str() << endl;
        cout << iter->second << endl;
    }

//插入结果【成功、失败】
const pair<map<string, int>::iterator, bool> result = mapVal.insert(pair<string, int>("二", 2));

if (result.second) {
        cout << "插入成功" << endl;
    } else {
        cout << "插入失败" << endl;
    }

//遍历
    for (map<string, int>::iterator it = mapVal.begin(); it != mapVal.end(); it++) {
        cout << it->first.c_str() << "|" << it->second << endl;
    }

//查找,操作 find
const map<string, int>::iterator findResult = mapVal.find("三");
if (findResult != mapVal.end()) {
        cout << "找到了   " << findResult->first.c_str() << findResult->second << endl;
    } else {
        cout << "没找到" << endl;
    }

multimap容器

//
// Created by lzl on 2021/4/1.
//
#include "map"
#include "iostream"

using namespace std;

int main() {
    // 1.key可以重复,
    // 2.key重复的数据可以分组,
    // 3.key会排序,
    // 4.value不会排序
    multimap<int, string> multimapVal;
    multimapVal.insert(make_pair(20, "二十个"));
    multimapVal.insert(make_pair(10, "十个"));
    multimapVal.insert(make_pair(10, "十个"));
    multimapVal.insert(make_pair(30, "三十个"));

//    auto关键字用于两种情况:声明变量时根据初始化表达式自动推断该变量的类型、声明函数时函数返回值的占位符。

    for (multimap<int, string>::iterator iterator = multimapVal.begin();
         iterator != multimapVal.end();
         iterator++) {
        cout << iterator->first << " value::" << iterator->second.c_str() << endl;
    }

    //核心功能是分组
//    int result;
//    cout << "请输入你想要查询的key,为int类型:" << endl;
//    cin >> result;
//
//    const multimap<int, string>::iterator resultMutilMap = multimapVal.find(result);
//    if (resultMutilMap != multimapVal.end()) {
//        cout << "找到了" << resultMutilMap->first << resultMutilMap->second << endl;
//    } else {
//        cout << "没找到" << endl;
//    }

    int result;
    do {
        cout << "请输入你想要查询的key,为int类型:" << endl;
        cin >> result;
        multimap<int, string>::iterator resultMutilMap = multimapVal.find(result);
        if (resultMutilMap != multimapVal.end()) {
            cout << "找到了结果为:  " << "key:" << resultMutilMap->first << "  value:" << resultMutilMap->second << endl;
            //因为有分组 需要把find 的结果遍历
            while (resultMutilMap != multimapVal.end() && resultMutilMap->first == result) {
                cout << "key:" << resultMutilMap->first << "  value:" << resultMutilMap->second << endl;
                resultMutilMap++;
                if (resultMutilMap == multimapVal.end()) {
                    break;
                }
            }

            break;
        } else {
            cout << "没找到" << endl;
        }
    } while (multimapVal.find(result) == multimapVal.end());

    return 0;
}

举报

相关推荐

C++ 容器浅析

C++容器

C/C++ -容器map

c++ 顺序容器

C++ vector 容器

C++——STL容器

C++ 容器操作

C++——顺序容器

c++ vector容器

0 条评论