【问题标题】:How to insert value in c++ map std::map<string , list<string> in c++?如何在 C++ 中的 C++ 映射 std::map<string, list<string> 中插入值?
【发布时间】:2015-12-30 09:32:57
【问题描述】:

我正在尝试在映射中插入一个值,其中映射的键是字符串,值是列表。 当我尝试插入时,我得到了错误。

#include <iostream>
#include <utility>
#include <vector>
#include <map>
#include <string>
using namespace std;
main()
{
     string key = "myKey";
     string str1 = "str1";

     map<string, list<string>> myMap;
     myMap.insert( make_pair (key, str1));

 }

错误

错误 2 错误 C2664: 'std::pair<_ty1> std::_Tree<_traits>::insert(std::pair &&)' : 无法将参数 1 从 'std::pair<_ty1>' 转换为 'std::pair<_ty1> &&'

感谢您的帮助!

【问题讨论】:

  • 注意std::map 在 C++11 之前已经引入
  • 为什么不使用multimapunordered_multimap

标签: list c++11 dictionary std-pair


【解决方案1】:

你有一个std::map,它接受一个字符串和一个列表作为值的键。 您正在尝试向它传递一个作为字符串的键和一个作为值的字符串,这就是问题所在。

main()
{
     string key = "myKey";
     string str1 = "str1";
     list<string> l;

     l.push_back( str1 );

     map<string, list<string>> myMap;
     myMap.insert( make_pair (key, l)); // pass a list here

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 2019-11-27
    • 2012-06-17
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多