c++ - 如何在 C++ 中并排显示两个函数?

标签 c++ function for-loop

我的代码的输出显示 function1()function2() 之上。有没有一种编码方式可以让输出在 function2() 旁边(如并排显示)显示 function1()

下面是我的代码片段:

#include <iostream>
using namespace std;

void function1()
{   
    int i;
    int array[5] = {5, 5, 5, 5, 5};

    cout << "   -1-2-3-4-5-" << endl;
    cout << "   |";
    for (i = 0; i < 5; ++i)
        cout << array[i] << "|";
        cout << endl;
}

void function2()
{   
    int i;
    char array[5] = {'A', 'A', 'A', 'A', 'A'};

    cout << "   -1-2-3-4-5-" << endl;
    cout << "   |";
    for (i = 0; i < 5; ++i)
        cout << array[i] << "|";
        cout << endl;
}

int main()
{

   function1();
   function2();

    return 0;
}

最佳答案

这是可能的,但并非完全没有技巧。

我在这里使用的技巧是将输出流 传递给函数,而不是让函数直接打印到std::cout。这允许我传递 std::stringstream到打印到那个的函数。 std::stringstream捕获输出供以后使用,我有一个功能可以一次打印一行,并排打印:

#include <sstream>
#include <iostream>

// output to a generic output stream
// rather than hard-coding std::cout
void function1(std::ostream& os)
{
    int i;
    int array[5] = { 5, 5, 5, 5, 5 };

    os << "   -1-2-3-4-5-" << '\n';
    os << "   |";
    for(i = 0; i < 5; ++i)
        os << array[i] << "|";
    os << '\n';
}

void function2(std::ostream& os)
{
    int i;
    char array[5] = { 'A', 'A', 'A', 'A', 'A' };

    os << "   -1-2-3-4-5-" << '\n';
    os << "   |";
    for(i = 0; i < 5; ++i)
        os << array[i] << "|";
    os << '\n';
}

// read each input stream line by line, printing them side by side
void side_by_side(std::istream& is1, std::istream& is2, std::size_t width)
{
    std::string line1;
    std::string line2;

    while(std::getline(is1, line1))
    {
        std::string pad;

        // ensure we add enough padding to make the distance
        // the same regardless of line length
        if(line1.size() < width)
            pad = std::string(width - line1.size(), ' ');

        // get same line from second stream
        std::getline(is2, line2);

        // print them size by the side the correct distance (pad)
        std::cout << line1 << pad << line2 << '\n';
    }

    // in case second stream has more line than the first
    while(std::getline(is2, line2))
    {
        auto pad = std::string(width, ' ');
        std::cout << pad << line2;
    }
}

int main()
{
    // some stream objects to store the outputs
    std::stringstream ss1;
    std::stringstream ss2;

    // capture output in stream objects
    function1(ss1);
    function2(ss2);

    // print captured output side by side
    side_by_side(ss1, ss2, 30);
}

输出:

   -1-2-3-4-5-                   -1-2-3-4-5-
   |5|5|5|5|5|                   |A|A|A|A|A|

关于c++ - 如何在 C++ 中并排显示两个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38893113/

相关文章:

javascript - 循环以在 Javascript 中生成数据点

c++ - 为什么异常的析构函数被调用两次?

c++ - 具有非常快速的 int-> 数据查找和快速反向查找(搜索/插入/删除数据)的压缩字典?

SQL错误: String or binary data would be truncated

php - 如何使当前页面具有不同的样式(PHP?page href)?

c - 我的 C 书中的神奇数字 127

c++ - 使用另一个宏的宏的奇怪行为

c++ - 重命名变量类型

bash - 如何记录shell函数?

r - 测试 R 中是否设置了函数的参数