簡體   English   中英

如何正確使用 setw() 格式化輸出?

[英]How to properly use setw() to format output?

我正在嘗試將我的輸出格式化為:

1 [tab] First Name: John [tab] Last Name: Smith [tab] Age: 20 [tab]daysInCourse: {35, 40, 55} Degree Program: Security

我目前的代碼是:

{
    cout << left << setw(15) << studentID;
    cout << left << setw(15) << "First Name: " << FN;
    cout << left << setw(15) << "Last Name: " << LN;
    cout << left << setw(15) << "Email " << studentEmail;
    cout << left << setw(15) << "Age: " << age;
    cout << left << setw(15) << "{" << days[0] << ", " << days[1] << ", " << days[2];
    cout << left << setw(15) << "Degree Program: ";
}

我已經玩過每一行的 setw 值,但似乎無法正確處理。 setw 函數是否只需要與特定值一起使用?

注意:我擔心您發布的數據包含真實的電子郵件地址和真實人物的信息,這些信息可能是也可能不是私密的和受保護的。 如果是這樣,我認為這是對隱私的侵犯,這是非法的。 即使獲得同意,也無需在本論壇上發布私人數據; 我建議你從你的帖子中刪除這些數據,以后不要再這樣做了。

如評論中所述, setw操作符僅適用於下一個字符串,因此

cout << left << setw(15) << "{" << days[0] << ", " << days[1] << ", " << days[2];

將僅為字符{設置 15 的寬度(與"First Name: "等類似)。 還值得注意的是,如果一個字符串超過指定的寬度,那么它將推送下一個內容並打破您的列的對齊方式; 所以你需要考慮最大可能的內容來設置寬度。

這是一個實現您想要的工作示例,打印之前使用stringstream形成字符串(以便setw應用於整個事物):

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>

// ------------------------------------------------------------------------

struct Student 
{
    std::string ID, First, Last, Email, Degree;
    unsigned age;
    std::vector<unsigned> days;

    Student( std::string ID, std::string First, std::string Last, unsigned age, 
        std::string Email, std::string Degree, std::vector<unsigned> days ) 
    : ID(ID), First(First), Last(Last), age(age), Email(Email), Degree(Degree), days(days)
    {}
};

std::ostream& operator<< ( std::ostream& os, const Student& s ) {
    std::ostringstream ss;

    ss << "First Name: " << s.First;
    os << std::left << std::setw(25) << ss.str();
    ss.str("");

    ss << "Last Name: " << s.Last;
    os << std::left << std::setw(35) << ss.str();
    ss.str("");

    ss << "Email: " << s.Email;
    os << std::left << std::setw(50) << ss.str();
    ss.str("");

    ss << "Age: " << s.age;
    os << std::left << std::setw(10) << ss.str();
    ss.str("");

    ss << "{" << s.days.at(0) << ", " << s.days.at(1) << ", " << s.days.at(2) << "}";
    os << std::left << std::setw(20) << ss.str();
    ss.str("");

    ss << "Degree Program: " << s.Degree;
    os << std::left << std::setw(30) << ss.str();
    ss.str("");

    return os << std::endl;
}

// ------------------------------------------------------------------------

int main() {
    std::cout << Student("A3","Robert","Smith",19,"example@foo.com","SOFTWARE",{20,40,33});
    std::cout << Student("A4","Alice","Smith",22,"example@bar.net","SECURITY",{50,58,40});
}

輸出:

First Name: Robert       Last Name: Smith                   Email: example@foo.com                            Age: 19   {20, 40, 33}        Degree Program: SOFTWARE      
First Name: Alice        Last Name: Smith                   Email: example@bar.net                            Age: 22   {50, 58, 40}        Degree Program: SECURITY 

請注意,我為每個字段設置了不同的寬度,具體取決於內容的預期長度。 例如,電子郵件地址可能很長,但年齡很少會超過兩位數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM