c++ - 将 3 个变量值插入到 multimap 中

标签 c++ dictionary syntax insert multimap

我是 map 和多重映射的新手,在尝试通过使用一对字符串(作为键)将 3 个值插入多重映射时遇到了问题(编译器错误和警告)和一个 int 值:

这是我的 multimap 声明:

multimap<pair<string, string>, int> wordpairs;

这就是我尝试填充 multimap 的方式:

int toInsert = 0;

  for (int i = 0; i < s; i++) {

    wordpairs.insert((words[i], words[i+1]), toInsert);

  }

单词在哪里:

vector<string> words

我收到此错误和一堆警告:

error: no matching function for call to ‘std::multimap<std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, int>::insert(std::__cxx11::basic_string<char>&, int&)’
     wordpairs.insert((words[i], words[i+1]), toInsert);
                                                      ^

不确定如何正确插入我想要的值。 :(

最佳答案

您的 key 是一对 ( pair<string, string> ) 和 (words[i], words[i+j])不是一对。你需要 wordpairs.emplace(std::make_pair(words[i], words[i+j]), toInsert)

编辑:有两种方法可以将某些东西放入 map (或多 map )中。第一个是插入,它需要一个对象复制到您的 map 中。您的 map 包含一对 < pair< string,string >, int >。所以你可以像这样调用插入... wordpairs.insert(std::make_pair(std::make_pair(words[i], words[i+j]), toInsert))或者你可以放置它们。 Emplace 就地构造对象,因此不用 make_pair 构造它。然后将其复制到 map 中,您可以通过给定的调用就地构建它。

关于c++ - 将 3 个变量值插入到 multimap 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40410771/

相关文章:

php - 如何在 php 扩展中包装 c++ 类?

pandas - python : Convert entire column to dictionary

python - 如何制作一个以 pandas 数据帧列表作为值的字典?

c++ - 将 boost::qi::rule 与 BOOST_FUSION_ADAPT_STRUCT 一起使用的正确方法是什么?

c++ - gdb 错误不是可执行格式 : File format not recognized

c - 简单的 C 程序 - 需要帮助

c++ - 需要帮助找出适合我的数据结构的语法

php - 语法错误,意外 'endforeach'

c++ - 使用 const cast 将 std 字符串转换为 char*

c++ - 如何更改 map 容器的内部排序方案?