473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Re: problem with a reverse iterator

Victor Bazarov wrote:
An element of a string is of type 'char'. As you have found out
already, the value is controlled by the encoding of the symbol.
If you want to convert a char that supposedly contains a decimal
digit into its corresponding number, you need to (a) test to see
that it's actually a digit (see 'isdigit' function) and (b) if it
is a digit, subtract '0' from it:
if (isdigit(*rit))
cout << position << (*rit - '0') * 2 << endl;
else
cerr << position << " - not a digit!!!" << endl;
> position++;
}
}

V
Wow. That works. I do not fully understand why it works, but I wanted to
thank you for the advice. I'll research it more until I better understand.

Brad
Jun 27 '08 #1
1 1104
On 2008-05-05 18:21:52 -0400, brad <by*******@gmail.comsaid:
>
Wow. That works. I do not fully understand why it works, but I wanted
to thank you for the advice. I'll research it more until I better
understand.
'0' is a character. It happens to also be a number, but it's the number
that represents the character '0' in whatever encoding the compiler
uses (typically ASCII). But C and C++ both have a rule that the
representations of the characters '0' through '9' must be contiguous
and increasing, that is, '1' is one more than '0', etc. So to convert a
character that represents a digit into the value of that digit, just
subtract '0'.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
31
by: Raymond Hettinger | last post by:
Based on your extensive feedback, PEP 322 has been completely revised. The response was strongly positive, but almost everyone preferred having a function instead of multiple object methods. The...
2
by: Aki Niimura | last post by:
Hello everyone, I need to reverse an iterator in my program. There are many posting to related to this. But most of them are talking about how to expand the language to support such. In fact...
10
by: Derek | last post by:
#include <deque> #include <iostream> using namespace std; int main() { deque<int> foo; foo.push_back(42); deque<int>::reverse_iterator it = foo.rbegin(); while (it != foo.rend())
15
by: Fabio Cannizzo | last post by:
Is it possible to do something similar to the STL iterators, i.e. to execute a foreach loop in reverse order? Thanks, Fabio
15
by: Boltar | last post by:
Hi I'm going through an STL list container using a reverse iterator but it seems the erase() method only accepts ordinary iterators. Is there a similar method that will accept reverse iterators...
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
6
by: Michael Coley | last post by:
Hi, I've wrote this function which should add a comma for every 3 digits in a number (so that it looks something like 5,000). This is my function: std::string formatNumber(int number) { //...
6
by: angela.xuli | last post by:
I have following code: class ABC { ... .... int getValueA() }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.