我正在使用boost iostreams读取一个gzip压缩文件:以下工作正常:
namespace io = boost::iostreams;
io::filtering_istream in;
in.push(boost::iostreams::basic_gzip_decompressor<>());
in.push(io::file_source("test.gz"));
stringstream ss;
copy(in, ss);
Run Code Online (Sandbox Code Playgroud)
但是,我不想把整个gzip压缩文件读入内存.我希望能够逐步读取文件.
例如,如果我有一个从istream初始化自己的数据结构X,
X x;
x.read(in);
Run Code Online (Sandbox Code Playgroud)
失败.据推测,这是因为如果我们正在进行部分流,我们可能不得不将字符放回到流中.任何想法是否提升iostreams支持这个?
有没有办法可以检查ostream的istream是否可以搜索?
我怀疑进行测试搜索并检查failbit是不正确的,因为搜索可能因为无关的原因而失败.
我需要这个在Linux和Mac上工作,如果这有所作为.
为什么以下代码不允许调用foo(ptr)?
#include <boost/scoped_ptr.hpp>
struct A {
virtual ~A() {}
};
struct B: public A {};
void foo(boost::scoped_ptr<A>& a) {}
void goo(A& a) {}
int main() {
boost::scoped_ptr<B> ptr(new B);
foo(ptr);
B b;
goo(b);
}
Run Code Online (Sandbox Code Playgroud)
我们传递引用的相应表单按预期工作.我们不应该使用boost scoped_ptr做多态吗?
g ++ with boost 1.49给了我:
error: invalid initialization of reference of type ‘boost::scoped_ptr<A>&’ from expression of type ‘boost::scoped_ptr<B>’
Run Code Online (Sandbox Code Playgroud) 我有一个具有向量成员变量的类,我填写如下:
class Foo {
vector<int> v;
void g() {
vector<int> w;
// fill w
v = w;
}
};
Run Code Online (Sandbox Code Playgroud)
我的问题:临时矢量w可能变得很大,我不想付出复制结构的代价.我应该使用std :: swap而不是复制吗?我的理解是std :: swap由于vector的特化而更有效率(它只是将指针交换到堆).
我有以下代码:
#include <iostream>
#include <vector>
using namespace std;
struct A{};
struct B: public A {};
template <typename T>
void foo(const T& obj) { cerr << "Generic case"<< endl;}
void foo(const A& a) {
cerr << "Specific case" << endl;
}
int main() {
vector<int> v;
foo(v);
B b;
foo(b);
A a;
foo(a);
}
Run Code Online (Sandbox Code Playgroud)
输出是
为什么foo(const A& a)没有为B对象选择它?
奇怪的是,如果我删除了模板化方法并且只是具有以下内容:
#include <iostream>
#include <vector>
struct A{};
struct B: public A {};
//template <typename T>
//void …Run Code Online (Sandbox Code Playgroud) c++ ×5
boost ×1
copy ×1
gzip ×1
inheritance ×1
linux ×1
overloading ×1
polymorphism ×1
scoped-ptr ×1
seekg ×1
stdvector ×1
swap ×1
templates ×1