【问题标题】:map<string, vector<string>> reassignment of vector valuemap<string, vector<string>> 向量值的重新赋值
【发布时间】:2013-11-04 23:59:46
【问题描述】:

我正在尝试编写一个程序,该程序从输入文件中获取行,将行排序为“签名”,以便将所有相互变位的单词组合在一起。我必须使用映射,将“签名”存储为键,并将与这些签名匹配的所有单词存储到字符串向量中。之后,我必须在同一行上打印所有相互变位的单词。这是我目前所拥有的:

#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <fstream>

using namespace std;

string signature(const string&);
void printMap(const map<string, vector<string>>&);

int main(){
string w1,sig1;
vector<string> data;
map<string, vector<string>> anagrams;
map<string, vector<string>>::iterator it;
ifstream myfile;
myfile.open("words.txt");

while(getline(myfile, w1))
{
  sig1=signature(w1);        
  anagrams[sig1]=data.push_back(w1); //to my understanding this should always work,
}                                    //either by inserting a new element/key or
                                     //by pushing back the new word into the vector<string> data
                                     //variable at index sig1, being told that the assignment operator
                                     //cannot be used in this way with these data types
myfile.close();

printMap(anagrams);

return 0;
}

string signature(const string& w)
{
string sig;
sig=sort(w.begin(), w.end());
return sig;
}

void printMap(const map& m)
{
for(string s : m)
{
  for(int i=0;i<m->second.size();i++)
     cout << m->second.at();
  cout << endl;
}
}

第一个解释有效,没想到这么简单!但是现在我的打印功能给了我: prob2.cc: In function âvoid printMap(const std::map<std::basic_string<char>, std::vector<std::basic_string<char> > >&)â: prob2.cc:43:36: error: cannot bind âstd::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}â lvalue to âstd::basic_ostream<char>&&â In file included from /opt/centos/devtoolset-1.1/root/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/iostream:40:0, 尝试了很多变化,他们总是抱怨绑定

void printMap(const map<string, vector<string>> &mymap)
{
for(auto &c : mymap)
  cout << c.first << endl << c.second << endl;
}

【问题讨论】:

  • 问题是什么?
  • 你为什么不直接使用std::multimap
  • 原始问题:为什么 anagrams[sig1]=data.push_back(w1) 没有编译。

标签: c++ vector map assign


【解决方案1】:

anagrams[sig1] 将返回对vector&lt;string&gt; 的引用。与其分配给它,不如将push_back 分配给它。

sig1 = signature(w1);
anagrams[sig1].push_back(w1);

由于您的代码是现在编写的,它试图替换向量而不是添加到它。例如,假设您的输入包含wassaw,并且您的signature 对字符串的字母进行排序。

这个案例你想要的是:

  1. 读作“曾经”
  2. 排序得到“asw”
  3. 插入“是”得到:anagrams["asw"] -&gt; ["was"]
  4. 读“锯”
  5. 排序得到“asw”(再次)
  6. 插入“锯”得到:anagrams["asw"] -&gt; ["was", "saw"]

使用您尝试编写的代码,但是,在第 6 步中,您不会添加到现有向量,而是使用仅包含“saw”的新向量覆盖当前向量,因此结果将只是anagrams["asw"] -&gt; ["saw"]

printmap而言:地图中的项目不是std::strings,而是std::pair&lt;std::string, std::vector&lt;std::string&gt;&gt;,所以当你尝试这样做时:

void printMap(const map& m)
{
    for(string s : m)

...这显然行不通。我通常会使用:

for (auto s : m) 

...这使得编译至少这么多内容变得容易。但是,要对s 做任何有用的事情,您需要意识到它是一个pair,因此您必须使用s.firsts.second(而s.first 将是一个strings.second 将是 std::vector&lt;std::string&gt;)。要将它们打印出来,您可能需要打印 s.first,然后是一些分隔符,然后遍历 s.second 中的项目。

【讨论】:

  • 知道了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-04
  • 1970-01-01
  • 2020-02-26
  • 1970-01-01
相关资源
最近更新 更多