Comma Separate Number Without stringstream

时光总嘲笑我的痴心妄想 提交于 2019-12-11 07:33:15

问题


So typically if I wanted to insert locale appropriate separators in some number, foo, I'd do something like this:

ostringstream out;

out.imbue(locale("en-US"));
out << foo;

Then I could just use out.str() as the separated string: http://coliru.stacked-crooked.com/a/054e927de25b5ad0

Unfortunately I've been asked not to use stringstreams in my current project. Is there any other way I can accomplish this? Ideally a locale dependent way?


回答1:


So this answer is the C++ distillation of Jerry Coffin's answer to this question: Cross Platform Support for sprintf's Format '-Flag

template <typename T>
enable_if_t<is_integral_v<remove_reference_t<T>>, string> poscommafmt(const T N, const numpunct<char>& fmt_info) {
    const auto group = fmt_info.grouping();
    auto posn = cbegin(group);
    auto divisor = static_cast<T>(pow(10.0F, static_cast<int>(*posn)));
    auto quotient = div(N, divisor);
    auto result = to_string(quotient.rem);

    while(quotient.quot > 0) {
        if(next(posn) != cend(group)) {
            divisor = static_cast<T>(pow(10.0F, static_cast<int>(*++posn)));
        }
        quotient = div(quotient.quot, divisor);
        result = to_string(quotient.rem) + fmt_info.thousands_sep() + result;
    }
    return result;
}

template <typename T>
enable_if_t<is_integral_v<remove_reference_t<T>>, string> commafmt(const T N, const numpunct<char>& fmt_info) {
    return N < 0 ? '-' + poscommafmt(-N, fmt_info) : poscommafmt(N, fmt_info);
}

Naturally this suffers from the identical 2's compliment negation issue.

This certainly benefits from C++'s string memory management, but also from the ability to pass in a specific numpunct<char> which need not be the current locale. For example whether or not cout.getloc() == locale("en-US") you can call: commafmt(foo, use_facet<numpunct<char>>(locale("en-US")))

Live Example




回答2:


Set up a pipe. Use the linked to code to construct an ofstream and ifstream from a file descriptor and then output to one and read from the other.

This is a bizarre and contorted solution. But then the idea that you have to use locales, have to store things, and can't use stringstream is just as bizarre. So, they give you bizarre requirements, they get bizarre code.



来源:https://stackoverflow.com/questions/44502040/comma-separate-number-without-stringstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!