Re: Initialed value lost !
On Aug 9, 6:54 am, sam.bark...@gmail.com wrote:
Hi there,
I am trying to develop a compressed suffix trie.
But first I want to get the trie working.
The structure of the trie is like
class trie {
private:
bool last_node;
map<char, trie> children;
This is undefined behavior. You can't instantiate a standard
container on an incomplete type. It might work in some
implementations, but it might not.
//methods
....
....
....}
The problem is that after inserting one word,the values held by the
private members of the nodes(other than the root is lost).
insert is a recursive function
new node is added like this
trie new_child;
this->children[alphabet]=new_child;
new_child.insert(word.substr(1));//insert is call=
ed again.
Is the above supposed to be C++. Parts of it look like it,
others don't.
I'd implement a trie more or less:
class Trie
{
public:
void insert(
std::string::const_iterator begin,
std::string::const_iterator end ) ;
private:
boost::array< boost::scoped_ptr< Trie >, UCHAR_MAX + 1 >
myChildren ;
} ;
void
Trie::insert(
std::string::const_iterator begin,
std::string::const_iterator end )
{
if ( begin != end ) {
unsigned char index = *begin ;
if ( myChildren[ index ].get() == NULL ) {
myChildren[ index ].reset( new Trie ) ;
}
myChildren[ index ]->insert( begin + 1, end ) ;
}
}
Note that you need the dynamic allocation in order for the class
to contain instances of itself.
I had stepped through to debug this is what happened.I had a
pasteing the value of this ppointer and the data member
last_node
[...]
could anyone tell what I am doing wrong?
You haven't really posted enough of your own code for us to say.
But something similar to the above should work. (With regards
to the undefined behavior, I suspect that in general, if the
code compiles, it will work. But the standard still says it is
undefined, and there are implementations where it won't
compile.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34