gpt4 book ai didi

c++ - 如何将不同文件的内容写入一个 vector 以供进一步使用 getline

转载 作者:行者123 更新时间:2023-11-27 23:30:47 25 4
gpt4 key购买 nike

我想将不同文件的内容保存到一个 vector 中: vector (0) = 内容文件 1 vector (1) = 内容文件 2...

稍后我需要逐行读取此 vector 的每个索引 (getline):

getline(Vector(0), string myString)

正如我在不同网站上看到的那样,我无法使用 vector<istream> myVector .

那我该如何解决呢?

最佳答案

这取决于您要操作的数据的大小。我的两个 sample 已经过测试。

你可以使用一个处理一些原始指针的类

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

class file_vector
{
public:
file_vector()
{}

virtual ~file_vector()
{
for( std::vector< std::ifstream* >::iterator it = m_file_streams.begin(); it != m_file_streams.begin(); it++)
{
std::ifstream * p_stream = *it;
delete p_stream;
}
}

void append_file(const std::string& file_name)
{
std::ifstream * p_stream = new std::ifstream( file_name.c_str() );
if(p_stream->is_open())
m_file_streams.push_back(p_stream);
else
delete p_stream;
}

void reset()
{
for( std::vector< std::ifstream* >::iterator it = m_file_streams.begin(); it != m_file_streams.end(); it++)
{
std::ifstream * p_stream = *it;
p_stream->seekg(0,p_stream->beg);
}
}

size_t size()
{
return m_file_streams.size();
}

std::ifstream & get(size_t index)
{
return * m_file_streams.at(index); // Using at because of index check
}

std::ifstream & operator [] (size_t index)
{
return get(index);
}

private:
std::vector< std::ifstream* > m_file_streams;
};


int main()
{
file_vector files_content;
files_content.append_file("file1.txt");
files_content.append_file("file2.txt");
files_content.append_file("file3.txt");

for(size_t i = 0; i < files_content.size(); i++)
{
std::string current_line;
while(std::getline(files_content[i],current_line))
std::cout << current_line << std::endl;
}

files_content.reset(); // To make getline usable again


return 0;
}

或者一个 std::vector< std::vector< std::string >>。这是针对小文件的基本解决方案,但它确实有效。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

typedef std::vector< std::string > string_vec;
typedef std::vector< std::string >::iterator string_it;

typedef std::vector< string_vec> file_vec;
typedef std::vector< string_vec >::iterator file_it;

int main()
{
string_vec file_names;
file_names.push_back("file1.txt");
file_names.push_back("file2.txt");
file_names.push_back("file3.txt");

file_vec files_content;
string_vec empty_file_content;

for(string_it file_name = file_names.begin(); file_name != file_names.end(); file_name++)
{
std::ifstream input_stream( file_name->c_str() );
files_content.push_back(empty_file_content);

if(input_stream.is_open())
{
string_vec & current_file_content = files_content[ files_content.size() - 1 ];
std::string current_line;
while(std::getline(input_stream, current_line))
current_file_content.push_back(current_line);
}
}

// Some stuff

// Reading the content later on
for(file_it file = files_content.begin(); file != files_content.end(); file++)
{
for(string_it line = file->begin(); line != file->end(); line++)
{
std::cout << *line << std::endl;
}
}

return 0;
}

关于c++ - 如何将不同文件的内容写入一个 vector 以供进一步使用 getline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5592231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com