C++ map常用操作及迭代器

1.返回迭代器

map_date.begin();

map_date.end();

map_date.find(find_date);

 

#include <iostream>
#include <vector>
#include <string>
#include <map>

using std::string;
using std::vector;
using std::map;


void vector_test()
{
    vector<unsigned char> Vec1;
    string Str{"123456"};

    Vec1.assign(Str.begin(), Str.end());
    std::cout << "String data is: " << std::endl;
    for(auto aa : Str)
        printf("%02x ", aa);
    std::cout << std::endl;
    std::cout << "Vector data is: " << std::endl;
    for(auto aa : Vec1)
        printf("%02x ", aa);
    std::cout << std::endl;
}

enum class enumResult
{
    NONE,
    SUCCESS,
    ERROR,
};
typedef enumResult (*pFun)(int);

enumResult mapTest_func1(int id)
{
    std::cout << "enter: " << __FUNCTION__ << std::endl;
    return enumResult::SUCCESS;
}

enumResult mapTest_func2(int id)
{
    std::cout << "enter: " << __FUNCTION__ << std::endl;
    return enumResult::SUCCESS;
}

void map_test(int num)
{
    map<int, pFun> map1 = 
    {
        {1, mapTest_func1},
        {2, mapTest_func2},
    };
    auto iterator_date = map1.find(num);
    iterator_date->second(iterator_date->first);
}

int main()
{
    vector_test();
    while(1)
    {
        int num = 0;
        std::cout << "**************test_menu*************" <<std::endl;
        std::cout << "***1.function1                   ***" <<std::endl;
        std::cout << "***2.function2                   ***" <<std::endl;
        std::cin >> num;
        if(1 != num && 2 != num)
        {
            std::cout << "num is error, please input anain!" << std::endl;
            continue;
        }
        map_test(num);
    }
}

posted @ 2020-06-11 20:31  生而为人,学无止境  阅读(3018)  评论(0编辑  收藏  举报