我将一个程序从 Perl 移植到 C++ 作为学习目标。我到达了一个用如下命令绘制表格的例程:
Perl: print "\x{2501}" x 12;
它绘制了 12 次 '━'(“方框图重水平”)。
现在我已经发现了部分问题:
Perl: \x{}, \x00 Hexadecimal escape sequence;
C++: \unnnn
要打印单个 Unicode 字符:
C++: printf( "\u250f\n" );
但是 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 matrix\n", argv[0] );
exit( 2 );
} else {
//std::string s(12, "\u250f" );
std::string s(12, "u" );
std::cout << s;
}
}