【问题标题】:Aligning output on the right in c++在 C++ 中将输出对齐到右侧
【发布时间】:2014-04-10 07:38:03
【问题描述】:
#include<iostream>
#include<iomanip>
using namespace std;

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

为什么这会导致如下输出:

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";
}

如果我试试这个,为什么“今天”部分会错位?

【问题讨论】:

    标签: c++


    【解决方案1】:

    改变算子的顺序来解决这个问题:

    #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;
    }
    
    • 您必须将所有运算符放在之前要格式化的值。
    • 避免使用using namespace std

    std::setw() 运算符将字段设置为下一个值。而std::leftstd::right 运算符设置该字段中值的位置。

    这个例子

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

    将创建这种格式:

    AAAAAAAAAABBBBBBBBBBBBBBBBBBBB
    word1                    word2
    

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

    AAAAAAAAAA....BBBBBBBBBBBBBBBBBBBB
    word1istoolong               word2
    

    第二个字段只是移动了字符数。流从不跟踪当前位置,它只是使用给定大小的“字段”构建输出。

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

    #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;
    }
    

    这个例子证明每一行在开始时填充给定页面。它的工作原理是将文本拆分为单词,用这些单词填充行,并证明每行与宽度完全匹配。

    【讨论】:

    • @AlexanderTobiasHeinrich 抱歉,我复制了错误的示例。现在它已修复并按预期工作。
    • @Flovdis 为什么要避免使用using namespace std
    • 如果 cout 像 cout
    • @user3340001 在这种情况下,只需将运算符放在“今天”字符串之前。所有运算符都应用于流中的所有后续值。
    • @user3340001 或者我可能误解了你的问题。你能解释一下“右对齐”是什么意思吗?
    【解决方案2】:

    问题是,在您调用setw 时,输出流不记得打印"Hello""Goodbye"

    试试这个来产生你想要的输出:

    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    int main()
    {
        cout << left << setw(10) << "Hello" << "World\n";
        cout << left << setw(10) << "Goodbye" << "World\n";
    }
    

    【讨论】:

      【解决方案3】:

      试试这个 - cout << right << "Hello" << "\t" << "World\n"; cout << right << "Goodbye" << "\t" << "World\n";

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-07
        • 1970-01-01
        • 2017-04-27
        • 2020-06-27
        • 2012-03-25
        • 2018-07-29
        • 1970-01-01
        相关资源
        最近更新 更多