【问题标题】:printing a struct with a map in it打印一个带有地图的结构
【发布时间】:2014-04-20 22:52:01
【问题描述】:

好的,我无法打印结构列表中的值。打印列表(使用迭代器)的正常方式不起作用。这是我将文件读入列表的代码。

struct Edge{
    map<char, string> edge;
    int weight= -1;

    Edge(){};
    Edge(char v, string e, int w){
      edge.insert(pair<char,string>(v,e));
      weight = w;
    }
};

int main(){
     list<Edge> edges;

        //Read in file//
string line;
char start;
vector<string> tokens;

if(!file.is_open()){
    cout<<"Could not open file\n";
}else{
    while(getline(file,line)){
        tokens.clear();
        start = line.front();
        istringstream iss(line);
        copy(istream_iterator<string>(iss), 
            istream_iterator<string>(),
            back_inserter<vector<string>>(tokens));
        for(int i = 1; i<tokens.size(); i+=2){
            //cout << "Position: " <<i << " = " << tokens.at(i) <<endl;
            //insert into edges list.
                Edge e = Edge(start, tokens.at(i), stoi(tokens.at(i+1)));
                edges.push_back(e);
        }//end for
    }//end while
}//end if-else

 return 0;
}//end main

向量标记被正确读入。我用注释掉的 cout 检查了它。

该文件是一个图形文件,第一个元素是起始顶点,该行的其余部分使用边的结束顶点和边的权重进行格式化。

例如:

1 2 3 4 5

表示边 (1,2) 的权重为 3,边 (1,4) 的权重为 5。

我不知道我是否正确阅读了我的列表,因为我不知道如何打印出来。我将如何打印出我的列表边缘?

或者有没有更好的方法来设置我的结构?或许是另一个结构体作为边缘,然后是一个结构体与边缘和权重?

尝试打印,但无法正常工作。语法甚至不起作用。 :(

打印列表的常规方式。但不喜欢它,因为我有一个结构列表。

    list<Edge>::iterator it;
    for(it=edges.begin(); it!=edges.end(); it++){
    cout << *it <<endl;
}//end for

这就是我在搜索时发现的。这就是我发现的。 C++ How to loop through a list of structs and access their properties 这是我的尝试。

//inside main
    list<Edge>::iterator it;
for(int i = 0; i<edges.size(); i++){
    for_each(edges.begin(), edges.end(), printEdges);
}//end for

//outside main
    void printEdges(Edge &data){
    cout << "( " << data.edge << " )"
     << ": Weight = " << data.weight <<endl;
    }//end printEdges

谢谢。

【问题讨论】:

    标签: c++ printing map struct hashtable


    【解决方案1】:

    更新评论:

    friend std::ostream& operator<<(std::ostream& os, Edge const& edge)
    {
        os << "Edge { weight:" << edge.weight << " {\n";
        for(edge_map::const_iterator it=edge.edge.begin(); it!=edge.edge.end(); ++it)
        {
            os << "{ '" << it->first << "', \"" << it->second << "\" } ";
        }
        return os << "\n}";
    }
    

    应该让你开始。

    1. int main,不是void main
    2. weight = w;,不是weight = w.
    3. 您需要增加循环变量。 i+2 无效。你是说i+=2吗?
    4. 如果你这样做了,应该修复循环条件以避免越界寻址:

          for(size_t i = 1; i+1<tokens.size(); i+=2){
      

      注意使用size_t,避免混淆signed/unsigned比较

    5. Edge 构造函数应该初始化weight

    6. 如果你需要//crutch comments,你的代码应该更清晰:_)
    7. 考虑使用解析器来读取您的输入。您严重缺乏输入验证、格式灵活性和错误处理。

    8. 哦,别用using namespace std

    这里尝试解决一些问题:

    #include <string>
    #include <map>
    #include <list>
    #include <vector>
    #include <iostream>
    #include <iterator>
    
    #include <sstream>
    #include <fstream>
    #include <algorithm>
    
    struct Edge{
        typedef std::map<char, std::string> edge_map;
        edge_map edge;
        int weight;
    
        Edge() : edge(), weight(0) {};
        Edge(char v, std::string e, int w) : edge(), weight(w)
        {
            edge.insert(std::pair<char,std::string>(v,e));
            weight = w;
        }
    
        friend std::ostream& operator<<(std::ostream& os, Edge const& edge)
        {
            os << "Edge { weight:" << edge.weight << " {\n";
            for(edge_map::const_iterator it=edge.edge.begin(); it!=edge.edge.end(); ++it)
            {
                os << "{ '" << it->first << "', \"" << it->second << "\" } ";
            }
            return os << "\n}";
        }
    };
    
    int main()
    {
        std::list<Edge> edges;
    
        std::string line;
        std::vector<std::string> tokens;
    
        std::ifstream file("input.txt");
    
        if(!file.is_open()){
            std::cout<<"Could not open file\n";
        }else{
            while(std::getline(file,line)){
                tokens.clear();
                char start = line.front();
                std::istringstream iss(line);
                std::copy(std::istream_iterator<std::string>(iss), 
                        std::istream_iterator<std::string>(),
                        std::back_inserter<std::vector<std::string>>(tokens));
                for(size_t i = 1; i+1<tokens.size(); i+=2){
                    Edge e = Edge(start, tokens.at(i), stoi(tokens.at(i+1)));
                    edges.push_back(e);
                }
            }
        }
    }
    

    【讨论】:

    • 对不起,我没有复制和粘贴。所以那些已经在我的代码中了。除了初始化权重。不过,我现在已经在此处的代码中更改了它们。
    • 呃。那太好了。随意同情投票。也许吧。
    • 我想知道如何打印我的列表 边缘。由于打印列表的常规方式不起作用(使用从 edge.begin() 到 edge.end() 的迭代器)。你能帮我解决这个问题吗?
    • @user3342236 呃。您没有包含任何与打印相关的代码? 更新答案中的更新有帮助吗?
    • 添加它并且什么都不做。导致我的程序无休止地运行。我必须手动停止它。如果它确实有效,我将如何调用 main 中的列表打印?
    猜你喜欢
    • 2020-07-21
    • 1970-01-01
    • 2015-05-08
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多