【发布时间】:2013-05-05 23:15:16
【问题描述】:
我希望有人可以就我遇到的特定问题提供一些见解。我正在编写一个程序,它接收整数,将它们存储在一个向量中,并用逗号分隔符将它们打印出来,用于大于 999 -> 1,000 的数字。
我的问题是.. 嗯,实际上有两个,我如何将向量传递给函数,第二,如果我想重载
来自逗号类的全局函数:
template <class T>
string formatWithComma(T value){
stringstream ss;
locale commaLoc(locale(), new CommaNumPunc() );
ss.imbue(commaLoc);
ss << value;
return ss.str();
在main()中循环显示向量:
for (vector<unsigned int>::iterator i = integers.begin(); i != integers.end(); ++i){
cout << formatWithComma(*i) << " ";
}
【问题讨论】:
-
你不需要写一个自定义的
locale来用逗号分隔数字,num_punct应该用,作为分隔符,3作为数字分组
标签: c++ vector operator-overloading manipulators