Requesting data from a vector with a struct?

This is a discussion on Requesting data from a vector with a struct? within the C++ Programming forums, part of the General Programming Boards category; Code: struct data { string name; string title; string author; }; vector<data> books; When you have stored data inside a ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    62

    Requesting data from a vector with a struct?

    Code:
        struct data 
        {
               string name;
               string title;
               string author;
        };
    
    
        vector<data> books;
    When you have stored data inside a vector like that, how would you request all the names, titles and authors inside the vector, using cout?

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    39
    use an iterator

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Or use a loop.
    Code:
    for(int i = 0; i < books.size(); i++){
        std::cout<<books[i].name;
        //ETC
    }//for
    Woop?

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by prog-bman View Post
    Or use a loop.
    Code:
    for(int i = 0; i < books.size(); i++){
        std::cout<<books[i].name;
        //ETC
    }//for
    Thanks!

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by BigFish21 View Post
    use an iterator
    Is it better than a loop? How do you use it in the example?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    I wouldn't say it's better - in fact, I'd say it's disadvantageous when you can use indexes.
    You would use an iterator like this:
    Code:
    for(std::vector<data>::iterator i = books.begin(); i != books.end();)
    {
        std::cout << *i++;
    }
    Iterators become invalidated (invalid) if you perform certain operations on the container, such as resizing buffer or capacity.
    Indexes never become invalid.
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Is it better than a loop?
    A loop is a control structure. An iterator is an object. In this case, the comparison is between using an index with a loop (prog-bman's example) or an iterator with a loop. It is also possible to use iterators with generic algorithms, but ignore that for now if you have not yet reached that stage in your learning.

    How do you use it in the example?
    Code:
    for (vector<data>::const_iterator i = books.begin(), end = books.end();
        i != end; ++i)
    {
        std::cout << i->name; // etc
    }
    Note that prog-bman's example is slightly inaccurate since the index is not of type int, but of type vector<data>::size_type, which is typically an unsigned int.

    Quote Originally Posted by Elysia
    Indexes never become invalid.
    That is not true: indices can become invalid when the container shrinks.
    Last edited by laserlight; 05-26-2008 at 01:44 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Fine, fine
    Code:
    for(std::vector<data>::size_type i = 0; i < books.size(); i++){
    }//for
    Woop?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    Quote Originally Posted by laserlight View Post
    That is not true: indices can become invalid when the container shrinks.
    Ah true, true. I never thought of that one.
    Nevertheless, an iterator will also become invalid if the container shrinks beyond its position.
    Iterators are a good idea, but the fact that they become invalid makes their usefulness drop. Thankfully, I have solved that issue in my own iterators.
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Iterators are a good idea, but the fact that they become invalid makes their usefulness drop.
    I would say that it just makes them slightly more complicated to use.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    I suppose you could see it that way, too.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 12:25 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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