Yes this can be done automatically in C++ by setting the correct facet on the locale.
#include <iostream>
#include <locale>
#include <string>
template<typename CharT>
struct Sep : public std::numpunct<CharT>
{
virtual std::string do_grouping() const {return "\003";}
};
int main()
{
std::cout.imbue(std::locale(std::cout.getloc(), new Sep <char>()));
std::cout << 123456789 << "\n";
}
Note: The C-locale (The locale used when your application does not specifically set one) does not use a thousands separator. If you set the locale of your application to a specific language then it will pick up the languages specific method of grouping (without having to do anything fancy like the above). If you want to set the locale to what your machines current language settings (as defined by the OS) rather than a specific locale then use "" (empty string) as the locale.
So to set the locale based on your OS specific settings:
int main()
{
std::cout.imbue(std::locale(""));
std::cout << 123456789 << "\n";
}