#include <iostream>
using namespace std;
#include <string>
#include <map>
typedef struct node{
char a;
map<char , node*> b;
}node;
node a[26] ;
void add(string s){
node *prev = &(a[s[0]-'a']);
int i = 0;
int len = s.length();
for(i = 1; i < len; ++i){
map<char,node*>::iterator it = ((*prev).b).find(s[i]);
if(it != ((*prev).b).end()){
prev = it->second;
}
else{
cout << (*prev).a << endl;
node pt;
pt.a = s[i];
((*prev).b)[s[i]] = &pt;
prev = &pt;
}
}
}
int main(){
string s = "helloworld";
string t = "htllothis";
int i = 0 ;
for(i = 0;i < 26;++i){
a[i].a = 'a'+i;
}
add(s);
add(t);
return 0;
}
I am trying to implement tire datastructure using map and char but cout<< (*prev).a is printing some other chars. What is the mistake I have done?
typedef struct node { ... } node;is very C like. In C++, you can just simply usestruct node { ... };asnodeis useable as is without atypedef.typedef structcrap drives me nuts, what a terrible solution.