3
4

1 回答 1

6

This version of insert takes the key by non-const reference, which means you can't use a temporary as the first value. This is to prevent memory leaks; in your code, temp would leak if the string constructor were to throw.

You must either create the key object before creating the raw pointer:

string key("SomeKey");
any* temp = new whatever;
SomeMap.insert(key, temp);

or use an auto_ptr to ensure that the object is deleted whatever happens:

auto_ptr<any> temp(new whatever);
SomeMap.insert("SomeKey", temp);
于 2010-06-18T17:20:51.977 回答