如何使用一个填充空间使用std:out setfill std :: setw std:right格式化文本 [英] How to format text using std:out setfill std::setw std:right with one padding space

查看:96
本文介绍了如何使用一个填充空间使用std:out setfill std :: setw std:right格式化文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想格式化一个字符串和一个带有右对齐的整数值。
在整数值前不加空格就可以做到这一点。

I just want to format a string and an integer value with right justify. There is no problem to do this without leading space before the integer value.

bytes.....................123981
total bytes..............1030131 

但这应该看起来像这样:

But it should look like this:

bytes ................... 123981
total bytes ............ 1030131

不幸的是下面的命令将不起作用,因为 setw (右对齐)仅与下一个流元素相关。

Unfortunately the example below wont work, because setw (right justify) relates only to the next stream element.

int iBytes = 123981;
int iTotalBytes = 1030131;
cout << setfill('.');
cout << right;
cout << "bytes " << setw(20) << " " << iBytes << endl;
cout << "total bytes " << setw(14) << " " << iTotalBytes << endl;

我几乎从来没有使用过std :: cout,所以有一种简单的方法可以不用事先加入

I hardly ever use std::cout, so is there a simple way to do this without previously joining a space char to the value?

推荐答案

最简单的方法是将和值写入std :: stringstream并将结果str()写入输出流,例如:

The simplest way would be to write your " " and value into a std::stringstream and write the resulting str() into your output stream like:

std::stringstream ss;
ss << " " << iBytes;
cout << "bytes " << setw(20) << ss.str() << endl;

这就是彻底的过度杀伤。可以打印的带有前缀的模板化类,并将两个构造函数参数 prefix,val 捆绑到一个要打印的字符串中。数字格式,并从最终输出流中获取精度。适用于int,float,string和const char *。

And here comes the complete overkill. A templated class prefixed which can be printed and bundles the two constructor arguments prefix,val into one string to be printed. number format, and precision is taken from the final output stream. Works with ints,floats, strings and const char *. And should work with every arg that has a valid output operator.

#include <fstream> 
#include <iostream> 
#include <iomanip> 
#include <sstream> 

using  namespace std; 

template<class T> 
class prefixed_base  { 
public: 
    prefixed_base(const std::string & prefix,const T val) : _p(prefix),_t(val) { 
    } 
protected: 
    std::string _p; 
    T           _t; 
}; 

// Specialization for const char *
template<> 
class prefixed_base<const char*>  { 
public: 
    prefixed_base(const std::string & prefix,const char * val) : _p(prefix),_t(val) { 
    } 
protected: 
    std::string _p; 
    std::string _t; 
}; 

template<class T> 
class prefixed : public  prefixed_base<T> { 
private: 
    typedef prefixed_base<T> super; 
public: 
    prefixed(const std::string & prefix,const T val) : super(prefix,val) { 
    } 

    // Output the prefixed value to an ostream
    // Write into a stringstream and copy most of the
    // formats from os.

    std::ostream & operator()(std::ostream & os) const { 
        std::stringstream ss; 

        // We 'inherit' all formats from the 
        // target stream except with. This Way we 
        // keep informations like hex,dec,fixed,precision 

        ss.copyfmt(os); 
        ss << std::setw(0); 
        ss << super::_p; 

        ss.copyfmt(os); 
        ss << std::setw(0); 
        ss << super::_t; 

        return os << ss.str(); 
    } 
}; 

// Output operator for class prefixed
template<class T> 
std::ostream & operator<<(std::ostream & os,const prefixed<T> & p) { 
    return p(os); 
} 

// This function can be used directly for output like os << with_prefix(" ",33.3)
template<class T> 
prefixed<T>    with_prefix(const std::string & p,const T  v) { 
    return prefixed<T>(p,v); 
} 

int main() { 
    int iBytes = 123981; 
    int iTotalBytes = 1030131; 
    cout << setfill('.'); 
    cout << right; 

    cout << "bytes " << setw(20) << with_prefix(" ",iBytes) << endl; 
    cout << "total bytes " << setw(14) << with_prefix(" ",iTotalBytes) << endl; 

    cout << "bla#1 "       << setw(20) <<  std::fixed << std::setprecision(9) << with_prefix(" ",220.55)      << endl; 
    cout << "blablabla#2 " << setw(14) <<  std::hex << with_prefix(" ",iTotalBytes) << endl; 
} 

这篇关于如何使用一个填充空间使用std:out setfill std :: setw std:right格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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