在 C++ 中使用 cout 打印输出中的绝对定位?

发布于 2024-08-20 20:12:52 字数 967 浏览 5 评论 0原文

如何使用 cout 获得“绝对定位”的列,使文本左对齐并右对齐数字?

#include <iostream>
#include <iomanip>
using namespace std;

struct Human {
    char name[20];
    char name2[20];
    char name3[20];
    double pts;
};

int main() {
    int i;
    Human myHumen[3] = {
        {"Mr", "Alan", "Turing", 12.25},
        {"Ms", "Ada", "Lovelace", 15.25},
        {"Sir",  "Edgar Allan", "Poe", 45.25}
    };
    cout << "Name1" << setw(22) << "Name2" << setw(22) << "Name3" << setw(22) << "Rating" <<endl;
    cout << "-----------------------------------------------------------------------\n";
    for(i = 0; i < 3; i++) {
        cout << myHumen[i].name << setw(22) << myHumen[i].name2 << setw(22) << myHumen[i].name3 << setw(20) << myHumen[i].pts << endl;
    }//this didn't do nice printouts, with leftalign for text and rightalign with numbers
}

How do you get "absolutely positioned" columns with cout, that leftaligns text and right-aligns numbers?

#include <iostream>
#include <iomanip>
using namespace std;

struct Human {
    char name[20];
    char name2[20];
    char name3[20];
    double pts;
};

int main() {
    int i;
    Human myHumen[3] = {
        {"Mr", "Alan", "Turing", 12.25},
        {"Ms", "Ada", "Lovelace", 15.25},
        {"Sir",  "Edgar Allan", "Poe", 45.25}
    };
    cout << "Name1" << setw(22) << "Name2" << setw(22) << "Name3" << setw(22) << "Rating" <<endl;
    cout << "-----------------------------------------------------------------------\n";
    for(i = 0; i < 3; i++) {
        cout << myHumen[i].name << setw(22) << myHumen[i].name2 << setw(22) << myHumen[i].name3 << setw(20) << myHumen[i].pts << endl;
    }//this didn't do nice printouts, with leftalign for text and rightalign with numbers
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青柠芒果 2024-08-27 20:12:52

您使用“左”和“右”操纵器:

cout << std::left  << std::setw(30) << "This is left aligned"
     << std::right << std::setw(30) << "This is right aligned";

带有文本+数字的示例:

typedef std::vector<std::pair<std::string, int> > Vec;
std::vector<std::pair<std::string, int> > data;
data.push_back(std::make_pair("Alan Turing", 125));
data.push_back(std::make_pair("Ada Lovelace", 2115));

for(Vec::iterator it = data.begin(), e = data.end(); it != e; ++it)
{
    cout << std::left << std::setw(20) << it->first
         << std::right << std::setw(20) << it->second << "\n";
}

打印:

Alan Turing                          125
Ada Lovelace                        2115

You use the "left" and "right" manipulators:

cout << std::left  << std::setw(30) << "This is left aligned"
     << std::right << std::setw(30) << "This is right aligned";

An example with text + numbers:

typedef std::vector<std::pair<std::string, int> > Vec;
std::vector<std::pair<std::string, int> > data;
data.push_back(std::make_pair("Alan Turing", 125));
data.push_back(std::make_pair("Ada Lovelace", 2115));

for(Vec::iterator it = data.begin(), e = data.end(); it != e; ++it)
{
    cout << std::left << std::setw(20) << it->first
         << std::right << std::setw(20) << it->second << "\n";
}

Which prints:

Alan Turing                          125
Ada Lovelace                        2115
-黛色若梦 2024-08-27 20:12:52

这会有点不受欢迎,但您可以使用 printf 来执行这种快速程序。格式化字符串更容易理解和使用(对于熟悉 iostreams 和 printf 的人来说)。

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>

struct Human {
  char name[20];   // consider using std::string
  char name2[20];
  char name3[20];
  double pts;
};

int main() {
  using namespace std;
  Human people[3] = {
    {"Mr", "Alan", "Turing", 12.25},
    {"Ms", "Ada", "Lovelace", 15.25},
    {"Sir",  "Edgar Allan", "Poe", 45.25}
  };
  printf("%-22s%-22s%-22s%20s\n", "Name1", "Name2", "Name3", "Rating");
  cout << string(22 * 3 + 20, '-') << '\n';
  for (int i = 0; i < 3; i++) {
    Human const& h = people[i];
    printf("%-22s%-22s%-22s%20f\n", h.name, h.name2, h.name3, h.pts);
  }
  return 0;
}

混合 cout 和 printf 是安全的(默认情况下,请参见 std::sync_with_stdio),并且一个好的编译器也可以为您检查格式字符串的类型(gcc 中的 -Wall)。

This is going to be slightly unpopular, but you can just use printf for this kind of quick program. The formatting strings are easier to understand and use (given someone who groks both iostreams and printf).

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>

struct Human {
  char name[20];   // consider using std::string
  char name2[20];
  char name3[20];
  double pts;
};

int main() {
  using namespace std;
  Human people[3] = {
    {"Mr", "Alan", "Turing", 12.25},
    {"Ms", "Ada", "Lovelace", 15.25},
    {"Sir",  "Edgar Allan", "Poe", 45.25}
  };
  printf("%-22s%-22s%-22s%20s\n", "Name1", "Name2", "Name3", "Rating");
  cout << string(22 * 3 + 20, '-') << '\n';
  for (int i = 0; i < 3; i++) {
    Human const& h = people[i];
    printf("%-22s%-22s%-22s%20f\n", h.name, h.name2, h.name3, h.pts);
  }
  return 0;
}

It is safe (by default, see std::sync_with_stdio) to mix cout and printf, and a good compiler can check the types of the format strings for you, too (-Wall in gcc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文