在C ++中将输出对齐到右边 [英] Aligning output on the right in c++

查看:87
本文介绍了在C ++中将输出对齐到右边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

int main()
{
    cout << right << "Hello" << setw(10) << "World\n";
    cout << right << "Goodbye"  << setw(10) << "World\n";
}

为什么会这样显示输出:

Why does this result in output like:

Hello    World
Goodbye    World

而不是:

Hello    World
Goodbye  World

我在做什么错了?

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

int main()
{
    cout    << "Hello"  <<  " World";
    cout << right << setw(10) << "today\n";
    cout   << "Goodbye"  <<  " World";
    cout << right << setw(10) << "today\n";
}

如果我尝试这样做,为什么今天"部分未对齐?

If I try this, why is the "today" part misaligned?

推荐答案

更改操作员的顺序以解决此问题:

Change the order of the operators to solve this problem:

#include <iostream>
#include <iomanip>

int main()
{
    std::cout << std::left << std::setw(10) << "Hello" << "World\n";
    std::cout << std::left << std::setw(10) << "Goodbye" << "World\n";
    return 0;
}

  • 您必须将所有运算符放在要设置格式的值之前.
  • 避免使用使用命名空间std .
  • std :: setw()运算符将字段设置为下一个值.然后 std :: left std :: right 运算符设置该值在此字段中的位置.

    The std::setw() operator sets the field with for the next value. And the std::left or std::right operators set the position of the value in this field.

    这个例子

    std::cout << std::left << std::setw(10) << "word1"
        << std::right << std::setw(20) << "word2" << std::endl;
    

    将创建这种格式:

    AAAAAAAAAABBBBBBBBBBBBBBBBBBBB
    word1                    word2
    

    您看到第一个带有10个字符的字段"放置了第一个文本,第二个带有20个字符的第二个字段"放置了第二个单词右对齐.但是,如果第一个字段中的文本比该字段长,则会发生这种情况:

    You see there is a first "field" with 10 characters in which the first text is placed, and a second "field" with 20 characters in which the second word is placed right aligned. But if the text in the first field is longer than the field, this happens:

    AAAAAAAAAA....BBBBBBBBBBBBBBBBBBBB
    word1istoolong               word2
    

    第二个字段仅移动了字符数.流永远不会跟踪当前位置,它只是使用给定大小的字段"构建输出.

    The second field is just shifted the number of characters. The stream never keeps track of the current position, it just build the output using "fields" of a given size.

    要使用给定的页面来证明文本的正确性,请使用如下代码:

    To justify text for a given page with, use code like this:

    #include <iostream>
    #include <sstream>
    #include <list>
    
    const int pageWidth = 78;
    typedef std::list<std::string> WordList;
    
    WordList splitTextIntoWords( const std::string &text )
    {
        WordList words;
        std::istringstream in(text);
        std::string word;
        while (in) {
            in >> word;
            words.push_back(word);
        }
        return words;
    }
    
    void justifyLine( std::string line )
    {
        size_t pos = line.find_first_of(' ');
        if (pos != std::string::npos) {
            while (line.size() < pageWidth) {
                pos = line.find_first_not_of(' ', pos);
                line.insert(pos, " ");
                pos = line.find_first_of(' ', pos+1);
                if (pos == std::string::npos) {
                    pos = line.find_first_of(' ');
                }
            }
        }
        std::cout << line << std::endl;
    }
    
    void justifyText( const std::string &text )
    {
        WordList words = splitTextIntoWords(text);
    
        std::string line;
        for (WordList::const_iterator it = words.begin(); it != words.end(); ++it) {
            if (line.size() + it->size() + 1 > pageWidth) { // next word doesn't fit into the line.
                justifyLine(line);
                line.clear();
                line = *it;
            } else {
                if (!line.empty()) {
                    line.append(" ");
                }
                line.append(*it);
            }
        }
        std::cout << line << std::endl;
    }
    
    int main()
    {
        justifyText("This small code sample will format a paragraph which "
            "is passed to the justify text function to fill the "
            "selected page with and insert breaks where necessary. "
            "It is working like the justify formatting in text "
            "processors.");
        return 0;
    }
    

    该示例说明每一行在开始时用给定的页面填充.它通过将文本拆分为单词,然后用这些单词填充行,并证明每一行与宽度完全匹配来进行工作.

    This example justifies each line to fill the given page with at the begin. It works by splitting the text into words, filling lines with these words, and justifies each line to exactly match the width.

    这篇关于在C ++中将输出对齐到右边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆