[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

std::reverse_iterator Confusion

MikeCopeland

6/17/2016 6:03:00 PM

I'm confused about string::reverse_iterator usage.
What I'm trying to do is scan the end of a string variable for all
numeric digits..and then convert those digits to an integer. For
example, I have;
std::string myData = "cldat89";
and I want to extract and convert the "89" to an integer variable.
Various explanations I've found via Google aren't making sense to me,
as they mostly assume I want to reverse the whole string data or such.
I just want to scan backwards (from last-to-first) until a character
_isn't_ a digit...and then be able to work with the substring of the
digit characters.
Although done this task "brute-force", the concept of
reverse_iterator seemed interesting, so I wanted to try it: so far, I
don't "get it". Please advise. TIA


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com...

2 Answers

legalize+jeeves

6/17/2016 6:24:00 PM

0

[Please do not mail me a copy of your followup]

MikeCopeland <mrc2323@cox.net> spake the secret code
<MPG.31cde929d72b6d169896c0@news.eternal-september.org> thusly:

> std::string myData = "cldat89";
>and I want to extract and convert the "89" to an integer variable.

A reverse iterator is not needed:

1 #include <iostream>
2 #include <string>
3
4 int main()
5 {
6 std::string s{"cldata89"};
7 auto pos = s.find_last_not_of("0123456789");
8 int val = std::stoi(s.substr(pos + 1));
9 std::cout << val << '\n';
10 return 0;
11 }

> g++ --std=c++11 /tmp/a.cpp
> ./a.out
89

However, for the purposes of education, a reverse iterator simply
traverses a container in the opposite order of a forward iterator.
I think cppreference.com does a good job of explaining it:
<http://en.cppreference.com/w/cpp/iterator/reverse_it...
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pi...
The Computer Graphics Museum <http://computergraphicsmuse...
The Terminals Wiki <http://terminals.classicc...
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpre...

Victor Bazarov

6/17/2016 6:30:00 PM

0

On 6/17/2016 2:02 PM, MikeCopeland wrote:
> I'm confused about string::reverse_iterator usage.
> What I'm trying to do is scan the end of a string variable for all
> numeric digits..and then convert those digits to an integer. For
> example, I have;
> std::string myData = "cldat89";
> and I want to extract and convert the "89" to an integer variable.
> Various explanations I've found via Google aren't making sense to me,
> as they mostly assume I want to reverse the whole string data or such.
> I just want to scan backwards (from last-to-first) until a character
> _isn't_ a digit...and then be able to work with the substring of the
> digit characters.
> Although done this task "brute-force", the concept of
> reverse_iterator seemed interesting, so I wanted to try it: so far, I
> don't "get it". Please advise. TIA

When you "scan backwards", remember that the characters come in the
reverse order. So, something like this is supposed to work:

int accumulator(0);
int multiplier(1), maxnumber(100000);
std::locale defloc;
for (auto it = myData.rbegin();
it != myData.rend() && multiplier < maxnumber;
++it, multiplier *= 10)
{
char c = *it;
if (std::isdigit(c, defloc))
accumulator += (c - '0') * multiplier;
else
break;
}

(I didn't actually test this, but it should give you an idea).

V
--
I do not respond to top-posted replies, please don't ask