rtoi implementation in STL

I am trying to study how "rtoi" is implemented in STL. The implementation is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
1.	template<typename Ch> unsigned int rtoi(const basic_string<Ch>& romanStr) {
2.	    int result = 0;                /* value to return */
3.	    AdditiveTerm currentTerm;
4.	 
5.	    unsigned int repeatedTimes;
6.	    
7.	    Ch lastSymbol = L'\0', currentSymbol, nextSymbol;
8.	 
9.	    /* main computing loop: inside reasonable length and without reaching the
10.	       end of the null terminated string */
11.	    for (typename basic_string<Ch>::const_iterator iter = romanStr.begin(), 
12.	            iterNext;
13.	            iter!=romanStr.end();
14.	            iter+= currentTerm.size, 
15.	            result+= currentTerm.value) {
16.	        currentSymbol = *iter;
17.	 
18.	        /* Rule 1: Repetition. Can't happen more than three times and not every
19.	           symbol is repeatable */
20.	        if (lastSymbol==currentSymbol) {
21.	            if ((++repeatedTimes==4)||(isNotRepeatable(currentSymbol))) {
22.	                throw ConversionError(IllegalRepetition);
23.	            }
24.	        } else {
25.	            repeatedTimes = 1;
26.	            lastSymbol = currentSymbol;
27.	        }
28.	 
29.	        /* the current symbol plus its follower (if not at the end) are 
30.	           evaluated in getNexAdditive() to see how much to cumulate*/
31.	        nextSymbol = ((iterNext = iter+1)==romanStr.end()) ? L'0' : *(iterNext);
32.	        currentTerm = getNextTerm(currentSymbol, nextSymbol);
33.	    }
34.	 
35.	    return result;
36.	}

But I do not know how to analyze the line 31 nextSymbol = ((iterNext = iter+1)==romanStr.end()) ? L'0' : *(iterNext); Thanks.
That's an arithmetic if. It works like this:

a = boolean_expression ? b : c;

If boolean_expression evalutes to true, a will be assigned the value of b. If it evalutes to false, it will be assigned the value of c instead.
closed account (E60S216C)
Wouldn't an if statement be the same thing?

if(boolean_expression == true)a = b;

if(boolean_expression == false)a = c;

Indeed. Actually, that could be put as:

1
2
if(boolean_expression) a = b;
else a = c;

Sometimes arithmetic ifs are just more convenient.
closed account (E60S216C)
So then why must there be arithmetic ifs and if statements if they do exactly the same thing?

It's annoying.
Because there's an old way and a new way. Technically in this case both are about the same age. The operator handles simple if else statements where if else can expand to handle more than once case.

the ?: is usually used during expressions and is nice and quick.

1
2
3
4
if ( isLeapYear )
februaryDays = 29;
else
februaryDays = 28;

simplifies to
februaryDays = (isLeapYear) ? 29 : 28;

Nice and smaller code. Usually it not used unless its for something simple like a expression.
Last edited on
Topic archived. No new replies allowed.