【问题标题】:c++ loop std::vector<std::map<std::string, std::string> >c++ 循环 std::vector<std::map<std::string, std::string>>
【发布时间】:2015-02-12 05:37:58
【问题描述】:

如何循环这个?

我已经试过了:

//----- code
std::vector<std::map<std::string, std::string> >::iterator it;
for ( it = users.begin(); it != users.end(); it++ ) {
    std::cout << *it << std::endl; // this is the only part i changed according to the codes below
}
//----- error
error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::map<std::basic_string<char>, std::basic_string<char> >]’

//----- code
std::cout << *it["username"] << std::endl;
//----- error
note:   template argument deduction/substitution failed:
note:   ‘std::map<std::basic_string<char>, std::basic_string<char> >’ is not derived from ‘const std::complex<_Tp>’
//----- code
std::cout << *it->second << std::endl; // also tried with parenthesis - second()
//----- error
error: ‘class std::map<std::basic_string<char>, std::basic_string<char> >’ has no member named ‘second’
//----- code
for( const auto& curr : it ) std::cout <<  curr.first() << " = " << curr.second() << std::endl;
//----- error
error: unable to deduce ‘const auto&’ from ‘<expression error>’

最后

//----- code
std::map<std::string, std::string>::iterator curr, end;
for(curr = it.begin(), end = it.end();  curr != end;  ++curr) {
    std::cout <<  curr->first << " = " << curr->second << std::endl;
}
//----- error
‘std::vector<std::map<std::basic_string<char>, std::basic_string<char> > >::iterator’ has no member named ‘begin‘ & ‘end’

我希望我给出一个清晰的细节..上面是代码然后下面是错误..目前我的大脑是空白的。

对此感到抱歉..

我已经使它适用于这种类型:std::map&lt;int, std::map&lt;std::string, std::string&gt; &gt;,并且我正在尝试使用矢量作为选项。

【问题讨论】:

    标签: c++ arrays dictionary multidimensional-array vector


    【解决方案1】:

    您的迭代代码是正确的;问题是你的输出语句。您的代码正在这样做:

    std::cout << *it << std::endl;
    

    在这种情况下,*it 指的是std::map&lt;string,string&gt;,而std::cout 不知道如何输出地图。也许你想要这样的东西:

    std::cout << (*it)["username"] << std::endl;
    

    确保在 *it 周围使用 (),否则您将遇到运算符优先级问题。

    【讨论】:

      【解决方案2】:
      std::vector<std::map<std::string, std::string> >::iterator it;
      for ( it = users.begin(); it != users.end(); it++ ) {
          std::cout << *it << std::endl;
      

      users 不是.empty() 时,上面的&lt;&lt; 运算符会尝试流式传输std::map&lt;std::string, std::string&gt; 对象,但标准库没有为流映射提供重载:它如何知道您使用什么分隔符需要在键和值之间以及元素之间?

      我建议你把问题分解成这样:

      std::vector<std::map<std::string, std::string> >::iterator it;
      for ( it = users.begin(); it != users.end(); it++ )
      {
          std::map<std::string, std::string>& m = *it;
      
          for (std::map<std::string, std::string>::iterator mit = m.begin();
               mit != m.end(); ++mit)
              std::cout << mit->first << '=' << mit->second << '\n';
      
          std::cout << "again, username is " << m["username"] << '\n';
      }
      

      这可以在 C++11 中简化:

      for (auto& m : users)
          for (auto& kv : m)
              std::cout << kv.first << '=' << kv.second << '\n';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-17
        • 1970-01-01
        • 2021-08-13
        • 2020-07-14
        • 1970-01-01
        • 2011-10-26
        • 2017-09-21
        相关资源
        最近更新 更多