0

在为 movieName 添加字符串流之后,我在下面的代码中得到了一个 List Iterator Not incrementable,并且不确定如何解决该错误。我正在尝试从文件中读取并将项目添加到列表中,然后遍历列表并将电影标题中的 eash 单词添加到另一个列表中。任何帮助,将不胜感激!

#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
list <string> movieList;
list <string>::iterator iterMovie; 

list <string> *titleWordList = new list <string>;
list <string>::iterator iterTitleWord; 

ifstream inFile;
inFile.open("JamesBond.txt"); 

if (!inFile)
{
    cout << "Could not find the specified file.";
}
else 
{
    movieList.clear();
    string movieName;

    while(getline(inFile,movieName))
    {
        movieList.push_back((movieName));

        /*
        for each(string movieName in movieList)
        {
            titleWordList->push_back(movieName); 
        }
        */

        stringstream ss(movieName);
        while (ss >> movieName)
        {
             titleWordList->push_back(movieName); 
        }

    }
}

if (movieList.empty())
{
    cout << "No Data Found!" << endl; 
}
else
{
    cout << "Writing Output: \n\n";

    for (iterMovie=movieList.begin(); iterMovie !=movieList.end(); ++iterMovie)
    {
        cout << *iterMovie << endl; 

    }

    for (iterTitleWord=movieList.begin(); iterTitleWord != movieList.end();     ++iterMovie)
    {
        cout << *iterTitleWord << endl;
        cout << &titleWordList; 
    }


  }
}
4

1 回答 1

2

在第二个for有一个复制粘贴错误,它应该是++iterTitleWord而不是++iterMovie.

此外,正如@Greg 所建议的那样,iterTitleWord=movieList.begin()应该iterTitleWord != movieList.end()iterTitleWord=titleWordList->begin()iterTitleWord != titleWordList->end()分别。

于 2012-06-04T04:59:01.723 回答