【问题标题】:mapping a vector of strings to integer将字符串向量映射到整数
【发布时间】:2014-10-04 07:07:38
【问题描述】:

我正在使用这些数据结构

map < vector < string > , int > hash;
vector < string > element;

在我创建了元素向量之后。 当我尝试使用此数据结构时-

if(hash[element].count()==0) // line 1
hash.insert(pair< <vector<string>,int>(element,1)); // line 2

我收到以下错误-

第 1 行:

[Error] 请求hash.std::map<_Key, _Tp, _Compare, _Alloc>::operator[]<std::vector<std::basic_string<char> >, int, std::less<std::vector<std::basic_string<char> > >, std::allocator<std::pair<const std::vector<std::basic_string<char> >, int> > >((*(const key_type*)(& element))) 中的成员count,这是非类类型 std::map<std::vector<std::basic_string<char> >, int>::mapped_type {aka int}

第 2 行:

模板参数的数量错误(1,应该是 2)

【问题讨论】:

  • 切换参数:map&lt;int, vector&lt;string&gt;&gt; hash; 这意味着..整数将用作键..字符串向量将是该键的值。我怀疑您是否打算使用字符串向量作为键。如果你这样做了,那就完全无视我吧。
  • 计算第 2 行的括号 - 有一个多余的左括号。
  • 认为你想要:hash.insert(pair&lt;vector&lt;string&gt;,int&gt;(element,1)); 我同意它看起来像是一个非常不寻常的键。

标签: c++ string vector stl


【解决方案1】:

线

if(hash[element].count()==0) // line 1

需要:

if(hash.count(element)==0) // line 1

线

hash.insert(pair< <vector<string>,int>(element,1)); // line 2
// Remove the   ^^^ Extra <

需要:

hash.insert(pair<vector<string>,int>(element,1)); // line 2

或者,更好:

hash.emplace(element,1); 

【讨论】:

  • 非常感谢它的工作!!
  • @ChrisDrew,感谢您的支持。是的,emplace 也可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多