【问题标题】:C++" no match for operator<<" when printing a std::vector<std::pair>C++" 在打印 std::vector<std::pair> 时与 operator<<" 不匹配
【发布时间】:2021-01-27 00:56:18
【问题描述】:

我想打印向量 dicVec 以检查一切是否如我所愿。但我不能。我已经查过了,但没有任何帮助。

这是我当前的代码:

void Translate::read(){
    int i = 0;

    std::ifstream dic("ende.dic");
    if (dic.is_open())
    {
        std::string dicLine;
        while (std::getline(dic, dicLine)){
            std::vector<std::pair<std::string, std::string>> dicVec;
            std::pair<std::string, std::string> dicPair;
            std::size_t pos = dicLine.find(";");
            dicPair.first = dicLine.substr(0, pos);
            //std::cout << dicPair.first << " : ";
            dicPair.second= dicLine.substr(pos+1);
            //std::cout << dicPair.second << std::endl;
            std::cout << dicVec[i];
            i++;
        }
    }
    else {
        throw std::runtime_error("File could not be opened");
    }
}

我也试过用这个来打印它:

std::cout << dicVec.at(i);

还有这个:

for (auto i = dicVec.begin(); i != dicVec.end(); ++i)
    std::cout << *i << ' ';

但总是有这个错误:

no match for operator<<

我必须做什么才能打印我的矢量? 感谢您的帮助。

【问题讨论】:

    标签: c++ std stdvector cout std-pair


    【解决方案1】:

    您正在尝试打印出std::pair,但没有为std::pair 实现的标准operator&lt;&lt;,因此您必须自己编写。

    即使你修复了这个问题,你的代码仍然会失败,因为当你调用std::cout &lt;&lt; dicVec[i];dicVec 是空的,你没有在打印dicVec 的内容之前将dicPair 添加到dicVec。事实上,dicVec 应该声明在循环的上方,而不是内部

    试试这样的:

    typedef std::pair<std::string, std::string> stringPair;
    typedef std::vector<stringPair> stringPairVec;
    
    std::ostream& operator<<(std::ostream &out, const stringPair &p) {
        out << p.first << " : " << p.second << "\n";
        return out;
    }
    
    std:ostream& operator<<(std::ostream &out, const stringPairVec &vec) {
        for (size_t i = 0; i < vec.size(); ++i) {
            out << vec[i];
        }
        return out;
    }
    
    void Translate::read(){
    
        std::ifstream dic("ende.dic");
        if (!dic.is_open()) {
            throw std::runtime_error("File could not be opened");
        }
    
        stringPairVec dicVec;
        std::string dicLine;
    
        while (std::getline(dic, dicLine)) {
            std::size_t pos = dicLine.find(";");
            stringPair dicPair = std::make_pair(
                dicLine.substr(0, pos),
                dicLine.substr(pos+1)
            );
            dicVec.push_back(dicPair);
        }
    
        std::cout << dicVec;
    }
    

    或者,在 C++11 及更高版本中,您可以改为这样做:

    using stringPair = std::pair<std::string, std::string>;
    using stringPairVec = std::vector<stringPair>;
    
    std::ostream& operator<<(std::ostream &out, const stringPair &p) {
        out << p.first << " : " << p.second << "\n";
        return out;
    }
    
    std:ostream& operator<<(std::ostream &out, const stringPairVec &vec) {
        for (const auto &p : vec) {
            out << p;
        }
        return out;
    }
    
    void Translate::read(){
    
        std::ifstream dic("ende.dic");
        if (!dic.is_open()) {
            throw std::runtime_error("File could not be opened");
        }
    
        stringPairVec dicVec;
        std::string dicLine;
    
        while (std::getline(dic, dicLine)) {
            std::size_t pos = dicLine.find(";");
            dicVec.emplace_back(
                dicLine.substr(0, pos),
                dicLine.substr(pos+1)
            );
        }
    
        std::cout << dicVec;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 2023-02-16
      • 1970-01-01
      • 2011-10-29
      • 2013-08-18
      • 2013-03-08
      • 2017-11-25
      相关资源
      最近更新 更多