[ACCEPTED]-How do you insert with a reverse_iterator-stl

Accepted answer
Score: 31

l.insert(reverse.base(), 10); will insert '10' at the end, given your 1 definition of the 'reverse' iterator. Actually, l.rbegin().base() == l.end().

Score: 16

Essentially, you don't. See 19.2.5 in TCPPPL.

The 7 reverse_iterator has a member called base() which will return 6 a "regular" iterator. So the following 5 code would work in your example:

l.insert(reverse.base(), 10);

Be careful 4 though because the base() method returns the element 3 one after the orginal reverse_iterator had pointed to. (This 2 is so that reverse_iterators pointing at 1 rbegin() and rend() work correctly.)

Score: 0

Just in case it is helpful, as this it the 5 first hit on a search, here is a working 4 example of using rbegin. This should be faster 3 than using std::stringstream or sprint. I defiantly call a member 2 fmt_currency many thousands of times in some print jobs. The 1 std::isalnum is for handling a minus sign.

std::wstring fmt_long(long val) {//for now, no options? Just insert commas
    std::wstring str(std::to_wstring(val));
    std::size_t pos{ 0 };
    for (auto r = rbegin(str) + 1; r != str.rend() && std::isalnum(*r); ++r) {
        if (!(++pos % 3)) {
            r = std::make_reverse_iterator(str.insert(r.base(), L','));
        }
    }
    return str;
}

More Related questions