Are vectors what I need?

This is a discussion on Are vectors what I need? within the C++ Programming forums, part of the General Programming Boards category; I would like to factor a number and store the factors into an array. Obviously, I don't know how many ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    8

    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. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Yes, vectors would be a fine choice.

    Get a better book.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Biggen View Post
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Yep, Accelerated C++ or PPP by Stroustrup are generally perceived to be the strongest beginner books.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    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!
    Last edited by Biggen; 07-29-2010 at 12:58 PM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Valid indexes in a vector is always [ 0, vector.size() ) (note the mathematical notation).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    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. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Check out this basic tutorial. With Vectors, you should use iterators.

    http://en.wikipedia.org/wiki/Vector_(C%2B%2B)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    Quote Originally Posted by Dino View Post
    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. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    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. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    Quote Originally Posted by darren78 View Post
    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'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. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Biggen View Post
    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. #15
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    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...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21