Thread: Requesting data from a vector with a struct?

  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,820
    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
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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 12:44 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,820
    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
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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-18-2006, 11:23 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03: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, 06:49 PM