【问题标题】:Formatting text in C++在 C++ 中格式化文本
【发布时间】:2015-02-16 04:41:39
【问题描述】:

所以我正在尝试为我的第一个真正的 C++ 项目制作一个基于文本的小游戏。当游戏需要一大段文字、几段文字时,我很难让它看起来不错。我想要统一的行长,为此我只需要在适当的地方手动输入换行符。有命令为我做这件事吗?我见过setw() 命令,但是如果它超过宽度,它不会使文本换行。有什么建议吗?如果有帮助,这就是我现在正在做的事情。

cout << "    This is essentially what I do with large blocks of" << '\n';
cout << "    descriptive text. It lets me see how long each of " << '\n';
cout << "    the lines will be, but it's really a hassle, see? " << '\n';            

【问题讨论】:

  • 使用头文件它提供了各种文本格式化功能
  • @RohitSaluja ,你能更具体一点吗?我在找到的列表中看到的唯一一个是setw(),我认为这不起作用。或者您可以发布一个示例来说明您将如何做到这一点?

标签: c++ text formatting


【解决方案1】:

获取或编写一个库函数来自动插入适合特定输出宽度的前导空格和换行符将是一个好主意。这个网站认为图书馆推荐是题外话,但我在下面包含了一些代码 - 不是特别有效但易于理解。基本逻辑应该是在字符串中向前跳转到最大宽度,然后向后移动,直到找到准备换行的空格(或者可能是连字符)......然后打印前导空格和剩余的线的一部分。继续直到完成。

#include <iostream>

std::string fmt(size_t margin, size_t width, std::string text)
{
    std::string result;
    while (!text.empty())   // while more text to move into result
    {
        result += std::string(margin, ' ');  // add margin for this line

        if (width >= text.size())  // rest of text can fit... nice and easy
            return (result += text) += '\n';

        size_t n = width - 1;  // start by assuming we can fit n characters
        while (n > width / 2 && isalnum(text[n]) && isalnum(text[n - 1]))
            --n; // between characters; reduce n until word breaks or 1/2 width left

        // move n characters from text to result...
        (result += text.substr(0, n)) += '\n';
        text.erase(0, n);
    }
    return result;
}

int main()
{
    std::cout << fmt(5, 70,
        "This is essentially what I do with large blocks of "
        "descriptive text. It lets me see how long each of "
        "the lines will be, but it's really a hassle, see?"); 

}

这还没有经过彻底的测试,但似乎有效。看到它运行here。对于一些替代方案,请参阅this SO question

顺便说一句,您的原始代码可以简化为...

cout << "    This is essentially what I do with large blocks of\n"
        "    descriptive text. It lets me see how long each of\n"
        "    the lines will be, but it's really a hassle, see?\n"; 

...因为 C++ 认为该语句未完成,直到它碰到分号,并且在代码中彼此相邻出现的双引号字符串文字会自动连接,就好像内部双引号和空格已被删除一样。

【讨论】:

  • 我对编码很陌生,我认为这有点超出我的技能水平。我很惊讶这种事情还不存在。
  • 恶心的家伙,完美!我将研究它一段时间以弄清楚它是如何工作的,但我真的印象深刻。感谢您如此迅速地回复并拥有如此专业的知识!
  • @Hammurabi8:当然——不用担心。我添加了一些更多的 cmets/解释 - 希望这也有帮助。干杯。
【解决方案2】:

有漂亮的库来处理文本格式,在github上命名为cppformat/cppformat

通过使用它,您可以轻松地操作文本格式。

【讨论】:

    【解决方案3】:

    你可以做一个简单的函数

    void print(const std::string& brute, int sizeline)
    {
         for(int i = 0; i < brute.length(); i += sizeline)
              std::cout << brute.substr(i, sizeline) << std::endl;
    }
    
    int main()
    {
         std::string mytext = "This is essentially what I do with large blocks of"
                              "descriptive text. It lets me see how long each of "
                              "1the lines will be, but it's really a hassle, see?";
    
         print(mytext, 20);
    
         return 0;
    }
    

    但是字会被删减

    输出:

    This is essentially 
    what I do with large
     blocks ofdescriptiv
    e text. It lets me s
    ee how long each of 
    1the lines will be, 
    but it's really a ha
    ssle, see?
    

    【讨论】:

    • 这个简单实用,谢谢。不过,我该怎么做才能防止它断词?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多