gpt4 book ai didi

c++ - 如何使用带有一个填充空间的 std:out setfill std::setw std:right 格式化文本

转载 作者:太空狗 更新时间:2023-10-29 23:02:57 27 4
gpt4 key购买 nike

我只想格式化一个字符串和一个右对齐的整数值。如果整数值前没有前导空格,这样做是没有问题的。

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

但它应该是这样的:

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

不幸的是,下面的示例无法运行,因为setw(右对齐)仅与下一个流元素相关。

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,那么有没有一种简单的方法可以做到这一点而无需事先将空格字符连接到值?

最佳答案

最简单的方法是将您的“”和值写入 std::stringstream 并将生成的 str() 写入您的输出流,如:

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

这里完全矫枉过正了。一个模板类前缀,可以打印并将两个构造函数参数 prefix,val 捆绑到一个要打印的字符串中。数字格式,精度取自最终输出流。适用于整数、 float 、字符串和 const char *。并且应该与每个具有有效输出运算符的参数一起使用。

#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;
}

关于c++ - 如何使用带有一个填充空间的 std:out setfill std::setw std:right 格式化文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26959517/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com