在C+中格式化文本+; 所以我想为我的第一个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'; cout

在C+中格式化文本+; 所以我想为我的第一个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'; cout,c++,text,formatting,C++,Text,Formatting,您可以使tierce函数变得简单 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 = "

您可以使tierce函数变得简单

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

获取或编写一个库函数来自动插入适合特定输出宽度的前导空格和换行符是一个好主意。这个网站考虑了图书馆的建议,但我在下面提供了一些代码——虽然不是特别有效,但很容易理解。基本逻辑应该是在字符串中向前跳转到最大宽度,然后向后移动,直到找到空白(或者可能是连字符),此时您准备断开该行。。。然后打印行首空白和行的剩余部分。继续,直到完成

#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?"); 

}
#包括
std::string fmt(大小边距、大小宽度、std::string文本)
{
std::字符串结果;
while(!text.empty())//while需要移动到结果中的更多文本
{
结果+=标准::字符串(边距“”;//为此行添加边距
if(width>=text.size())//文本的其余部分可以容纳…非常简单
返回(结果+=文本)+='\n';
size\u t n=width-1;//首先假设可以容纳n个字符
而(n>width/2&&isalnum(文本[n])&&isalnum(文本[n-1]))
--n、 //字符之间;减少n,直到分词或剩余1/2宽度
//将n个字符从文本移动到结果。。。
(结果+=text.substr(0,n))+='\n';
文本擦除(0,n);
}
返回结果;
}
int main()
{

std::cout在github上有一个用于处理文本格式的漂亮库


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

使用头文件,它为文本提供了各种功能formatting@RohitSaluja,你能说得更具体一点吗?我在列表中看到的唯一一个是
setw()
,我认为这是行不通的。或者你可以发布一个你将如何做到这一点的例子吗?这很简单,也很有用,谢谢你。不过,我必须做些什么来防止它切碎单词?我对编码非常陌生,我认为这比我的技能水平有点高。我对这种事情还不存在感到非常惊讶。杜生病了吗德,这很好用!我打算研究一下它是如何工作的,但我真的很感动。谢谢你这么快的回复和这么专业的知识!@Hammurabi8:当然-不用担心。我添加了一些评论/解释-希望这也有帮助。干杯。
#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?"); 

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