# Thread: Are vectors what I need?

1. ## Are vectors what I need?

I would like to factor a number and store the factors into an array. Obviously, I don't know how many factors ahead of time any given number would have, so I need something that would be able to expand to hold those factors.

I have been doing some reading, and it looks like vectors are what I need. Unforunately, the C++ book I am learning from doesn't cover vectors.

Any idea on where to start working with vectors?

2. Yes, vectors would be a fine choice.

Get a better book.

3. Originally Posted by Biggen
Unforunately, the C++ book I am learning from doesn't cover vectors.
Throw it away and get a better one. We have a book recommendations thread.

4. Yep, Accelerated C++ or PPP by Stroustrup are generally perceived to be the strongest beginner books.

5. Thanks guys. I guess I'll need to add another book to my collection after I finish the one I am using!

I did some research and figured out how to use (slightly) vectors and how to add elements via the push.back feature. But I do have a question...

I want to be able to go to the absolute end of the vector, begin using the data in that last element, and work my ways backwards back to the beginning of the vector. So I setup a for loop to do this but I seem to be having issues:

Code:
```for (int i = vector_name.size(); i >= 0; i--){
Do stuff...
}```
My confusion is exactly what size() is doing. Say the vector has 4 elements (0 - 3) Is it actually giving me access to the data in the last element (data in element 3) or is size() simply assigning "i" the number of total elements which is 4. Two completely different things...

Thanks!

6. Size tells you how many elements there are.

I guess I'll need to add another book to my collection after I finish the one I am using!
Didn't you listen? Throw it away! A C++ beginner's book that doesn't teach vectors cannot be trusted.

Your for loop is broken. The values of i will be, for a vector of size 4, be 4,3,2,1,0, which is five iterations. That just can't be right.

7. Valid indexes in a vector is always [ 0, vector.size() ) (note the mathematical notation).

8. Hmm... Ok, here is what I did after some research:

Code:
```for (int i = euler_vector.size() - 1; i >= 0; i--){
Do stuff...```
This seems to work great! I don't know if this was the "best" way to do it, but it seems to be working for my purpose...

9. Check out this basic tutorial. With Vectors, you should use iterators.

http://en.wikipedia.org/wiki/Vector_(C%2B%2B)

10. Originally Posted by Dino
Check out this basic tutorial. With Vectors, you should use iterators.

http://en.wikipedia.org/wiki/Vector_(C%2B%2B)
Awesome, Dino! I'll give it a read!

I'm only 2 weeks into learning C++ so right now I am finding a lot of stuff overwhelming. I have partially learned other languages over the last 15 years (Perl, Basic, Cobal, Java) so I think knowing some of the "theory" is helping me some. Just have to keep plugging away...

11. If you are serious about learning though Biggen, I would seriously recommend a different book.
Like the previous comments, a C++ book that doesn't have vectors, sets off warning bells.

Out of interest, which book is it that you are learning from?

12. Originally Posted by darren78
If you are serious about learning though Biggen, I would seriously recommend a different book.
Like the previous comments, a C++ book that doesn't have vectors, sets off warning bells.

Out of interest, which book is it that you are learning from?
Here is the book, Darren:
Amazon.com: C++ Without Fear: A Beginner&#39;s Guide That Makes You Feel Smart…

I just started on Chp7 which is String Arrays. I got this book for two reasons. One it was cheap! Second, it seemed to have some good user reviews overall (although those should be taken with a grain of salt).

I appreciate your help. I'll check out the book thread. Although I don't want a reference book. I want a book that actually teaches... O'reillys are a good example of references which are horrible books to learn from.

13. I suggest you check the reviews from here. Cross-reference the books you find in the books thread.
The problem with user reviews is that they don't always know what's good and what is not. It may appear a good book to them while in reality it isn't. That's why it's better to have professionals review books. And that is what that site does.
Unfortunately, I couldn't find your book there. However, I do know that Accelerated C++ is a good book (indeed, it has a positive review on the site mentioned).

14. Originally Posted by Biggen
Here is the book, Darren:
Amazon.com: C++ Without Fear: A Beginner's Guide That Makes You Feel Smart…

I just started on Chp7 which is String Arrays. I got this book for two reasons. One it was cheap! Second, it seemed to have some good user reviews overall (although those should be taken with a grain of salt).

I appreciate your help. I'll check out the book thread. Although I don't want a reference book. I want a book that actually teaches... O'reillys are a good example of references which are horrible books to learn from.
Generally the best books are: Accelerated C++

Or, PPP..Stroustrup

They are both beginner books and teach C++ the right way. My preference, having used both, would be PPP.

15. Well I guess I need to place another Amazon order! I wish I would have found you guys before I jumped in feet first a few weeks ago.

I like how the Accelerated book is under 400 pages. Some of the C++ books are over 1000 which is information overload for a beginner like me...

Thanks guys...