Print integers with thousands and millions of delimiters

There was a problem with printing integers with thousands / million separators.

I have a text file in which I received the country, city, general population.

I need to read in a file and sort by country. If the country is eual, I have to sort in descending order of population.

The text file is similar:

Australia ........ Sydney ......... 10.123.456

Brazil ........... Sao Paulo ....... 7.123.345

I read all 3 in a separate line. Then I delete everything. "" in the population line. Then I use atoi () to group the population chain.

Now I can sort by population if the country is equal. This view is working correctly.

So far so good. But I need the thousand / millionth separator to go to the press of the population.

If I use a string with "." For the public, sorting does not work correctly. Its sorting:

x ........ x ...... 1.123456

x ........ x ...... 10.123.456

x ........ x ...... 2.123.232

It should look like this:

Australia ........ Sydney ......... 10.123.456

Australia ........ Brisbane ....... 8.123.456

Is there a way to manipulate printing by adding a separator to int again?

Thank you very much in advance

+4
c ++ separator fstream
Jul 08 '13 at 15:26
source share
2 answers

imbue() output stream with locale that has the desired delimiter. For example:

 #include <iostream> #include <locale> int main() { // imbue the output stream with a locale. int i = 45749785; std::cout << i << "\n"; std::cout.imbue(std::locale("")); std::cout << i << "\n"; } 

Output to my car (and online demo ):

 45749785
 45.749.785

As commented and answered, James Kanze soaks up the input stream also to read the divided int values ​​without manually modifying the input.




See a detailed overview of locales. Appendix D: Locales.

+9
Jul 08 '13 at 15:33
source share

Use a locale that supports the required delimiters to read the file (this way you can read the values ​​as integers), and to write data.

Please note that you may not have such a language standard, or if you do, you may not know its name (and using the name to change other things that you do not want to change); on my machine, imbue ing with "" behaves differently according to the compiler (or maybe the shell with which I call it) - you should never use the language standard "" if you have a strict requirement format. (Using locale "" for the case when you want the format to depend on the user environment of the specification.)

In this case, it is probably better to specify locally explicitly:

 class MyNumPunct : public std::numpunct<char> { protected: virtual char do_thousands_sep() const { return ','; } virtual std::string do_grouping() const { return "\03"; } }; int main() { std::cout.imbue( std::locale( std::locale::classic(), new MyNumPunct ) ); std::cout << 123456789 << std::endl; return 0; } 

Of course, you will want to use this language for input as well. (This code will provide you with the "C" locale, and only the grouping has changed.)

+5
Jul 08 '13 at 15:33
source share



All Articles