【问题标题】:map,vector in c++ [duplicate]地图,C++中的矢量[重复]
【发布时间】:2021-02-17 07:37:02
【问题描述】:

错误:'operator')

我想要相同的键和多个值,例如键是 10 值是 2,3,4 但是“*iter”是错误的.. 如何在 C++ 中计算地图、向量?

【问题讨论】:

  • Here 是一个不错的模板版本。
  • 你需要第二个循环来输出iter->second中的向量。
  • 你为什么要编辑你的代码?

标签: c++ dictionary for-loop vector iterator


【解决方案1】:

在您的代码 sn-p 中,表达式 *iter 的值是 std::pair<std::string, std::vector<int>> 类型的对象,未定义运算符

还有错误提示

error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and
 ‘std::pair<const std::__cxx11::basic_string, std::vector >’)

说到这个。

最简单的方法是使用基于范围的for循环。

这是一个演示程序。

#include <iostream>
#include <string>
#include <vector>
#include <map>

int main() 
{
    std::map<std::string, std::vector<int>> m;
    
    m["10"].assign( { 2, 3, 4 } );
    
    for ( const auto &p : m )
    {
        std::cout << p.first << ": ";

        for ( const auto &item : p.second )
        {
            std::cout << item << ' ';
        }

        std::cout << '\n';
    }
    return 0;
}

程序输出是

10: 2 3 4 

如果你想使用迭代器编写普通的 for 循环,那么循环可以如下所示。

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iterator>

int main() 
{
    std::map<std::string, std::vector<int>> m;
    
    m["10"].assign( { 2, 3, 4 } );
    
    for ( auto outer_it = std::begin( m ); outer_it != std::end( m ); ++outer_it )
    {
        std::cout << outer_it->first << ": ";

        for ( auto inner_it = std::begin( outer_it->second ); 
             inner_it != std::end( outer_it->second );
             ++inner_it )
        {
            std::cout << *inner_it << ' ';
        }

        std::cout << '\n';
    }
    return 0;
}

程序输出又是

10: 2 3 4 

【讨论】:

    【解决方案2】:

    我建议使用结构化绑定和基于范围的 for 循环:

    std::map<std::string,std::vector<int>> m;
    
    for (auto&[str, vec] : m) { // bind str to "first" in the pair and vec to "second"
        std::cout << str << ':';
        for(auto lineno : vec) std::cout << ' ' << lineno;
        std::cout << '\n';
    }
    

    【讨论】:

      【解决方案3】:

      您可以通过std::ostream 定义如何打印内容,如下所示:

      #include <iostream>
      #include <map>
      #include <vector>
      #include <string>
      
      // define how to print std::pair<std::string, std::vector<int>>
      std::ostream& operator<<(std::ostream& stream, const std::pair<std::string, std::vector<int>>& pair) {
          stream << "(" << pair.first << ", {";
          bool first = true;
          for (int e : pair.second) {
              if (!first) stream << ", ";
              stream << e;
              first = false;
          }
          stream << "})";
          return stream;
      }
      
      int main(void) {
          std::string yytext = "hoge";
          int lineno = 42;
      
          // below is copied from the question
      
          std::map<std::string,std::vector<int>> m; 
          m[yytext].push_back(lineno);
      
          std::map<std::string,std::vector<int>>::iterator iter;
      
          for (iter=m.begin(); iter!=m.end(); iter++){
              std::cout<<iter->first<<":"<<*iter<<std::endl;}
      }
      

      【讨论】:

        猜你喜欢
        • 2010-10-15
        • 2011-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多