c++ - 控制台输出线的宽度限制

标签 c++

Class 向控制台打印一个字符串。 如何使输出行的宽度等于 characterWidth = 40, 即在 40 个字符被转移到一个新行之后?

#include <string>
#include <iostream>

class StringProcessing {
  public:
      StringProcessing() : characterWidth(40),
                     textToBeFormatted("NULL") {}

      inline void StringProcessing::initString() {
          textToBeFormatted =
              "text    some text   some text some text   some text some text"
              "text some text  some text    some text some text some text"
              "text    some      text some    text some   text some text"
              "text some  text some    text some    text some text some text"
              "text   some     text some text some   text some text some text";
      }

      inline void displayString()
        { std::cout << textToBeFormatted << std::endl; }
  private:    
      int characterWidth;
      std::string textToBeFormatted;
};

我有一个想法但是这里console中的字被截断了,所以需要调到下一行进行宽度对齐

inline void displayString()
      {
         const std::string& s = textToBeFormatted;
         for (int i = 0; i < s.length() / 40 + (bool)(s.length() % 40); ++i)
         {
            std::cout << std::left
                      << std::setfill(' ')
                      << std::setw(40)
                      << s.substr(i * 40, 40)
                      << std::endl;
         }
      }

最佳答案

这是适合我的答案

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

class StringProcessing
{
public:
    StringProcessing() : characterWidth(40),
        textToBeFormatted("NULL") {}

    inline void initString() {
        textToBeFormatted = "text    some text   some text some text   some text some text"
            "text some text  some text    some text some text some text"
            "text    some      text some    text some   text some text some text"
            "text some  text some    text some    text some text some text"
            "text   some     text some text some   text some text some text";
    }

    inline void displayString()
    {
        const std::string& s = textToBeFormatted;
        const int& width = characterWidth;
        for (int current = 0; current < s.length();)
        {
            if (s.length() < width)
            {
                output(s);
                break;
            }
            if (s.length() - current < width)
            {
                output(s.substr(current));
                break;
            }
            std::string substr = s.substr(current, width);
            current += width;
            size_t space = substr.rfind(' ');
            if (space != std::string::npos && (substr[width - 1] != ' ' &&
                (s.length() > current && s[current] != ' ')))
            {
                current -= width - space - 1;
                substr = substr.substr(0, space + 1);
            }
            output(substr);
        }
    }
private:
    inline void output(const std::string& s)
    {
        std::cout << setfill(' ') << std::right << std::setw(characterWidth) << s << std::endl;
    }
    int characterWidth;
    std::string textToBeFormatted;
};

关于c++ - 控制台输出线的宽度限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21486958/

相关文章:

c++ - 停止接受新连接

c++ - 试图理解 C++ 中的基本正则表达式函数

具有不同参数的派生类中的 C++ 方法

c++ - 如何在程序中打开用户自定义的扩展文件?

c++ - 在提供 JSON 数据的 C++/Qt(充当服务器)中创建简单的 WebService

c++ - 在对所需抽象基的引用不起作用的地方传递了临时 concreate 派生对象?

c++ - 使用专门函数的异构集合的 STL 算法

具有较小学习曲线的 C++ 图形 API - linux

c++ - 追踪奇怪的错误

c++ - 如果一个函数定义有一个类模板类型的参数并且没有使用它(它的成员)那么它是否被实例化?