Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > question on map...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 45747 of 45898
Post > Topic >>

question on map<K, V>::insert(iterator, const value& v)

by "subramanian100in@[EMAIL PROTECTED] India" <subramanian100in@[EMAIL PROTECTED] > May 4, 2008 at 12:16 AM

consider the following program:

#include <cstdlib>
#include <iostream>
#include <map>
#include <utility>
#include <string>

using namespace std;

int main()
{
        typedef map<int, string> container_type;

        container_type m;

        m[0] = "zero";
        m[1] = "one";
        m[2] = "two";

        container_type::iterator it = m.find(0);

        ++it;
        ++it;

        container_type::iterator res = m.insert(m.end(), make_pair(0,
"Test"));

        if (res == m.begin())
                cout << "res equals m.begin()" << endl;
        else
                cout << "res !=  m.begin()" << endl;

        cout << res->second << endl;

        return EXIT_SUCCESS;
}

The output of the above program is

res equals m.begin()
zero

My question: From the output, it looks like, the insert() operation
starts searching from the begin() position even though the hint is to
start at m.end() which is passed as the first argument to m.insert().
I am unable to understand this.

What actually does 'passing the iterator as hint' to the insert()
function mean ?

Does it mean, it will start searching ONLY from this hint iterator or
it will search from begin() irrespective of whatever 'hint iterator'
argument is passed(ie, in essence, it is ignored). ? How does this
version of insert() work ?

Kindly explain.

Thanks
V.Subramanian




 2 Posts in Topic:
question on map::insert(iterator, const value& v)
"subramanian100in@[E  2008-05-04 00:16:26 
Re: question on map::insert(iterator, const value& v)
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-04 09:27:02 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed May 14 19:13:21 CDT 2008.