Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.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++ 告诉'endl'不要冲洗_C++_Newline_Iostream - Fatal编程技术网

C++ 告诉'endl'不要冲洗

C++ 告诉'endl'不要冲洗,c++,newline,iostream,C++,Newline,Iostream,我的程序将大量短行打印到cout 作为一个稍微做作的例子,我的线条看起来有点像这样: cout<<"The variable's value is: "<<variable<<endl; 但这似乎不是一个好的解决方案,因为endl抽象了指定端点的特定系统方式,而as\n没有。这似乎也是一个糟糕的解决方案,因为如果我将来需要缓冲,我将不得不修改整个代码库 因此,我问,有没有办法禁用endl的缓冲区刷新功能 编辑 进一步的挖掘似乎表明endl和\n都尊重操作系统

我的程序将大量短行打印到
cout

作为一个稍微做作的例子,我的线条看起来有点像这样:

cout<<"The variable's value is: "<<variable<<endl;
但这似乎不是一个好的解决方案,因为
endl
抽象了指定端点的特定系统方式,而as
\n
没有。这似乎也是一个糟糕的解决方案,因为如果我将来需要缓冲,我将不得不修改整个代码库

因此,我问,有没有办法禁用
endl
的缓冲区刷新功能

编辑

进一步的挖掘似乎表明
endl
\n
都尊重操作系统可能选择的各种方式来结束它的行。输出流检测是否处于潜在的交互状态,并相应地缓冲和刷新。因此:可以通过手动告诉输出流执行主动缓冲来解决此问题。。。如果我能想出办法的话

endl抽象了指定端点的特定系统特定方式,其中as\n没有”

std::endl
被定义为输出
'\n'
,然后是刷新。系统特定换行符的正确抽象就是
'\n'

为了防止刷新,只需不使用
std::endl
。此外,如果标准输出连接到或可能连接到交互设备,则可以对其进行行缓冲,在这种情况下,换行符将刷新流。如果这是一个问题,请使用连接到命名文件的
of stream
。我认为在类似Unix的系统上可以使用行缓冲仅当标准输出为终端时,才会发生错误

endl抽象了指定端点的特定系统特定方式,其中as\n没有”

std::endl
定义为输出
'\n'
,然后是刷新。特定于系统的换行符的正确抽象就是
'\n'


为了防止刷新,只需不使用
std::endl
。此外,如果标准输出连接到或可能连接到交互设备,则可以对其进行行缓冲,在这种情况下,换行符将刷新流。如果这是一个问题,请使用连接到命名文件的流的
。我认为在类Unix系统上,只有当标准输出是终端时,才会发生行缓冲。

endl
刷新。如果不希望出现这种行为,请不要使用
endl
。如果要轻松更改代码,请使用自己的操纵器:

inline std::ostream& myendl( std::ostream& os ){
    os.put(os.widen('\n'));
    return os;
}

这样,您就可以在一个位置轻松更改
myendl的行为。

endl
刷新。如果不希望出现这种行为,请不要使用
endl
。如果要轻松更改代码,请使用自己的操纵器:

inline std::ostream& myendl( std::ostream& os ){
    os.put(os.widen('\n'));
    return os;
}
这样,您就可以在一个位置轻松地更改您的
myendl的行为。

根据

endl::在输出序列os中插入一个结束行字符,并通过调用
os.put(os.wilded('\n'))
然后调用
os.flush()
来刷新它

因此,您似乎只想编写
os.put(os.wilded('\n'))
,从这个定义来看,它应该是安全的、可移植的和正确的,并且满足您的主要需求。

根据

endl::在输出序列os中插入一个结束行字符,并通过调用
os.put(os.wilded('\n'))
然后调用
os.flush()
来刷新它


因此,您似乎只想编写
os.put(os.wilded('\n'))
,从这个定义来看,它应该是安全的、可移植的和正确的,并且能够满足您的主要需求。

有一个std::nounitbuf,它在这方面有一定的作用。 然而,我没有注意到任何区别。为了绕过ostream关于何时或何时冲洗的所有想法,我尝试了以下方法:

std::ostringstream oss;
//  std::cout << std::nounitbuf;
for( int i = 0; i < 1000000; i++ ){
//  std::cout <<  "Test " << "file" << '\n';
    oss <<  "Test " << "file" << '\n';
}
std::cout << oss.str();

有一种std::NonuitBuf被证明对这件事有一定的影响。 然而,我没有注意到任何区别。为了绕过ostream关于何时或何时冲洗的所有想法,我尝试了以下方法:

std::ostringstream oss;
//  std::cout << std::nounitbuf;
for( int i = 0; i < 1000000; i++ ){
//  std::cout <<  "Test " << "file" << '\n';
    oss <<  "Test " << "file" << '\n';
}
std::cout << oss.str();

如果刷新是一个问题,您可以实现一个流缓冲区,该缓冲区覆盖
sync()
成员函数,以便仅刷新到外部设备(如果您指定)。如果您打算在整个程序中更改这些首选项,则还必须创建自己的操纵器
flush\u on_endl
noflush\u on_endl

#include <iostream>

static int disable() {
    static int index(std::ios_base::xalloc());
    return index;
}

class save_buffer
{
public:
    save_buffer(std::ios& other)
        : str(other), buf(other.rdbuf())
    { }

    ~save_buffer() { str.rdbuf(buf); }
private:
    std::ios& str;
    std::streambuf* buf;
};

class syncing_buffer_optional : public std::streambuf, private save_buffer
{
public:
    syncing_buffer_optional(std::ostream& other)
        : save_buffer(other),
          buf(other.rdbuf()),
          disable_sync(other.iword(disable()))
    { }

    std::streambuf::int_type overflow(std::streambuf::int_type c)
    {
        buf->sputc(c);
        return 0;
    }

    int sync()
    {
        return disable_sync? 0: buf->pubsync();
    }
private:
    std::streambuf* buf;
    bool disable_sync;
};

std::ostream& flush_on_endl(std::ostream& os)
{
    os.iword(disable()) = false;
    return os;
}

std::ostream& noflush_on_endl(std::ostream& os)
{
    os.iword(disable()) = true;
    return os;
}


std::ostream& endl(std::ostream& os)
{
    syncing_buffer_optional eb(os);
    os.rdbuf(&eb);

    return os << std::endl;
}

int main()
{
    std::cout << noflush_on_endl << endl;
}
#包括
静态int disable(){
静态int索引(std::ios_base::xalloc());
收益指数;
}
类保存缓冲区
{
公众:
保存缓冲区(std::ios和其他)
:str(其他)、buf(其他.rdbuf())
{ }
~save_buffer(){str.rdbuf(buf);}
私人:
std::ios和str;
标准::streambuf*buf;
};
类同步\u缓冲区\u可选:public std::streambuf,private save\u缓冲区
{
公众:
同步缓冲区可选(标准::ostream和其他)
:保存缓冲区(其他),
buf(other.rdbuf()),
禁用同步(other.iword(disable()))
{ }
std::streambuf::int_类型溢出(std::streambuf::int_类型c)
{
buf->sputc(c);
返回0;
}
int sync()
{
返回disable_sync?0:buf->pubsync();
}
私人:
标准::streambuf*buf;
bool禁用同步;
};
std::ostream&flush_-on_-endl(std::ostream&os)
{
os.iword(disable())=false;
返回操作系统;
}
std::ostream&noflush_on_endl(std::ostream&os)
{
os.iword(disable())=true;
返回操作系统;
}
std::ostream&endl(std::ostream&os)
{
同步缓冲区可选eb(操作系统);
操作系统rdbuf(&eb);

return os如果刷新是一个问题,那么可以实现一个流缓冲区来覆盖
sync()
成员函数仅在您指定的情况下刷新到外部设备。如果您打算在整个程序中更改这些首选项,它还必须创建您自己的操纵器
flush\u on_endl
noflush\u on_endl

#include <iostream>

static int disable() {
    static int index(std::ios_base::xalloc());
    return index;
}

class save_buffer
{
public:
    save_buffer(std::ios& other)
        : str(other), buf(other.rdbuf())
    { }

    ~save_buffer() { str.rdbuf(buf); }
private:
    std::ios& str;
    std::streambuf* buf;
};

class syncing_buffer_optional : public std::streambuf, private save_buffer
{
public:
    syncing_buffer_optional(std::ostream& other)
        : save_buffer(other),
          buf(other.rdbuf()),
          disable_sync(other.iword(disable()))
    { }

    std::streambuf::int_type overflow(std::streambuf::int_type c)
    {
        buf->sputc(c);
        return 0;
    }

    int sync()
    {
        return disable_sync? 0: buf->pubsync();
    }
private:
    std::streambuf* buf;
    bool disable_sync;
};

std::ostream& flush_on_endl(std::ostream& os)
{
    os.iword(disable()) = false;
    return os;
}

std::ostream& noflush_on_endl(std::ostream& os)
{
    os.iword(disable()) = true;
    return os;
}


std::ostream& endl(std::ostream& os)
{
    syncing_buffer_optional eb(os);
    os.rdbuf(&eb);

    return os << std::endl;
}

int main()
{
    std::cout << noflush_on_endl << endl;
}
#包括
s