Aleksander Fular
Aleksander Fular

Reputation: 873

boost::flyweight as value in std::map

I am wondering why std::map does not replace value after using insert.
example:

using std::string;
using boost::flyweight;
using std::map;
int main() 
{ 
    map<string,flyweight<string>> testMap;

    flyweight<string> str("1");
    testMap.insert(std::make_pair("1","1"));
    testMap.insert(std::make_pair("1","2"));
    str = "2";
    printf("Inside map at \"1\" is:%s\r\n",testMap.at("1").get().c_str());
    printf("str equals %s",str.get().c_str());
}

Will print:

Inside map at "1" is: 1

str equals 2

used flyweight<string> just as a example, same thing happens when using ints.

Working on windows OS, visual 2010 ide.
Thanks,

Upvotes: 3

Views: 506

Answers (1)

P0W
P0W

Reputation: 47834

std::pair<iterator,bool> insert( const value_type& value ); of std::map

won't insert element if it already exists

Here pair::second element in the pair is set to true if a new element was inserted or false if an equivalent key already existed.

Your question has nothing to do with boost

Check the example on this page. It illustrates your scenario.

Upvotes: 2

Related Questions