I'm having problems updating a multimap. I need to change the key value. I have found various examples but none seem to work. Any help gratefully received.

Below a simplified map test bit of code. I had hoped the line between the commented asterisks would do the trick but seems not/

/*
Testing maps
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <list>
#include <string>
#include <time.h>
#include <map>
#include <vector>
#include <conio.h>

using namespace std;

int main(int argc, char *argv[])
{
	typedef map <string, string> MAP_STRING_MESSAGE;
	MAP_STRING_MESSAGE::iterator ilocator1;
   	MAP_STRING_MESSAGE::iterator findlocator1;
   	
	MAP_STRING_MESSAGE amap;
	
	amap.insert(make_pair("first","One"));
	amap.insert(make_pair("second","Two"));
	amap.insert(make_pair("third","Three"));
	
	for ( ilocator1 = amap.begin(); ilocator1 != amap.end(); \
   		++ ilocator1 )
   	{
		cout << ilocator1->first << "\n";
		cout << ilocator1->second << "\n";
	cout << "____________________________\n";		
	}
		   	
	findlocator1 = amap.find("second");
// ************
	findlocator1->first = "fourth";
// ************
	
	for ( ilocator1 = amap.begin(); ilocator1 != amap.end(); \
   		++ ilocator1 )
   	{
		cout << ilocator1->first << "\n";
		cout << ilocator1->second << "\n";
	cout << "____________________________\n";			
	}
getch();	
}

Try it,

const_cast<string &>(findlocator1->first) = "fourth";

Try it,

const_cast<string &>(findlocator1->first) = "fourth";

Many tx that works (as u know) but why does

findlocator1->second.s_A_header = message_element.s_header;

work??

I guess because 'first' and 'second' are pointers in effect and by adding the s_header variable that one becomes a string?

I suppose there is no dummy name for the key so that the coding could be consistent? Something like

findlocator1->first.key= "fourth";

Maybe I'm asking too much of the STL.

Read this document carefully - http://www.gotw.ca/publications/mill11.htm

Ada tx again. I had seen this doc but when the examples within it failed it lost some credibility and I found other stuff suggesting a key change was OK.

But now I see the error of my ways.

So I guess it is down to delete and insert.

cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.