Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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++ 在cpp中的istream前面插入字符串_C++_Istream_Boost Iostreams - Fatal编程技术网

C++ 在cpp中的istream前面插入字符串

C++ 在cpp中的istream前面插入字符串,c++,istream,boost-iostreams,C++,Istream,Boost Iostreams,我的问题是,我想在iostream前面附加一些字符串。你可以在std::cin前面说 #include <iostream> #include <string> void print(std::istream & in){// function not to be modified std::string str; in >> str; std::cout<< str << std::endl;

我的问题是,我想在iostream前面附加一些字符串。你可以在std::cin前面说

#include <iostream>
#include <string>

void print(std::istream & in){// function not to be modified
    std::string str;
    in >> str;
    std::cout<< str << std::endl;
    in >> str;
    std::cout<< str << std::endl;
}

int main() {
    std::string header = "hello ";
    //boost::iostream::filtering_istream in(std::cin);
    std::istream & in = std::cin;

    //you may do someting here
    //something like inserting characters into cin or something with buffers

    print(in);
}
#包括
#包括
无效打印(std::istream&in){//不修改函数
std::字符串str;
在>>str;
std::cout-str;

std::cout流不是容器。它是一个数据流。您无法更改已浮动的数据。唯一的例外是绑定到块设备的流,您可以在其中四处搜索,例如
fstream
——但即使这样,您也只能覆盖


相反,请构造代码,以便在查询流之前先使用
标题

您应该避免这个问题。如果不是同一个流,就不要从同一个流中解析它

低技术 低技术的“解决方案”是将流复制到stringstream,首先将“前缀”放在缓冲区中:

#include <iostream>
#include <string>
#include <sstream>

void print(std::istream &in) { // function not to be modified
    std::string str;
    while (in >> str)
        std::cout << str << std::endl;
}

int main() {
    std::string header = "hello ";
    std::stringstream in;
    in << header << std::cin.rdbuf();

    print(in);
}
#include <iostream>
#include <sstream>
#include <vector>

template <typename B1, typename B2>
class cat_streambuf : public std::streambuf {
    B1* _sb1;
    B2* _sb2;
    std::vector<char> _buf;
    bool _insb1 = true;

  public:
    cat_streambuf(B1* sb1, B2* sb2) : _sb1(sb1), _sb2(sb2), _buf(1024) {}

    int underflow() {
        if (gptr() == egptr()) {
            auto size = [this] {
                if (_insb1) {
                    if (auto size = _sb1->sgetn(_buf.data(), _buf.size()))
                        return size;
                    _insb1 = false;
                }

                return _sb2->sgetn(_buf.data(), _buf.size());
            }();

            setg(_buf.data(), _buf.data(), _buf.data() + size);
        }
        return gptr() == egptr() 
            ? std::char_traits<char>::eof()
            : std::char_traits<char>::to_int_type(*gptr());
    }
};

void print(std::istream &in) { // function not to be modified
    std::string str;
    while (in >> str)
        std::cout << str << std::endl;
}

int main() {
    std::stringbuf header("hello ");
    cat_streambuf both(&header, std::cin.rdbuf());

    std::istream is(&both);
    print(is);
}
高科技 您始终可以创建实现所需行为的自定义流缓冲区:

#include <iostream>
#include <string>
#include <sstream>

void print(std::istream &in) { // function not to be modified
    std::string str;
    while (in >> str)
        std::cout << str << std::endl;
}

int main() {
    std::string header = "hello ";
    std::stringstream in;
    in << header << std::cin.rdbuf();

    print(in);
}
#include <iostream>
#include <sstream>
#include <vector>

template <typename B1, typename B2>
class cat_streambuf : public std::streambuf {
    B1* _sb1;
    B2* _sb2;
    std::vector<char> _buf;
    bool _insb1 = true;

  public:
    cat_streambuf(B1* sb1, B2* sb2) : _sb1(sb1), _sb2(sb2), _buf(1024) {}

    int underflow() {
        if (gptr() == egptr()) {
            auto size = [this] {
                if (_insb1) {
                    if (auto size = _sb1->sgetn(_buf.data(), _buf.size()))
                        return size;
                    _insb1 = false;
                }

                return _sb2->sgetn(_buf.data(), _buf.size());
            }();

            setg(_buf.data(), _buf.data(), _buf.data() + size);
        }
        return gptr() == egptr() 
            ? std::char_traits<char>::eof()
            : std::char_traits<char>::to_int_type(*gptr());
    }
};

void print(std::istream &in) { // function not to be modified
    std::string str;
    while (in >> str)
        std::cout << str << std::endl;
}

int main() {
    std::stringbuf header("hello ");
    cat_streambuf both(&header, std::cin.rdbuf());

    std::istream is(&both);
    print(is);
}

对于相同的输入。对于(非常)大的流,这肯定可以更好地扩展。

“我的问题是,我想在iostream前面附加一些字符串。”不,这是您认为需要解决问题的方式。请告诉我们实际问题。Boost的过滤器可能是执行我在回答中建议的重组的好方法。我邀请您首先尝试实施它:P@LightnessRacesinOrbit我的实际问题是,我想从流中读取一些字符,并将它们放回流中以供la使用ter use。不,我不能这样做。对您的问题进行适当的编辑,以便所有人都能从中受益。@LightnessRacesinOrbit我的问题是..的扩展,正如您在评论中所注意到的,在boost中提到使用unget。我怀疑在中使用多个unget不被视为安全的事情。在boost中使用多个unget安全吗::过滤流?
hello
foo
bar
qux