为什么在标准输出的末尾出现%百分比,但在使用endl时却没有呢? [英] Why does a % percentage appear at the end of the output to standard out but not when I use endl?

查看:90
本文介绍了为什么在标准输出的末尾出现%百分比,但在使用endl时却没有呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在此输出的末尾有%?我曾尝试刷新流(而不是endl),但是当我通过调用endl结束流时,它会清除%。

Why is there a trailing % at the end of this output? I've tried flushing the stream (not endl), however when I end the stream with a call to endl it gets rid of the %. Any help here would be much appreciated.

template<class T>
void print_collection(T * t){
        for(int i = 0; i < t->size()-1; ++i){
                cout << t->at(i) << ", ";
        }
        std::cout<< t->at(t->size() -1);
        std::cout<<flush;
}

int main(){
        int n_A = 5;
        int A[] = {2, 3, -2, 34, -29};

        vector<int> vec(A, A + n_A);
        sort(vec.begin(), vec.end());

        print_collection(&vec);
        return 0;
}

OUTPUT
-29, -2, 2, 3, 34%  

以下是有关编译器的更多信息,我使用的是macOS 10.15.4:

Here is more information about the compiler, I'm using macOS 10.15.4:

g++ -Wall -std=c++11 array.cpp -o array

g++ Information:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.59)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin  


推荐答案

重复的问题。谢谢@WBuck!

Duplicate question. Thanks @WBuck!

@Kevin状态:
当程序的输出(非null)不包含尾随换行符时,zsh会添加颜色-倒置%以指示这一点,并在打印提示之前移至下一行;通常,它比bash的行为更方便,只需在输出结束处启动命令提示符即可。

@Kevin states: When (non-null) output from a program doesn't include a trailing newline, zsh adds that color-inverted % to indicate that and moves to the next line before printing the prompt; it's generally more convenient than bash's behavior, just starting the command prompt where the output ended.

获取一个奇怪的百分比符号在printf中输出。在终端中使用C来获取printf输出中的一个奇怪的百分比符号。

这篇关于为什么在标准输出的末尾出现%百分比,但在使用endl时却没有呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆