i came across a line in a source code:
My question is:Code:set<Item> header; set<Item>::iterator itI; itI = header.insert(a).first;
I would like to know what .first means ? i tried to look in cppreference.com but i cant find anything about it.
i came across a line in a source code:
My question is:Code:set<Item> header; set<Item>::iterator itI; itI = header.insert(a).first;
I would like to know what .first means ? i tried to look in cppreference.com but i cant find anything about it.
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
Dont get what you mean by this line ... What is the connection between first or second with boolean ?The boolean could have been accessed by using second just like you example used first.
second represent the boolean part of the pair returned by this form of the insert call. For example you could maybe do this...
Another way is to just save the pair to a variable...Code:set<Item> header; if( header.insert(a).second ) cout << "Insert was successfull." << endl; else cout << "Insert failed, item already in set." << endl;
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
so you are saying that first is the iterator part and second is the boolean part ?
where can i get information on stuffs like this ? it is not in cppreference, i never see it in books before
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
is there any online references?
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