vector<string> names;
// Read names from file book.txt
ifstream in("movie.txt");
if (!in.is_open())
cout << "Unable to open file\n";
string word;
while (getline(in, word))
names.push_back(word);
int pos(0), i(0), j(0);
string temp;
for (size_t i = 0; i < names.size(); i++)
{
j = i;
while (j >= 0 && names[j] < names[j - 1])
{
temp = names[j];
names[j] = names[j - 1];
names[j - 1] = temp;
j--;
}
}
// Loop to print names
for (size_t i = 0; i < names.size(); i++)
cout << names[i] << '\n';
不知道错误是从哪里来的,因为它仍然在运行,但是在我尝试时 并执行显示“调试断言错误”的文件。有帮助吗?
答案 0 :(得分:0)
如果您想阅读姓名,请尝试使用它
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main(int argc, char **argv)
{
vector<string> names;
ifstream in("movies.txt");
if (!in.is_open())
cout << "Unable to open file\n";
string word;
while (getline(in, word))
names.push_back(word);
for (auto i : names)
cout << i << '\n';
return 0;
}