文字列のセンタリング
Posted feedbacks - C++
C++ では boost::format でセンタリングができるようです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <cstdlib>
#include <iostream>
#include <sstream>
#include "boost/format.hpp"
using namespace std;
using namespace boost;
string ¢er( string &str, int width ){
int len = str.length();
if( len > width ){
str = str.substr( (len-width+1)/2, width );
}
ostringstream oss;
oss << "%1$=" << width << "s";
str = ( format(oss.str()) % str ).str();
return str;
}
int main ( int argc, char *argv[] ){
int width( atoi(argv[2]) );
string str( argv[1] );
cout << center( str, width ) << endl;
return EXIT_SUCCESS;
}
|


nobsun
#4089()
Rating0/2=0.00
文字列を指定のカラム幅にセンタリング配置する関数を示してください。文字列の長さが指定した幅より長い場合には文字列の両端をできるだけ均等に切り落して指定幅に収めてください。1文字は1カラムに収まるものと仮定してかまいません。
[ reply ]