将 Perl 移植到C++"打印"x{2501}" x 12";

Porting Perl to C++ `print "x{2501}" x 12;`

本文关键字:2501 打印 Perl C++      更新时间:2023-10-16

我正在将一个程序从Perl移植到C++作为学习目标。我到达了一个例程,该例程使用如下命令绘制表:

Perl: print "x{2501}" x 12;

它绘制了 12 次"━"("框图重水平")。

现在我已经解决了部分问题:

Perl: x{}, x00        Hexadecimal escape sequence;
C++:  unnnn

要打印单个 Unicode 字符:

C++:  printf( "u250fn" );

但是C++是否有一个智能等价物的"x"运算符,或者它会归结为一个 for 循环?


更新让我包括我尝试使用建议的解决方案编译的完整源代码。编译器确实会抛出错误:

g++ -Wall -Werror project.cpp -o project
project.cpp: In function ‘int main(int, char**)’:
project.cpp:38:3: error: ‘string’ is not a member of ‘std’
project.cpp:38:15: error: expected ‘;’ before ‘s’
project.cpp:39:3: error: ‘cout’ is not a member of ‘std’
project.cpp:39:16: error: ‘s’ was not declared in this scope

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
int main ( int argc, char *argv[] )
{
        if ( argc != 2 )   
        {
                fprintf( stderr , "usage: %s matrixn", argv[0] );
                exit( 2 );
        } else {
                //std::string s(12, "u250f" );
                std::string s(12, "u" );
                std::cout << s;
        }       
}

不,C++没有"x"运算符,但是您可以创建带有重复字符的字符串:

std::string s(12, <any character>);

然后你可以像下面这样打印它("printf"是从C继承而来的,在你的作业中使用它可能不好):

std::cout << s;

(当然,您可以使用任何数字,而不仅仅是 12)

以上更新的更新:

我只需稍作改动即可编译您的代码("u"替换为"u",因为它必须是一个字符)。我的编译器是GCC 4.6.2 for Windows XP(MINGW32版本)。您使用什么操作系统/编译器?