Thread: Inserting struct data into a vector?

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

    Inserting struct data into a vector?

    Code:
    struct data {
      string name;
      string title;
      string author;
    };
    
    vector <data> books;
    Say that you have an x ammount of names, decided by the user, that needs to be put inside the vector. How do you do that in the example provided using a for loop (where i < x)?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You may take a look at this to see what you can do with a vector, in particular push_back.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by anon View Post
    You may take a look at this to see what you can do with a vector, in particular push_back.
    So how would that be in the example provided?

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    Code:
    data tmp;
    
    tmp.name = "name";
    tmp.title= "title";
    tmp.author= "author";
    
    books.push_back( tmp );

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by EOP View Post
    Code:
    data tmp;
    
    tmp.name = "name";
    tmp.title= "title";
    tmp.author= "author";
    
    books.push_back( tmp );
    Thanks!
    How do you later access a certain name, title, or an author if you want to cout it?

    If you like an ID to be associated with "tmp", what would be a clever way to do so?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Glauber View Post
    Thanks!
    How do you later access a certain name, title, or an author if you want to cout it?

    If you like an ID to be associated with "tmp", what would be a clever way to do so?
    Download the book at this link: http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

    You're asking basic questions without making even a modest effort to learn on your own.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    @medievalelks: I'm in a gentle mood

    Code:
    for( vector<data>::iterator vi = books.begin(); vi != books.end(); ++vi )
    	cout << "Name: " << vi->name << " Title: " << vi->title << " Author: " vi->author << endl;
    A "clever" way to add an ID is your homework for today, Glauber.
    Last edited by EOP; 05-25-2008 at 04:30 PM.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Glauber View Post
    Thanks!
    How do you later access a certain name, title, or an author if you want to cout it?
    Vectors can work the same as an array.
    cout << tmp[0].name << tmp[0].title << tmp[0].author;

    Quote Originally Posted by Glauber View Post
    If you like an ID to be associated with "tmp", what would be a clever way to do so?
    Then I believe what you want is a map<ID, data> instead of a vector<data>

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by cpjust View Post
    Vectors can work the same as an array.
    cout << tmp[0].name << tmp[0].title << tmp[0].author;


    Then I believe what you want is a map<ID, data> instead of a vector<data>
    Yes probobly, so how would you create it as you suggest? Would the ID be a var bound to the data? How would it work?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    62
    Could you in words describe what that does, each symbol and all?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Certainly.
    First define a map. It is a template that takes two template types: the key and the value.
    The key is the type that you will use with the find function to find the data you specified with value.
    So in essence, you will search using an integer (id) to find a struct named "player".
    Next line is an example of how to insert something into the map.
    You use the index operator and inside, specify your id that you want to associate your value with. Then I use assignment to assign the value I want to be associated with that key.
    The third line uses the find function to find that specific value associated with that id. std::map::find returns an iterator. The iterator is always of the type of the type of your map::iterator. So that means std::map<int, player>::iterator is the type.
    After assigning it, must must check if it's NOT equal to std::map::end() because the function will return std::map::end() (which is an iterator) if it fails.
    Considering it succeeds, accessing the property i->second finds the value associated with that id.
    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.

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by Elysia View Post
    Certainly.
    First define a map. It is a template that takes two template types: the key and the value.
    The key is the type that you will use with the find function to find the data you specified with value.
    So in essence, you will search using an integer (id) to find a struct named "player".
    Next line is an example of how to insert something into the map.
    You use the index operator and inside, specify your id that you want to associate your value with. Then I use assignment to assign the value I want to be associated with that key.
    The third line uses the find function to find that specific value associated with that id. std::map::find returns an iterator. The iterator is always of the type of the type of your map::iterator. So that means std::map<int, player>::iterator is the type.
    After assigning it, must must check if it's NOT equal to std::map::end() because the function will return std::map::end() (which is an iterator) if it fails.
    Considering it succeeds, accessing the property i->second finds the value associated with that id.
    Explenation appreciated. If you got the will, could you explain what good typing "std::" before something does, and when you use it?
    And what exactly does "->" do?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All of the STL and standard library stuff are located in the std namespace. Therefore, you must specify the namespace first. You do that with "namespace::member". The namespace being std, and we want to access map, that becomes "std::map".
    And "->" is another syntax for "(*obj).something", so i->second is the same as (*i).second.
    Iterators also have an overloaded -> operator. Iterators are meant to behave the same way as pointers.
    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.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    could you explain what good typing "std::" before something does, and when you use it?
    It qualifies the name as being in the std namespace. Have you learnt about namespaces?

    And what exactly does "->" do?
    a->b is equivalent to (*a).b.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM