Les map en c++

Fermé
my_bok Messages postés 1 Date d'inscription lundi 7 février 2011 Statut Membre Dernière intervention 7 février 2011 - Modifié par my_bok le 7/02/2011 à 20:45
Bonjour,

lorsque je remplace la clé node par un string directement il marche mais bien mais lorsque je mets ma structure node il ne marche pas.

en outre lorsque j'utilise directement la classe string et j'affiche il l'affiche avec des espaces entre les lettres je ne comprend pas pourquoi.
merci d'avance pou votre aide.

program correct:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>

using namespace std;

/*struct node
{
string t;
node(string val): t(val){};
};*/

struct sun
{
vector<string> s;
int indegree;
sun(string val, int i)
{
s.push_back(val); indegree=i;

};
};

typedef map<string,sun> p_map;


int _tmain(int argc, _TCHAR* argv[])
{
p_map graf;
ifstream file("data.txt", ios::in);
if (!file) exit(1);

string str;
int line=1;

p_map::iterator it;

while(file>>str)
{
//node A(str);
sun B("lucas",0);
graf.insert(p_map::value_type(str,B));
}
it=graf.begin();
(*it).second.s.push_back("luca");

vector<string>::iterator t=(*it).second.s.begin();t++;
cout<<*t;

for ( it=graf.begin() ; it!= graf.end(); it++ )
cout << (*it).first;

cout<<endl;
system("pause");
return 0;
}

program erroné:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>

using namespace std;

struct node
{
string t;
node(string val): t(val){};
};

struct sun
{
vector<string> s;
int indegree;
sun(string val, int i)
{
s.push_back(val); indegree=i;

};
};

typedef map<node,sun> p_map;


int _tmain(int argc, _TCHAR* argv[])
{
p_map graf;
ifstream file("data.txt", ios::in);
if (!file) exit(1);

string str;
int line=1;

p_map::iterator it;

while(file>>str)
{
node A(str); sun B("lucas",0);
cout<<A.t;
graf.insert(p_map::value_type(A,B));
}
cout<<endl;
system("pause");
return 0;
}
A voir également: