thinking in c++ exercise

Started by
9 comments, last by Vopisk 17 years, 9 months ago
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

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 :-)
h20, member of WFG 0 A.D.
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;}
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]!
thanks!i understand it now, it start with 0!
Yeah, sorry.. that was my mistake :)
I edited my post while you were writing a reply I think :)
ok :)
lol
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?
A little off-topic, but isn't thinking in c++ available for free as an ebook?
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 :)

This topic is closed to new replies.

Advertisement