0

主类创建运行轮胎类的实例。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
4

2 回答 2

0

查看我的代码,您可能很快就会出错:

void test() {
    int n;
    for( int i = 0; i < 100; i++ ) {
        n = i + 2;
        std::cout << "Address of n is: " << &n << std::endl;
    }
}

你认为我的程序的输出应该改变吗?我改变了价值,n但我不能改变它的地址,可以吗?
在您的情况下,您正在更改 的值node_data,但您永远不能更改 的地址node_data。你能?你打印的是地址node_data而不是它的值。

于 2012-10-23T16:59:26.477 回答
0

您正在打印 &node_data,它是 node_data 成员的地址,它是轮胎对象地址的常量偏移量。

分配实例的地址是 node_data (您在其上创建新实例的地址)

于 2012-10-23T17:00:17.933 回答