std::numpun...do_grouping(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

okay so welcome to std::numpun...do_grouping(3), the C++ Standard Libary page, bestie. The NAME line is literally std::numpunct::grouping,std::numpunct::do_grouping - std::numpunct::grouping,std::numpunct::do_grouping β€” yes, both functions, same page, iconic.


πŸ¦‹ Synopsis πŸ¦‹

Defined in header <locale>:

public:                                  (1)
std::string grouping() const;
protected:                               (2)
virtual std::string do_grouping() const;
  1. This public member function calls the member function do_grouping of the most derived class.
  2. This one returns an std::string holding, in each char element, the number of digits in each group of the numeric output formatted by num_put::put() (and, therefore, basic_ostream::operator<<).

So this function returns a string, vec, which is used as a vector of integer values. For example, "\003" specifies groups of 3 digits each, while "3" implies groups of 51 digits each. Each element vec[i] represents the number of digits in the ith digit group of the integer part of the number, counting from the right: vec[0] holds the number of digits in the rightmost group, vec[1] β€” in the second group from the right, etc. The grouping indicated by the last character, vec[vec.size()-1], is repeatedly reused to group all remaining digits in the (left part of the) number. If vec[i] is non-positive or equals CHAR_MAX the size of the corresponding digit group is unlimited.


✨ Return value ✨

The object of type std::string holding the groups. The standard specializations of std::numpunct return an empty string, indicating no grouping. Typical groupings (e.g. the en_US locale) return "\003".


🌸 Example 🌸

The page says // Run this code, so here's the whole example, no cap:

#include <iostream>
#include <limits>
#include <locale>

struct space_out : std::numpunct<char>
{
    char do_thousands_sep()   const { return ' ';  } // separate with spaces
    std::string do_grouping() const { return "\1"; } // groups of 1 digit
};

struct g123 : std::numpunct<char>
{
    std::string do_grouping() const { return "\1\2\3"; }
};

int main()
{
    std::cout << "Default locale: " << 12345678 << '\n';
    std::cout.imbue(std::locale(std::cout.getloc(), new space_out));
    std::cout << "Locale with modified numpunct: " << 12345678 << '\n';
    std::cout.imbue(std::locale(std::cout.getloc(), new g123));
    std::cout << "Locale with \\1\\2\\3 grouping: "
              << std::numeric_limits<unsigned long long>::max() << '\n'
              << "Same, for a floating-point number: "
              << std::fixed << 123456789.123456789 << '\n';
}

πŸŽ€ Output πŸŽ€

Default locale: 12345678
Locale with modified numpunct: 1 2 3 4 5 6 7 8
Locale with \1\2\3 grouping: 18,446,744,073,709,551,61,5
Same, for a floating-point number: 123,456,78,9.123457

πŸ‘‘ See also πŸ‘‘

  • do_thousands_sep provides the character to use as thousands separator β€” it's marked [virtual] and is a virtual protected member function.

mwah πŸ’‹ bestie, that's the whole vibe β€” source/version: http://cppreference.com, date: 2024.06.10.

πŸ“¦ where this page comes from