简体   繁体   English

使std :: cout一个接一个地输出同一行中的多个字符串

[英]Make std::cout output multiple string in the same row one after the other

The title is a mess, it's probably a lot faster to just see the code 标题是乱七八糟的,看到代码可能要快得多

p1_dado = rand() % 6 + 1;

        switch (p1_dado) {
        case 1:
            cout << ".-----." << endl;
            cout << "|     |" << endl;
            cout << "|  o  |" << endl;
            cout << "|     |" << endl;
            cout << "._____." << endl << endl;
            p1_somma = p1_somma + p1_dado;
            break;
        case 2:
            cout << ".-----." << endl;
            cout << "| o   |" << endl;
            cout << "|     |" << endl;
            cout << "|   o |" << endl;
            cout << "._____." << endl << endl;
            p1_ndadi--;
            break;
        case 3:
            cout << ".-----." << endl;
            cout << "|o    |" << endl;
            cout << "|  o  |" << endl;
            cout << "|    o|" << endl;
            cout << "._____." << endl << endl;
            p1_somma = p1_somma + p1_dado;
            break;
        case 4:
            cout << ".-----." << endl;
            cout << "| o o |" << endl;
            cout << "|     |" << endl;
            cout << "| o o |" << endl;
            cout << "._____." << endl << endl;
            p1_somma = p1_somma + p1_dado;
            break;
        case 5:
            cout << ".-----." << endl;
            cout << "| o o |" << endl;
            cout << "|  o  |" << endl;
            cout << "| o o |" << endl;
            cout << "._____." << endl << endl;
            p1_ndadi--;
            break;
        case 6:
            cout << ".-----." << endl;
            cout << "| o o |" << endl;
            cout << "| o o |" << endl;
            cout << "| o o |" << endl;
            cout << "._____." << endl << endl;
            p1_somma = p1_somma + p1_dado;
            break;
        }

I should also mention that this piece of code is in a for loop so the switch case will be ran for 4-5 times. 我还要提一下,这段代码在for循环中,因此switch case将运行4-5次。 the way this outputs is all the dices on top of each other, is there a way to show all the dices in 1 "row" ? 这个输出的方式是彼此重叠的所有骰子,有没有办法在1“行”中显示所有骰子? Like: 喜欢:

1 2 3 4 5

instead of: 代替:

1
2
3
4
5

Thanks for the help :) 谢谢您的帮助 :)

Assuming you install something intercepting and rearranging the output the actual output can stay mostly unchanged. 假设您安装了拦截和重新排列输出的东西,实际输出可以保持不变。 Since I'd be inclined to key sending compiled lines off a flush for the stream, the only change really necessary is replacing the excessive use of std::endl by uses of '\\n' . 由于我倾向于从流的刷新中键入发送编译行,唯一真正需要的改变是使用'\\n'替换过度使用std::endl That's something I strongly recommend anyway (for an explanation have a look at the link ). 无论如何,这是我强烈建议的(有关解释,请查看链接 )。

So, assuming there is a something called dicebuf defined, the main() function could look like this (the interesting part is the first definition inside the function): 因此,假设有一个名为dicebuf的东西定义, main()函数可能看起来像这样(有趣的部分是函数内的第一个定义):

int main()
{
    dicebuf buf(std::cout);

    for (int i = 0; i != 5; ++i)
    {
        int p1_dado = rand() % 6 + 1;
        switch (p1_dado) {
        case 1:
            std::cout << ".-----." << '\n';
            std::cout << "|     |" << '\n';
            std::cout << "|  o  |" << '\n';
            std::cout << "|     |" << '\n';
            std::cout << "._____." << '\n' << '\n';
            break;
        case 2:
            std::cout << ".-----." << '\n';
            std::cout << "| o   |" << '\n';
            std::cout << "|     |" << '\n';
            std::cout << "|   o |" << '\n';
            std::cout << "._____." << '\n' << '\n';
            break;
        case 3:
            std::cout << ".-----." << '\n';
            std::cout << "|o    |" << '\n';
            std::cout << "|  o  |" << '\n';
            std::cout << "|    o|" << '\n';
            std::cout << "._____." << '\n' << '\n';
            break;
        case 4:
            std::cout << ".-----." << '\n';
            std::cout << "| o o |" << '\n';
            std::cout << "|     |" << '\n';
            std::cout << "| o o |" << '\n';
            std::cout << "._____." << '\n' << '\n';
            break;
        case 5:
            std::cout << ".-----." << '\n';
            std::cout << "| o o |" << '\n';
            std::cout << "|  o  |" << '\n';
            std::cout << "| o o |" << '\n';
            std::cout << "._____." << '\n' << '\n';
            break;
        case 6:
            std::cout << ".-----." << '\n';
            std::cout << "| o o |" << '\n';
            std::cout << "| o o |" << '\n';
            std::cout << "| o o |" << '\n';
            std::cout << "._____." << '\n' << '\n';
            break;
        }
    }
}

The idea here is that the dicebuf constructor injects a custom stream buffer which aggregates lines. 这里的想法是dicebuf构造函数注入一个聚合行的自定义流缓冲区。 It simply doesn't output things but just adds characters to a current row. 它只是不输出东西,只是添加字符到当前行。 If a newline ( '\\n' ) is encountered, it goes to the next row. 如果遇到换行符( '\\n' ),它将转到下一行。 If a two newlines in a row are encountered, it assumes it time to go back to the top and carry on adding to the current line. 如果遇到连续的两个换行符,则会假定有时间返回到顶部并继续添加到当前行。

Creating such a utility intercepting the output is done by implementing a suitable stream buffer, ie, a class derived from std::streambuf . 通过实现合适的流缓冲区(即,从std::streambuf派生的类)来创建拦截输出的这种实用程序。 Here is an example implementation: 这是一个示例实现:

#include <utility>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
#include <vector>

struct dicebuf
    : std::streambuf
{
    std::ostream&                       out;
    std::streambuf*                     sbuf;
    std::vector<std::string>            rows;
    std::vector<std::string>::size_type row;
public:
    dicebuf(std::ostream& out)
        : out(out)
        , sbuf(out.rdbuf())
        , rows(1)
        , row(0) {
        out.rdbuf(this);
    }
    ~dicebuf() {
        this->sync();
        this->out.rdbuf(this->sbuf);
    }
    int overflow(int c) {
        if (c != std::char_traits<char>::eof()) {
            if (c == '\n') {
                if (this->rows[this->row].empty()) {
                    this->row = 0;
                }
                else {
                    ++this->row;
                    if (this->rows.size() == this->row) {
                        this->rows.push_back(std::string());
                    }
                }
            }
            else {
                this->rows[this->row].push_back(c);
            }

        }
        return std::char_traits<char>::not_eof(c);
    }
    int sync() {
        std::ostream out(this->sbuf);
        std::copy(this->rows.begin(), this->rows.end(),
                  std::ostream_iterator<std::string>(out, "\n"));
        this->sbuf->pubsync();
        this->row = 0;
        this->rows.resize(0);
        this->rows.push_back(std::string());
        return 0;
    }
};

Together with the main() above it should produce the output as desired. 与上面的main()一起,它应该根据需要产生输出。 If you want multiple lines with results, you'll just inject a flush, eg, using 如果你想要多行结果,你只需注入一个刷新,例如,使用

std::flush;

at strategic points. 在战略要点。

Code snippet: 代码段:

char* hline = ".-----.";
char* dice[6][3] = { {"|     |", "|  o  |", "|     |" }, { ... }}; // 3 middle lines for each dice

for (int l=0; l < 5; ++l) {
  for(int i=0; i < 6; ++i) // or any random dice sequence with index = dice value - 1
    cout << (l == 0 || l == 4) ? hline : dice[i][l-1] << " ";
  cout << "\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM