简体   繁体   English

使用istream_iterator构造自定义类型的向量

[英]Constructing a vector of custom type with istream_iterator

I want to write and read STL Vector of my class type to binary file, but don't understand what's wrong with istream_iterator. 我想写我的类类型的STL Vector到二进制文件,但是不明白istream_iterator有什么问题。
Project rules disallow to use text files, same as third-party libraries like Boost. 项目规则不允许使用文本文件,与Boost等第三方库相同。

This is Book.h: 这是Book.h:

class Book{
    public:
    Book(const std::vector<Book>& in_one_volumes,const std::string& title,
    const std::string& author,const int pagecount,const int price,const std::string& date);
    private:
    std::vector<Book> inOneVolumes;
    std::string title;
    std::string author;
    int pagecount;
    int price;
    std::string date;
};

This is write method: 这是写方法:

void writeBook(std::vector<Book> books) {
    std::ofstream binOut("book.bin", std::ios::binary);
    std::copy(books.begin(), books.end(),
        std::ostream_iterator<Book>(binOut, "\n")); 
}

And i want to read like this: 我想这样读:

std::vector<Book> readBooks() {
    std::vector<Book> toReturn;
    std::ifstream BinIn("book.bin", std::ios::binary);
    std::istream_iterator<Book> file_iter(BinIn);
    std::istream_iterator<Book> end_of_stream;
    std::copy(file_iter, end_of_stream, std::back_inserter(toReturn));
    return toReturn;    
}

Compiller says -- Book:no appropriate default constructor available. Compiller说 - Book:没有合适的默认构造函数。

std::istream_iterator<Book> uses operator>>(std::istream&, Book&) to read data into objects. std::istream_iterator<Book>使用operator>>(std::istream&, Book&)将数据读入对象。 Since this operator>> requires an existing Book object as parameter (to write the data into), the iterator has to construct one before it can dump the data from the stream into it, and for this it requires a default constructor. 由于此operator>>需要现有的Book对象作为参数(将数据写入),因此迭代器必须先构造一个,然后才能将数据从流中转储到其中,为此需要一个默认的构造函数。

Your Book class does not have one. 您的Book课程没有。 The easiest solution to the problem would be to give it one. 解决问题的最简单方法是给它一个。

In the event that this is not an option (eg, if Book as to guarantee invariants that a default constructor cannot provide), you could introduce an intermediate data transfer class that is default-constructible, can be filled with data via operator>> , and can be converted to Book . 如果这不是一个选项(例如,如果Book为保证默认构造函数无法提供的不变量),您可以引入一个默认构造的中间数据传输类,可以通过operator>>填充数据,并可以转换为Book Sketch: 草图:

class TransferBook {
public:
  // To read data from stream
  friend std::istream &operator>>(std::istream &in, TransferBook &dest);

  // Conversion to Book. Use the non-default Book constructor here.
  operator Book() const {
    return Book(all the data);
  }

private:
  // all the data
};

...

std::vector<Book> books;
std::ifstream file;

// Note that books contains Books and the iterator reads TransferBooks.
// No Book is default-constructed, only TransferBooks are.
std::copy(std::istream_iterator<TransferBook>(file),
          std::istream_iterator<TransferBook>(),
          std::back_inserter(books));

To be sure, this approach is rather cumbersome and essentially duplicates code, and probably it is less hassle to give Book the default-constructor. 可以肯定的是,这种方法相当麻烦并且基本上重复了代码,并且可能不那么麻烦给Book默认构造函数。 However, if Book cannot be changed in this way, this is a possible workaround. 但是,如果无法以这种方式更改Book ,则这是一种可能的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM