Re: Problem using std::prev on ustring iterator



On 18 March 2016 at 18:11, Jonathan Wakely <gtkmm kayari org> wrote:
On 18 March 2016 at 17:57, jeremy.harmon wrote:
Hello,

When I try to use std::prev on a ustring const_iterator nothing
happens, operator-- works fine.

Glib::ustring str = "test";
Glib::ustring::iterator iter = str.begin();

std::advance(iter);
printf("%c\n", *iter);

std::prev(iter);
printf("%c\n", *iter);

iter--;
printf("%c\n", *iter);

Output is
e
e
t

Any ideas why it doesn't work?

Because unlike std::advance, std::prev doesn't modify its argument, it
returns the new iterator instead.

http://en.cppreference.com/w/cpp/iterator/prev

So, for completeness, you probably want to do this instead:

iter = std::prev(iter);

or std::advance(iter, -1)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]