1

I need some help in understanding what is happening to the vector rvN

This is my understanding:

rvN is a vector containing the number characters 0001. In the function Clear(), all of the leading zeroes are to be erased from the vector. The iterator is set to pointing to past the end of the last vector element (1).

it = 3 *it != 0 (since *it == 1) rvN.erase(it+1, rvN.end()) ->>> rvN.erase(4,rvN.end()) ? 

How are all of the zeroes eliminated?

for (int i = Number.size() - 1; i >= 0; i--) { if (Number[i]<'0' || Number[i]>'9') break; else rvN.push_back(Number[i] - '0'); } Clear(rvN); } void LargeNumber::Clear(vector<char>& rvN) { //Eliminate all leading 0s vector<char>::iterator it = rvN.end(); while (it != rvN.begin()) { it--; if (*it != 0) break; } rvN.erase(it + 1, rvN.end()); } 
2
  • The vector appears to be reversed, so that the least significant value is stored at the "begin" end. E.g. 1234 is stored as { 4, 3, 2, 1 }. Presumably, 'reverse vector Numbers' Commented Nov 29, 2013 at 22:38
  • Have you see this code work? What is the block before the code with it=3 in it describing? Commented Nov 29, 2013 at 22:40

1 Answer 1

2

The digits are read from right to left:

for (int i = Number.size() - 1; i >= 0; i--) 

If "Number" contains "1234" the first digit read will be the least-significant, i.e. "4". These are then pushed onto "rvN" in the order they are read. As a result, rvN would become

{ 4, 3, 2, 1 } 

or if Number contained 00005

{ 5, 0, 0, 0, 0 } 

So Clear is simply working from right-to-left until it finds a non zero and then trimming the back of the array to leave only the right-most digits in the normal representation (050 -> 50, 005 -> 5, 500 -> 500).

Sign up to request clarification or add additional context in comments.

3 Comments

Number is 1000. So if reversed, it would be 0001. If it is working from right to left, how are the zeroes before 1 removed? Since it reaches 1 first and breaks out of the while loop. I'm a little confused.
@Justin They're not. Thats the point. If the number is "1000" the zeros are significant. If the number were "0001" the reversal would be 1000 then the loop would iterate four times, keeping the last test and thus returning 1. The only way you get 0001 as rvN to begin with is if the original data was "1000".
Edited the last line of my answer slightly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.