【问题标题】:How to display two functions next to each other in C++?如何在 C++ 中相邻显示两个函数?
【发布时间】:2016-08-11 09:59:09
【问题描述】:

我的代码输出在function2() 上方显示function1()。有没有办法编写代码,以便输出在function2() 旁边(如并排)显示function1()

下面是我的代码的 sn-p:

#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;
}

【问题讨论】:

  • 不要在cout of function1()中使用endl
  • 嗯,我猜他想要一个像 5
  • @Anedar 完全正确。两个函数的第一行、第二行等将彼此相邻。

标签: c++ function for-loop


【解决方案1】:

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

我在这里使用的技巧是将输出流 传递给函数,而不是让函数直接打印到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|

【讨论】:

    【解决方案2】:

    不,没有。

    你先调用function1(),然后再调用function2(),所以它们一个接一个地被执行。

    但是,您可以做的是,让这些函数仅将数据作为数组或向量返回,然后在收集完两个结果后处理输出。

    另一种选择是让函数始终只处理一次迭代(您可能希望为此使用static int i 或类似方法),然后在函数调用之外循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 2014-08-23
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 2020-05-08
      相关资源
      最近更新 更多