[英]Error whenever I try to add a string to a vector using find()
所以我正在编码并且我试图将一个字符串添加到一个向量但是每当我编译 function 它返回
terminate called after throwing an instance of 'ErrorException'
what(): r���U
Aborted (core dumped)
但是如果我 cout 字符串变量它工作得很好,为什么当我尝试 push_back 到向量时它不会工作?
这是我的代码
#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 Issac\tEthan 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 Issac\tEthan 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
谢谢@Yksisarvinen 的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.