在C ++中填充stl字符串 [英] Padding stl strings in C++

查看:202
本文介绍了在C ++中填充stl字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 std :: string ,需要将它们左移到给定的宽度。在C ++中推荐的方法是什么?

I'm using std::string and need to left pad them to a given width. What is the recommended way to do this in C++?

输入示例:

123

填入10个字符。

示例输出:

       123

(7个空格,123之前)

推荐答案

std :: setw(setwidth)manipulator

std::setw (setwidth) manipulator

std::cout << std::setw (10) << 77 << std::endl;

std::cout << std::setw (10) << "hi!" << std::endl;

输出已填充77和hi!。

outputs padded 77 and "hi!".

如果您需要结果作为字符串使用std :: stringstream的实例而不是std :: cout对象。

if you need result as string use instance of std::stringstream instead std::cout object.

ps:负责的头文件 ; iomanip>

ps: responsible header file <iomanip>

这篇关于在C ++中填充stl字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆