简体   繁体   English

如何对齐不同长度的输出C ++

[英]how to align different lengthed outputs C++

I have tried to search many post on this, and I still cant figure this out. 我已经尝试搜索许多关于此的帖子,但仍然无法弄清楚。 say I have a loop where it reads lines from a text file and then displays them on the screen. 说我有一个循环,它从文本文件中读取行,然后在屏幕上显示它们。 I want each peice of data to be aligned, but the problem is the data can be diferent legnths so using a static value in setw is not working. 我希望每个数据的位置都对齐,但是问题是数据可能是不同的长度,因此在setw中使用静态值不起作用。 for example here is a text file 例如,这是一个文本文件

000000  apples      pears     2.00
000001  oranges     bannana   1.00

this is how I want it to look on the screen, but my screen looks like this 这就是我希望它在屏幕上显示的样子,但是我的屏幕看起来像这样

000000  apples      pears     2.00
000001 oranges    banana   1.00

it is seeing that oranges is 1 longer than apples and then it is moving it over 1 to the left. 看到橘子比苹果长1,然后将它向左移动1。 it does the same thing with banana and pears 用香蕉和梨做同样的事情

how do I align it like the first example(which is how it looks in my text file) 我如何像第一个示例一样对齐它(它在我的文本文件中的外观)

this is what I am using: 这就是我正在使用的:

cout << account << setw(10) << fruit << setw(10) << fruit2 << setw(10) << money << endl;

I feel like the setw(10) needs to be non static. 我觉得setw(10)必须是非静态的。 I have also tried left right and that does not work either. 我也尝试过左右,但这也不起作用。

Here is how I solved this problem, if you are using std::string: 如果您使用的是std :: string,这就是我解决此问题的方法:

cout << left << setw(10) << account << setw(10) << fruit << setw(10 + strlen(fruit2.c_str()) -  strlen(fruit.c_str())) << fruit2 << endl;   

It takes the length of the string into account with the formatting. 它考虑格式时字符串的长度。 You can choose an arbitrary offset as long as the fruit names don't get too long ("strawberries" in place of "apples" breaks the format with 您可以选择一个任意的偏移量,只要水果名称不要太长即可(用“草莓”代替“苹果”会用

setw(10)

This makes little or no sense--if you're really getting that result, it sounds like a problem with your compiler. 这几乎没有意义-如果您确实得到了结果,这听起来像是您的编译器有问题。 I wrote a quite demo, similar to what you have in the question: 我编写了一个非常演示,类似于您在问题中的演示:

#include <iostream>
#include <iomanip>
#include <string>
#include <iterator>

struct account {
    std::string acct;
    std::string fruit;
    std::string fruit2;
    double qty;

    friend std::ostream &operator<<(std::ostream &os, account const &a) {
        return os << a.acct << std::setw(10) 
                << a.fruit << std::setw(10)
                << a.fruit2 << std::setw(5)
                << a.qty;
    }   
};

int main() { 
    account accts[] = {
        { "0000", "apples", "pears", 2.0 },
        { "0001", "oranges", "banana", 1.0 }
    };

    std::copy_n(accts, 2, std::ostream_iterator<account>(std::cout, "\n"));
}

The result is produced is aligned as anybody who understood setw would expect: 产生的结果与了解setw任何人都一致:

0000    apples     pears    2
0001   oranges    banana    1

Each item is right-aligned in its field, just as you'd expect from the specification. 就像您期望的那样,每个项目在其字段中都右对齐。

You're simply missing the std::ios_base::left flag. 您只是缺少了std :: ios_base :: left标志。

std::cout << std::setw(10) << std::setiosflags(std::ios_base::left)
      << "x" << "y" << std::endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM