每当我尝试使用find()将字符串添加到向量时出错



所以我正在编码,并试图将字符串添加到向量中,但无论何时编译函数,它都会返回

terminate called after throwing an instance of 'ErrorException'
what():  r���U
Aborted (core dumped)

但是,如果我对字符串变量进行了定制,它工作得很好,为什么当我试图将其推回到向量时它就不工作了?

这是我的代码

#include <vector>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
/*
movie Data
- returns data from the txt file and stores them in a vector
Returns:
- a vector with movie names and actors
*/
vector<string> movieData(string &filename)
{
fstream myFile;
vector<string> data;
string content; //All the content casts and movie title
myFile.open(filename);
if(!myFile.good()) //If the file failed
{
cout << "File Not Found" << endl;
exit(1);
}
//---------------------------------------
while(getline(myFile, content, 'n')) //seperates each movie by the line
{
data.push_back(content);
}
//---------------------------------------
myFile.close();
return data;
}
/*
movie Title
- a Vector with Movie Titles
Returns:
- a Vector only including the movie titles from each
distinct line
*/
vector<string> movieTitle(vector<string> &data)
{
vector<string> Titles;
string word;
for(int i = 0; i<data.size(); i++)
{
int pos = 0;
pos = data[i].find("t");
Titles.push_back(data[i].substr(0, pos));
}
return Titles;
}
int main()
{
vector<string> data; //contains titles and cast seperated by t
//ex: Moon Knight (2022)tOscar IssactEthan Hawke
vector<string> Title;
string filename = "movies_mpaa.txt";
data = movieData(filename);
Title = movieTitle(data);
cout << "Title: "<< Title[Title.size()] << endl;
cout << "Data: " << data[0] << endl;

return 0;
}

我还通过调试器运行了它,显然错误或导致问题的原因是

pos = data[i].find("t"); //This iterates 2 times

Titles.push_back(data[i].substr(0, pos)); //This iterates 3 times

我不知道我做错了什么,因为对我来说,这在逻辑上是有道理的。

这是txt.file

#include <vector>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
/*
movie Data
- returns data from the txt file and stores them in a vector
Returns:
- a vector with movie names and actors
*/
vector<string> movieData(string &filename)
{
fstream myFile;
vector<string> data;
string content; //All the content casts and movie title
myFile.open(filename);
if(!myFile.good()) //If the file failed
{
cout << "File Not Found" << endl;
exit(1);
}
//---------------------------------------
while(getline(myFile, content, 'n')) //seperates each movie by the line
{
data.push_back(content);
}
//---------------------------------------
myFile.close();
return data;
}
/*
movie Title
- a Vector with Movie Titles
Returns:
- a Vector only including the movie titles from each
distinct line
*/
vector<string> movieTitle(vector<string> &data)
{
vector<string> Titles;
string word;
for(int i = 0; i<data.size(); i++)
{
int pos = 0;
pos = data[i].find("t");
Titles.push_back(data[i].substr(0, pos));
}
return Titles;
}
int main()
{
vector<string> data; //contains titles and cast seperated by t
//ex: Moon Knight (2022)tOscar IssactEthan Hawke
vector<string> Title;
string filename = "movies_mpaa.txt";
data = movieData(filename);
Title = movieTitle(data);
cout << "Title: "<< Title << endl;
cout << "Data: " << data[0] << endl;

return 0;
}

这是main((中的语法错误;Title[Title.size()] change to --> Title

感谢@Yksisarvien对的帮助

最新更新