我希望有人可以提供一些有關特定問題的見解。我正在編寫一個程序,它採用整數,將它們存儲在一個向量中,並用逗號分隔符將它們打印出來,數字大於999 - > 1,000。重載<<並定義一個流操作器C++
我的問題是..好吧,其實兩個,我怎樣才能傳遞一個矢量到一個函數,第二,如果我想超載的所有這些在幕後將是可能的嗎?
從類逗號全局函數:
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'作爲數字分組 – 2013-05-05 23:28:21