将 Perl 移植到C++"打印"x{2501}" x 12";
Porting Perl to C++ `print "x{2501}" x 12;`
我正在将一个程序从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版本)。您使用什么操作系统/编译器?
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 回溯C++不打印函数,因此文件
- 在一定长度后从数组中打印时缺少整数
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 在gem5中打印文件中的所有cache_blocks
- 打印数字图案
- Log4cpp:以UTC/GMT时区打印日期
- 将 Perl 移植到C++"打印"x{2501}" x 12";