简体   繁体   English

如何打印由另一个字符串链接列表组成的链接列表?

[英]How to print a linked list that is made up of another linked list of string?

I have decided to use the STL list, although our instructor has been teaching us one that is made from the book. 我已决定使用STL清单,尽管我们的讲师一直在教我们一个由本书制成的清单。 I made this change because the header file from the book is not working with me, most probably my fault. 我进行此更改是因为书中的头文件对我不起作用,很可能是我的错。 So, after researching online, I have tried my best to understand how the iterator of the STL list works. 因此,在在线研究之后,我尽了最大的努力来了解STL列表的迭代器是如何工作的。 The following is the code that I made to print the linked list x defined by type list<list<string>> . 以下是我制作的用于打印由list<list<string>>类型定义的链表x的代码。

  list < list <string>> x;

while (!inData.fail())
{
    //...
    string str(cstr);//cstr at this point is the sorted string
    bool match = false;
    list <string> inner_list;
    inner_list.push_back(str);
    list<list <string>>::const_iterator cit;
    for (cit = x.begin(); !match && cit !=x.end(); advance(cit,1))
    {
        if (*cit == inner_list)
        {
            inner_list.push_back(temp2); //temp2 is unsorted string
            match = true;
        }
    }

    if (match ==false) x.push_back(inner_list);

} }

//printing


list<list <string>>::const_iterator it; 
    list<string>::const_iterator it2;
    for (it = x.begin(); it != x.end(); it++)
    {
        for (it2 = it->begin(); it2 != it->end(); it2++)
        {
            cout << *it2 << " ";
        }

    }

I am getting a list iterator dereferencing error, but I have no clue why. 我收到列表迭代器取消引用错误,但是我不知道为什么。 I would appreciate any help or hints. 我将不胜感激任何帮助或提示。 And if anything is unclear or needs explanation please tell me. 如果有任何不清楚或需要解释的地方,请告诉我。

You can use the following code. 您可以使用以下代码。 Hope this will help you. 希望这会帮助你。 Thanks. 谢谢。

typedef long long ll;

ll N,M;
string str;
list<string> innerList;
list<list<string> > Data;

cin >> M; // M holds the no. of nodes containing linked list of strings //
for(ll i=0; i<M; i++){
    getchar();
    cin >> N; // No. of strings containing in inner linked list 
    getchar();
    for(ll i=0; i<N; i++){
        cin >> str;
        innerList.push_back(str);
    }

    Data.push_back(innerList);
    innerList.clear();
}

// print data //

for(list<list<string> >::iterator it = Data.begin(); it!=Data.end(); it++){
    for(list<string>::iterator iit = (*it).begin(); iit!=(*it).end(); iit++){
        cout << (*iit) << "  " ;
    }
    cout << "\n";

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM