Thread: ATD: C++ Sets

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    55

    ATD: C++ Sets

    i came across a line in a source code:

    Code:
    set<Item> header;
    set<Item>::iterator itI;
    
    itI = header.insert(a).first;
    My question is:
    I would like to know what .first means ? i tried to look in cppreference.com but i cant find anything about it.


  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    One of the forms of the insert function returns a pair consisting of an iterator, and a boolean. The iterator is the first part of the pair and this is what itI gets set to and is the location of where whatever we are inserting into the set gets placed. The boolean by the way determines whether we were successfull or not in the insert operation or not, since it might have failed if we were inserting a duplicate entry into the set. The boolean could have been accessed by using second just like you example used first.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    The boolean could have been accessed by using second just like you example used first.
    Dont get what you mean by this line ... What is the connection between first or second with boolean ?


  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    second represent the boolean part of the pair returned by this form of the insert call. For example you could maybe do this...

    Code:
    set<Item> header;
    if( header.insert(a).second )
        cout << "Insert was successfull." << endl;
    else
        cout << "Insert failed, item already in set." << endl;
    Another way is to just save the pair to a variable...

    Code:
    set<Item> header;
    pair<set<Item>::iterator,boolean> ItemSetPair;  // I think this line is right... its been awhile
    
    ItemSetPair = header.insert(a);
    
    if( ItemSetPair.second )
    {
        cout << "Insert was successfull." << endl;
        // Do something with ItemSetPair.first perhaps?
        // At this stage, ItemSetPair.first is the same as itI after the insert call in your original post
    }
    else
        cout << "Insert failed, item already in set." << endl;
    Last edited by hk_mp5kpdw; 11-08-2004 at 03:21 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    so you are saying that first is the iterator part and second is the boolean part ?

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    yes.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    where can i get information on stuffs like this ? it is not in cppreference, i never see it in books before

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It's in this book that I use a reference -> Nicolai Josuttis "The C++ Standard Library: A Tutorial And Reference". Chapter 4 "Utilities", section 4.1 "Pairs", pages 33-37. Not alot there but at least a brief overview of pairs.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    is there any online references?

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Try to google for "pair" "STL" "C++" and see what comes up.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 08:09 PM
  2. Major Problems with GetSaveFileName() and GetOpenFileName()
    By CodeHacker in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2004, 11:05 AM
  3. creating new sets
    By axon in forum C++ Programming
    Replies: 7
    Last Post: 12-03-2003, 06:37 PM
  4. Problem with My Sets ADT
    By jawwadalam in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 06:36 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM