Jump to content
  • Advertisement
Sign in to follow this  
coding penguin

thinking in c++ exercise

This topic is 4342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i buy a book Thinking in C++. relly fantastic book! in one exercise i must print lines reversed. i do it but i think it is not good. look at for loop.why i must decrement one?if i not it crashes when i run it (not compile) vector.size is not explained in book,so i dont know how exactly i works. here is it.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> riadky;
    string riadok;

    ifstream in("main.cpp");
    while(getline(in, riadok))
        riadky.push_back(riadok);

    for(int i=riadky.size()-1; i>-1; i--)
	cout << i+1 << ". " << riadky << endl;

	return 0;
}

//5.7.2006 (16:41)
//ThinkingInC++ exercise 2 / 5 - Vypis riadkov od zadu

Share this post


Link to post
Share on other sites
Advertisement
vector.size() returns the number of elements stored in the vector. It initially contains no elements, but each time you call push_back(), it stores a new element in the vector. In this case, it adds each new line of input from the user to the back of the vector:

> a
VECTOR: 1. a

> b
VECTOR: 1. a 2. b

> c
VECTOR: 1. a 2. b 3. c

In this case, vector.size() will return 3, so our for loop starts at 2 and decreases to zero by one each time. This lets us print the elements of the vector from the back to the front. The purpose of the exercise might be to show you that a vector object can be used as a stack (Last-In-First-Out). So,

vector[0] = a, vector[1] = b, vector[2] = c.

Your for loop would also not crash if you wrote it as so (but now, elements would be printed in queue order -- First-In-First-Out):

for (int i=0; i<riadky.size(); i++)

If you need any more clarification, just say so :-)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You should take a look at reverse_iterator.


#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
vector<string> riadky;
vector<string>::reverse_iterator rit;
string riadok;

ifstream in("test.cpp");
while(getline(in, riadok))

riadky.push_back(riadok);


rit = riadky.rbegin();

while (rit<riadky.rend())
{
cout<< *rit << endl;
++rit;
}

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by lulul
vector.size is not explained in book,so i dont know how exactly i works.


Google is your friend :)
http://www.google.com/search?hl=en&lr=&safe=off&q=vector.size+site%3Amicrosoft.com&btnG=Search
http://msdn2.microsoft.com/en-us/library/3y41k4hb.aspx

"Returns the number of elements in the vector."

The first element in your vector riadky is in position 0, riadky[0], so the last element can be found at riadky[ number_of_elements - 1 ].
If you have stored 3 strings in your vector, they will be in positions 0, 1 and 2.
riadky.size() will return the number of elements in riadky, which is 3. The index/position of the last element is 2! That's why you have to start with i = riadky.size() - 1;


// a vector with 3 strings in it.
vector<string> myVector;
myVector.push_back( "hello" ); // myVector[0]
myVector.push_back( "moo" ); // myVector[1]
myVector.push_back( "bye!" ); // myVector[2]

int size = myVector.size(); // size is 3, but the position of the last element is myVector[2]!


Share this post


Link to post
Share on other sites
Yeah, sorry.. that was my mistake :)
I edited my post while you were writing a reply I think :)

Share this post


Link to post
Share on other sites
uh, all exercises in 2 are done!
i read that i can buy completed exercises.why they i not free?i buy a book so why i must pay for exercises?(too if i dont want him :)
it is bad if i complete all exercises and then put in on the internet for free?

Share this post


Link to post
Share on other sites
Hi LuLu, i too am using the book (in the free PDF format), and
you shouldn't be too worried about vectors now, as he mentions that
they will be covered in depth later on.

The focus was to learn to use the STL and library' s in general.
Happy learning :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!