Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何创建过滤流的成员向量?_C++_C++11_Vector_Boost Iostreams - Fatal编程技术网

C++ 如何创建过滤流的成员向量?

C++ 如何创建过滤流的成员向量?,c++,c++11,vector,boost-iostreams,C++,C++11,Vector,Boost Iostreams,让我们从一个使用boost::iostreams的简单压缩文件读取器类开始: class SingleFileOpener{ public: SingleFileOpener(const std::string& filename, bool is_compressed) { if(is_compressed) m_stream.push(bio::zlib_decompressor()); m_stream.pus

让我们从一个使用
boost::iostreams
的简单压缩文件读取器类开始:

class SingleFileOpener{
    public:
        SingleFileOpener(const std::string& filename, bool is_compressed) {
            if(is_compressed) m_stream.push(bio::zlib_decompressor());
            m_stream.push(bio::file_source{filename});
        }

        void print() {
            bio::copy(m_stream, std::cout);
        }
    private:
        using unseekable_stream = boost::iostreams::filtering_istream;
        unseekable_stream m_stream;
};
现在调用
SingleFileOpener(“input.txt”,true)
然后调用
print()
可以正常工作

我想扩展我的类,以类似的方式读取和操作多个文件。下面是我试用过的示例代码(在上面的Coliru链接中也有注释):

类多文件开卷器{
公众:
多文件开启器(const std::vector文件名,std::vector已压缩){
对于(自动i=0u;i
在这里:


不使用初始值设定项列表方式是否可以执行此操作

选项1

使用列表:

    std::list<unseekable_stream> m_stream;

选项2

使用
唯一\u ptr

    std::vector<std::unique_ptr<unseekable_stream>> m_stream;

这样,您以后就无法添加或删除流。如果需要这些功能,请使用其他选项。

您能指出编译器阻塞了哪一行吗?您在说您尝试了
std::shared\u ptr
时,已经以注释的形式指出,您的意思是您不能使用需要
std::vector
不可查看的\u流;
成为
std::shared\u ptr s(新的不可查看的\u流);
是的,shared\u ptr确实有效-我在实现中犯了一个错误。你能详细说明一下(1)-我甚至尝试了
std::move(s)
而且它失败过滤\u stream没有复制和移动构造函数,您必须直接在容器中创建它,然后引用它。vector不能包含既不可移动也不可复制的类型。因此,请使用我的示例中的列表。我对boost不是专家,但我希望它不喜欢移动,因为s是在sco中声明的pe,它希望在退出作用域时自动销毁时保持完整。这与boost无关,
move
不起作用,因为没有声明move构造函数。其他选项是使用
vector
,例如。
    std::list<unseekable_stream> m_stream;
m_stream.emplace_back();
auto& s = m_stream.back();
if(is_compressed[i]) s.push(bio::zlib_decompressor());
s.push(bio::file_source{filenames[i]});
    std::vector<std::unique_ptr<unseekable_stream>> m_stream;
auto stream_ptr = std::make_unique<unseekable_stream>();
... //same as above but change . to ->
m_stream.push_back(std::move(stream_ptr));
std::vector<unseekable_stream> m_stream;

MultiFileOpener(const std::vector<std::string>& filenames, const std::vector<bool>& is_compressed) 
 : m_stream(filenames.size())
   {
        for(auto i = 0u; i < filenames.size(); i++) {
            unseekable_stream& s = m_stream[i];
            if(is_compressed[i]) s.push(bio::zlib_decompressor());
            s.push(bio::file_source{filenames[i]});
        }
    }