如何在c++中使用链表读取txt文件

how to use linked list to read in txt file in c++

本文关键字:链表 读取 txt 文件 c++      更新时间:2023-10-16

这是我到目前为止所拥有的,但这不会读取文本文件的未确定长度

#include <iostream> 
#include <string>   
#include <fstream>  
using namespace std ;
int main() 
    {
char *file_name = "doc.txt" ; 
ifstream fin ;
fin.open( file_name ) ;
if( ! fin ) {
    cout << "Problems opening " << file_name << endl ;
    return -1 ;
}

const unsigned MAX = 100 ; 
string doc[MAX] ;
unsigned word_count = 0 ;

while( fin >> doc[ word_count ] ) 
{
    cout << doc[ word_count ] << endl ;
    word_count ++ ;
}

fin.close() ;
return 0 ;
}

您可以使用矢量并将新单词推到上面。

如果你只是推和弹出,堆叠会更快。

或者,总有清单。

这在很大程度上取决于你在做什么,以及速度是否重要。