【问题标题】:How can I add horizontal space between two functions in C++?如何在 C++ 中的两个函数之间添加水平空间?
【发布时间】:2021-12-26 07:22:04
【问题描述】:

所以我想输出一个名称列表和随机生成的数字。我已经做了其他所有事情,只是我不知道如何按我想要的方式输出它。

我希望它像这样输出:

ID #:       Names: 
1            bob
23           rob
44          kanye

这是我目前所拥有的:

cout << "Would you like to view the archived names and IDs? (Y/N)" << endl;
        string archiveInput;
        cin >> archiveInput;
        if(tolower(archiveInput[0]) == 'y')
        {
            cout << "ID #:    Names: " << endl;
            
            output(ids, names);
            
        }

这是我使用的函数。

void output(const vector<int>& ids)
{
    for(int i = 0; i < ids.size(); i++)
    {
        cout << ids[i] << endl;
    }
    cout << endl;
}

void output(const vector<string>& names)
{
    for(int i = 0; i < names.size(); i++)
    {
        cout << names[i] << endl; //might have to use endl for list format
    }
    cout << endl;
}

void output(const vector<int>& ids, const vector<string>& names)
{
    cout << output(ids) << "       " << output(names); //I thought this would work, im new :(
}

【问题讨论】:

  • 也许先尝试更多的东西。如果只能从上到下,从左到右,你会怎么手写呢?

标签: c++ vector output


【解决方案1】:

试试这个方法

#include <iostream>
#include <iomanip>

using namespace std;

class Student
{
public:
    string studentName;
    int studentAge;
    int studentMarks;
    int admissionYear;

    Student(string name, int age, int marks, int year)
    {
        studentName = name;
        studentAge = age;
        studentMarks = marks;
        admissionYear = year;
    }
};

int main()
{
    Student studentArray[4] = {Student("Alex", 20, 80, 2018), Student("Bob", 21, 82, 2018), Student("Chandler", 23, 85, 2017), Student("Rose", 18, 89, 2019)};

    cout
        << left
        << setw(10)
        << "Name"
        << left
        << setw(5)
        << "Age"
        << left
        << setw(8)
        << "Marks"
        << left
        << setw(5)
        << "Year"
        << endl;

    for (int i = 0; i < 4; i++)
    {
        cout
            << left
            << setw(10)
            << studentArray[i].studentName
            << left
            << setw(5)
            << studentArray[i].studentAge
            << left
            << setw(8)
            << studentArray[i].studentMarks
            << left
            << setw(5)
            << studentArray[i].admissionYear
            << endl;
    }
    return 0;
}

它将打印以下输出:

Name      Age  Marks   Year
Alex      20   80      2018
Bob       21   82      2018
Chandler  23   85      2017
Rose      18   89      2019

我们为每一列设置了不同的宽度。第一列宽10,第二列宽5,第三列宽8,最后一列宽5。

宽度在这里很重要。如果小于其内容的大小,内容就会溢出。

【讨论】:

  • 请解释你的答案。
  • 不是魔法和看似随意的数字的忠实粉丝。但它可能非常适合 OP。
  • 谢谢!没想到会这么复杂,但这应该可行!
【解决方案2】:
cout << output(ids) << "       " << output(names);

首先运行带有 ids 参数的函数输出,然后 afterwards 运行名称如下的版本:

for(int i = 0; i < ids.size(); i++)
{
    cout << ids[i] << endl;
}
cout << endl;
// returns from first function call
cout << "              ";
// enters second function call
for(int i = 0; i < names.size(); i++)
{
    cout << names[i] << endl; //might have to use endl for list format
}
cout << endl;

编译器将如何运行它,这就是为什么你的输出在彼此之下的原因。

这段代码是你想要的带有两个参数的重载:

for(int i = 0; i < ids.size(); i++)
    {
        cout << ids[i] << "           " << names[i] << endl;
    }
    cout << endl;

但是,您的输出仍然不会像您想要的那样。您必须为此应用其他技巧。

这就是您当前的代码行为如此的原因。查看 M Khaidar 的答案,了解解决问题的正确方法。

【讨论】:

    猜你喜欢
    • 2021-07-30
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多