主类创建运行轮胎类的实例。Tire 类调用成员函数bool tire<TypeTire, TypeNode>::add_node(TypeTire const& input_data)
以将所有字符“ABCDEFAB”推送到向量 by node_root_vec.push_back(node_data);
。推到向量之前的node_data
创建。node_data = new node_tire<TypeTire, TypeNode>();
#include <iostream>
#include <string>
#include <vector>
using namespace boost;
template<typename TypeTire, typename TypeNode>
struct node_tire {
// data in node
TypeNode const *data;
int addr_node;
// address insert next node
std::vector<node_tire<TypeTire, TypeNode> > addr_next_node;
// address insert pervious node
std::vector<node_tire<TypeTire, TypeNode> > addr_prve_node;
// insert data to node
node_tire() : data(NULL) { }
};
template<typename TypeTire, typename TypeNode>
class tire
{
public:
tire( );
node_tire<TypeTire, TypeNode> *check_node(node_tire<TypeTire, TypeNode>& node);
bool add_node(TypeTire const& data);
private:
node_tire<TypeTire, TypeNode> *node_data;
std::vector<node_tire<TypeTire, TypeNode>* > node_root_vec;
// handling node
std::vector<shared_ptr<node_tire<TypeTire, TypeNode> > > node_root_shared_ptr;
typedef shared_ptr<node_tire<TypeTire, TypeNode> > node_ptr;
};
template<typename TypeTire, typename TypeNode>
tire<TypeTire, TypeNode>::tire()
{
}
// Finding data in root node before travelling in branch.
template<typename TypeTire, typename TypeNode>
node_tire<TypeTire, TypeNode> *tire<TypeTire, TypeNode>::check_node(node_tire<TypeTire, TypeNode>& node)
{
int index_root_node = 0;
for(typename std::vector<node_tire<TypeTire, TypeNode>* >::iterator iter_root = node_root_vec.begin();
iter_root != node_root_vec.end();
++iter_root, ++index_root_node) {
node_tire<TypeTire, TypeNode> *node_in_vec = *iter_root;
TypeNode const *data = node_in_vec->data;
std::cout<<" Compare : " << *data << " : " << *node.data <<", Data address : " << &data <<std::endl;
if( *data == *node.data) {
node_tire<TypeTire, TypeNode> *node_in_root = node_root_vec[index_root_node];
std::cout<<"Address fond node in root : " << &node_root_vec[index_root_node] <<", value : " << node_in_root->data <<std::endl;
return node_root_vec[index_root_node];
}
}
return NULL;
}
template<typename TypeTire, typename TypeNode>
bool tire<TypeTire, TypeNode>::add_node(TypeTire const& input_data)
{
int index_input = 0;
for(typename TypeTire::const_iterator iter_input = input_data.begin(); iter_input != input_data.end(); ++iter_input, ++index_input) {
node_data = new node_tire<TypeTire, TypeNode>();
node_data->data = &(*iter_input);
std::cout<<" Node Address : " << &node_data <<std::endl;
// Found data in root node in condition else.
// Not found in root node to if.
if(check_node(*node_data) == NULL) {
node_root_vec.push_back(node_data);
}
}
}
// mian.cpp
int main()
{
tire<std::string, char> tire_node;
std::string str_test = "ABCDEFAB";
tire_node.add_node(str_test);
}
我从向量打印node_data
发布地址,它应该是新地址node_data
,但在创建后只显示旧地址node_data = new node_tire<TypeTire, TypeNode>();
。为什么不能新地址node_data
?
Node Address : 0x7fff0ebb28b0
Node Address : 0x7fff0ebb28b0
Compare : A : B, Data address : 0x7fff0ebb2760
Node Address : 0x7fff0ebb28b0
Compare : A : C, Data address : 0x7fff0ebb2760
Compare : B : C, Data address : 0x7fff0ebb2760
Node Address : 0x7fff0ebb28b0
Compare : A : D, Data address : 0x7fff0ebb2760
Compare : B : D, Data address : 0x7fff0ebb2760
Compare : C : D, Data address : 0x7fff0ebb2760
Node Address : 0x7fff0ebb28b0
Compare : A : E, Data address : 0x7fff0ebb2760
Compare : B : E, Data address : 0x7fff0ebb2760
Compare : C : E, Data address : 0x7fff0ebb2760
Compare : D : E, Data address : 0x7fff0ebb2760
Node Address : 0x7fff0ebb28b0
Compare : A : F, Data address : 0x7fff0ebb2760
Compare : B : F, Data address : 0x7fff0ebb2760
Compare : C : F, Data address : 0x7fff0ebb2760
Compare : D : F, Data address : 0x7fff0ebb2760
Compare : E : F, Data address : 0x7fff0ebb2760
Node Address : 0x7fff0ebb28b0