C++ 一串数字三位一节,用逗号隔开表示

 1 #include <iostream>
 2 #include <string>
 3 #include <sstream>
 4 using namespace std;
 5 string LongToString(long l)
 6 {
 7     stringstream str;
 8     str<<l;
 9     return str.str();
10 }
11 int main()
12 {
13     long l;
14     cin>>l;
15     string str = LongToString(l);
16     size_t len = str.length();
17     for(int index =(int) len-3; index > 0; index -= 3)
18       str.insert(index, ",");
19     cout<<str<<endl;
20     return 0;
21 }
原文地址:https://www.cnblogs.com/chechen/p/4225787.html