Salut,

J'ai recommencer à coder en C++ il n'y a pas très longtemps, et j'arrive rapidement à un problème (que j'ai été capable de reproduire). Le problème est : Lorsqu'une même variable est utilisée pour initialiser plusieurs entrées dans une variable map<const char*,string>, il se produit un comportement bizarre et plutôt inattendu.

Voici un code que j'ai fait pour simplifier le problème :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <map>
 
using namespace std;
 
map<const char*, string> *Map = new map<const char*, string>( );
 
void Set( const char* Name, const char* Value )
{
	cout << Name << "=" << Value << endl;
	(*Map)[Name] = Value;
}
 
int main()
{
	string Std;
	string Std2;
 
	for( int i = 1 ; i <= 8 ; i++ )
	{
		Std = "Variable"; Std.resize( i );
		Std2 = "Variable"; Std2.resize( i );
 
		Set( Std.c_str( ), Std2.c_str( ) );
	}
 
	cout << "Variable dump : " << endl;
	for( map<const char*,string>::iterator it = Map->begin( ) ; it != Map->end( ) ; it++ )
	{
		cout << it->first << " = " << it->second << endl;
	}
 
	delete Map;
 
	return 0;
}
Output :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
V=V
Va=Va
Var=Var
Vari=Vari
Varia=Varia
Variab=Variab
Variabl=Variabl
Variable=Variable
Variable dump :
Variable = Variable
Avez-vous déjà rencontré ce genre de problème ? Savez-vous comment le régler ?

Merci d'avance pour vos réponses .