一种打印多地图地图的方法

A way to print of map of multimaps?

本文关键字:地图 方法 打印 一种      更新时间:2023-10-16

我试图超载operator<<,这让我发疯:

std::ostream& operator<<(std::ostream & lhs, TuringMachine::TRTable& rhs){
    for(auto& statePtr : rhs){
        lhs << statePtr.first->getLabel().toStdString();
        for(auto& charPtr: statePtr.second){
            //lhs << 't';
            lhs << charPtr.first.toAscii() ;
            //lhs << 'b ';
            lhs << charPtr.second.getState().getLabel().toStdString() << std::endl;
        }
    }
return lhs;
}

TRTablestd::map<State*, std::multimap<QChar, Transition>>typedefState的标签是QString因此呼吁.toStdString() .

在另一堂课上,我称std::cout << machine->table << std::endl;machineTuringMachine*,这给了我

error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

我做错了什么?为什么&&

编辑:使用g ++ 4.6和-std=c++0x

您在哪个命名空间中声明了operator<<?由于TRTable是一个 typedef ADL 不适用,因此 ADL 仅在namespace std中搜索operator<<,因为这是定义实际类的地方。因此,当您想要使用它时,您可能必须use在其中定义operator<<的命名空间。

lhs应该有类型 std::ostream & 。没有const.

rhs应该const TuringMachine::TRTable&

std::ostream& operator<<(std::ostream& lhs, const TuringMachine::TRTable& rhs)