0

さまざまなファイルのコンテンツをベクトルに保存したい: Vector(0) = Content File1 Vector(1) = Content File2 ...

後で、このベクトルの各インデックスから行ごとに読み取る必要があります (getline):

getline(Vector(0), string myString)

いろいろなサイトで読んでいるように、使えませんvector<istream> myVector

では、どうすれば解決できますか?

4

4 に答える 4

3

操作するデータのサイズによって異なります。私の 2 つのサンプルがテストされました。

いくつかの生のポインターを処理するクラスを使用できます

#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;
}
于 2011-04-08T09:05:33.870 に答える
1

iostream は std コンテナーに入れることはできません。

たぶん、void ポインター配列を使用してそれらを保存できます。

于 2011-04-08T08:10:57.960 に答える
0

明確にするために、ファイルハンドルを保存してから後でファイルから読み取るか、ファイルをコンテナのような文字列に読み取ってベクトルに保存しますか?

std::string後者について話していて、ファイルがテキスト ファイルの場合、それらをオブジェクトに読み込んで、その形式で保存できるはずです。キャリッジ リターンなどを a に保存しても問題はありstd::stringませんが、このソリューションはスケーラブルではない可能性があることに注意してください。ベクトルに 4Gb 以上のファイルまたは 100,000 ファイルを保存する必要がある場合はどうなりますか?

于 2011-04-08T08:23:28.360 に答える
0

ストリームのベクトルを格納する場合は、次のようなポインター コンテナーまたはスマート ポインターのベクトルを使用することを検討してください。

typedef boost::shared_ptr<std::istream> stream_ptr
std::vector<stream_ptr> stream_container;

// Then add your streams
stream_container.push_back(stream_ptr(new std::ifstream("f1")));
stream_container.push_back(stream_ptr(new std::ifstream("f2")));
stream_container.push_back(stream_ptr(new std::ifstream("f3")));
:

// Then iterate through and call get line..
std::vector<stream_ptr>::iterator it(stream_container.begin()), end(stream_container.end());

std::string sv;
for(; it != end; ++it)
  getline(**it, sv);

これは単なるコード例であり、テストされていません。

于 2011-04-08T08:25:59.117 に答える