尝试使用以下
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <utility>
int main()
{
typedef std::pair<std::string, int> value_type;
std::multimap<std::string, std::vector<value_type>> m;
m.insert({ "A", std::vector<value_type>(1, { "A", 'A' }) });
return 0;
}
或者另一示例
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <utility>
int main()
{
typedef std::pair<std::string, int> value_type;
std::multimap<std::string, std::vector<value_type>> m;
auto it = m.insert({ "A", std::vector<value_type>(1, { "A", 'A' }) });
for (char c = 'B'; c <= 'Z'; ++c)
{
const char s[] = { c, '\0' };
it->second.push_back({ s, c });
}
size_t i = 0;
for (const auto &p : it->second)
{
std::cout << "{" << p.first << ", " << p.second << "} ";
if (++i % 7 == 0) std::cout << std::endl;
}
std::cout << std::endl;
return 0;
}
输出是
{A, 65} {B, 66} {C, 67} {D, 68} {E, 69} {F, 70} {G, 71}
{H, 72} {I, 73} {J, 74} {K, 75} {L, 76} {M, 77} {N, 78}
{O, 79} {P, 80} {Q, 81} {R, 82} {S, 83} {T, 84} {U, 85}
{V, 86} {W, 87} {X, 88} {Y, 89} {Z, 90}
用['的std :: make_pair'](HTTP://en.cppreference .com/w/cpp/utility/pair/make_pair)以避免键入和拼写错误的冗余类型信息。 – Csq 2014-09-28 12:43:59
@Csq我添加了评论,我跳过所有std ::简化代码一点。无论如何感谢您的提示 – user3119781 2014-09-28 12:59:03
不工作......怎么样?错误?警告?崩溃?不要让我们陷入悬念。 – 2014-09-28 13:06:41