Reply to topic  [ 5 posts ] 
C++ for loop 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Just going through my C++ book and came across an exercise about iterators and vectors and stuff.

It asked me to write a program (using iterators in a for loop) that would take ints into a vector and then reverse the vector before printing it.

i.e. if I enter 1 2 3 4 5 into the console window it would print out as 5 4 3 2 1 (with new lines instead of spaces).

The for loop I wrote went like this...

data is my vector storing the inputs.

Code:
for(vector<int>::iterator up(data.begin()), down(data.end() - 1) ; up < down ; ++up, --down)
{
    //swap entries
}

i.e. up starts at the front and goes up. down starts at the last entry (NB not the off the end entry) and goes down. if up >= down then it ends.

This worked perfectly and fit everything into the for loop control. It seemed to work a treat.

However, the book came up with this...

Code:
for (vector<int>::iterator start(data.begin()), end(data.end()) ; start != end ; /*nothing here*/)
{
    --end;
    if (start != end)
    {
        //swap entries
        ++start;
    }
}

It seems like their way is a lot more long winded and harder to read (for me anyway).

Which would you prefer?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Jan 20, 2011 12:39 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
Neither of them look particularly fun...Could you not use some sort of a foreach loop?


Thu Jan 20, 2011 3:39 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
forquare1 wrote:
Neither of them look particularly fun...Could you not use some sort of a foreach loop?

I've not got to that part yet :D

The loop I wrote isn't that messy is it?

It just has 2 variables that control the loop as opposed to 1.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Jan 20, 2011 4:27 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
forquare1 wrote:
Neither of them look particularly fun...Could you not use some sort of a foreach loop?

You could also use the data.reverse() function but it as it was about learning the format of loop s and iterators they said not to :D

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Jan 20, 2011 5:01 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
The book has just done a while loop with the start values initialised in the start, for loops put the iterations at the start

IMO they have overcomplicated it

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Thu Jan 20, 2011 6:23 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.