【问题标题】:C++ equivalent of Python '#'*number? [duplicate]C++ 等价于 Python '#'*number? [复制]
【发布时间】:2017-04-15 02:09:00
【问题描述】:

是否存在与'#'*number 等效的单行代码,如果数字为 4,则该表达式的输出将是 C++ 中的"####"?如果没有等效的单线,我想知道是否有任何不包含 for 循环的快速方法。

注意:我正在使用 C++ 14,在我正在使用的程序上我不能使用 C++1z (17),所以没有 C++1z 建议。

【问题讨论】:

标签: python c++ c++14


【解决方案1】:

正如我在 cmets 中所说,c++ 中的 std::string 中有一个填充构造函数。

string(size_t n, char c);

http://www.cplusplus.com/reference/string/string/string/

【讨论】:

    【解决方案2】:

    作为std::string constructor provides an option。可以简单地做到:

    std::string(4, '#');
    

    Sample program:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        cout << string(4, '#') << endl;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-11
      • 2011-11-25
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      相关资源
      最近更新 更多