B00010 map

这是一个有关map的例子程序,代码来自:std::map::emplace - cppreference.com

源程序如下:

/* B00010 map */

#include <iostream>
#include <utility>
#include <string>
#include <map>

using namespace std;

int main()
{
    std::map<std::string, std::string> m;

    // uses pair's move constructor
    m.emplace(std::make_pair(std::string("a"), std::string("a")));

    // uses pair's converting move constructor
    m.emplace(std::make_pair("b", "abcd"));

    // uses pair's template constructor
    m.emplace("d", "ddd");

    // uses pair's piecewise constructor
    m.emplace(std::piecewise_construct,
              std::forward_as_tuple("c"),
              std::forward_as_tuple(10, 'c'));
    // as of C++17, m.try_emplace("c", 10, 'c'); can be used

    for (const auto &p : m) {
        std::cout << p.first << " => " << p.second << '
';
    }
}

运行结果如下:

a => a
b => abcd
c => cccccccccc
d => ddd


原文地址:https://www.cnblogs.com/tigerisland/p/7564745.html