我有一个文本文件,其中包含节目名称和加星标的人。
我需要在类中创建一个向量,我可以在其中键入actor,它将输出电影。
但我不想将这整个条目读入一个向量来节省内存。
我怎样才能这样做以便我会读取下面包含此内容的txt文件,然后找到例如Min, Jun So
的演员名称,它将找到两个实例,然后将两部电影都读入矢量并输出?
或许这样做的代码布局会非常棒。只是为了让我开始。
Min, Elia
Starlight Inn (2010) [Skyler] <4>
Min, Jun So
"Joseon X-Files - Secret Book" (2010) {Ghosts of Yidu (#1.6)} [Choi Eui Shin] <5>
Min, Jung So
"Mischievous Kiss" (2010) [Oh Ha Ni]
Min, Xiao
Little Sister (2010) [Mei Mei] <2>
答案 0 :(得分:1)
尝试示例代码:
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
std::ifstream file( "test.txt" ) ;
std::string search_str = "Elia" ;
std::string line ;
int line_number = 0 ;
std::vector<string> finds;
finds.resize(100);
while( std::getline( file, line ) )
{
++line_number ;
if( line.find(search_str) != std::string::npos )
{
std::cout << "line " << line_number << ": " << line << '\n' ;
finds.push_back(line);
std::getline( file, line );
finds.push_back(line);
}
}
for (int i=0;i<finds.size();i++)
{
std::cout<<finds[i]<<"\n";
}
return 0;
}
输入:Elia
输出:
Min, Elia
Starlight Inn (2010) [Skyler] <4>
Press any key to continue