Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 指针数组中地址的索引?_C++_Arrays_Pointers - Fatal编程技术网

C++ 指针数组中地址的索引?

C++ 指针数组中地址的索引?,c++,arrays,pointers,C++,Arrays,Pointers,以下代码是满足以下要求的解决方案: “更改§27.9中的链接和列表的表示形式,而不更改功能提供的用户界面。在链接的数组中分配链接,并让成员:第一个,最后一个,上一个,和下一个成为int“Br>数组”(6)第27章-编程:C++原理与B. Stroustrup < /P> 该接口继承自一个普通的侵入式双链表实现。我添加了bool数组(以及相关函数)来跟踪内存: #include <iostream> struct Link { int next; int prev;

以下代码是满足以下要求的解决方案:

“更改§27.9中的
链接
列表
的表示形式,而不更改功能提供的用户界面。在
链接的数组中分配
链接
,并让成员:
第一个
最后一个
上一个
,和
下一个
成为
int
“Br>数组”(6)第27章-编程:C++原理与B. Stroustrup < /P> 该接口继承自一个普通的侵入式双链表实现。我添加了bool数组(以及相关函数)来跟踪内存:

 #include <iostream>

struct Link
{
    int next;
    int prev;
};

//------------------------------------------------------------------------------------

struct List
{
    Link** head;
    int first;      // points to the current first node
    int last;

    bool* available;
    int list_size;

    int get_index()
    {
        for (int i = 0; i < list_size; ++i)
        {
            if (available[i] == true) 
            {
                available[i] = false;
                return i;
            }
        }
        throw std::bad_alloc("bla bla!\n");
    }

    List()
    {
        list_size = 30;

        head = new Link*[list_size];
        available = new bool[list_size];

        first = -1;
        last = -1;

        for (int i = 0; i < list_size; ++i)
        {
            available[i] = true;
        }
    }

    void List::push_back(Link* l)
    {
        if (l == nullptr)
        {
            throw std::invalid_argument("bla bla!\n");
        }

        int index = get_index();
        head[index] = l;

        if (last != -1)
        {
            head[last]->next = index;
            head[index]->prev = last;
        }
        else
        {
           first = index;
           head[index]->prev = -1;
        }

        last = index;
        head[index]->next = -1;
    }

    void push_front(Link* l)
    {
        if (l == nullptr)
        {
            throw std::invalid_argument("bla bla\n");
        }

        int index = get_index();
        head[index] = l;

        if (first != -1)
        {
            head[first]->prev = index;
            head[index]->next = first;
        }
        else
        {
            last = index;
            head[index]->next = -1;
        }

        first = index;
        head[index]->prev = -1;
    }

    // index = ptr - base
    std::ptrdiff_t index_from_address(Link* l) { return l - head[0]; }

    Link* front() const { return head[first]; }
};

//------------------------------------------------------------------------------------

int main()
{
    List l;

    for (int i = 0; i < 10; ++i)
    {
        l.push_back(new Link());
    }

    for (int i = 0; i < 10; ++i)
    {
        l.push_front(new Link());
    }

    std::cout <<"first = "<< l.first <<", index = " << l.index_from_address(l.front());
    getchar();
}
#包括
结构链接
{
int-next;
国际通行证;
};
//------------------------------------------------------------------------------------
结构列表
{
连杆**头;
int first;//指向当前的第一个节点
int last;
bool*可用;
int列表大小;
int get_index()
{
对于(int i=0;inext=索引;
head[索引]->prev=最后一个;
}
其他的
{
第一个=索引;
头[指数]->prev=-1;
}
last=索引;
head[索引]->next=-1;
}
无效前推(连杆*l)
{
if(l==nullptr)
{
throw std::无效的_参数(“bla bla\n”);
}
int index=get_index();
水头[指数]=l;
如果(第一个!=-1)
{
head[第一]->prev=指数;
head[索引]->next=第一个;
}
其他的
{
last=索引;
head[索引]->next=-1;
}
第一个=索引;
头[指数]->prev=-1;
}
//索引=ptr-基
std::ptrdiff_t index_from_address(Link*l){return l-head[0];}
Link*front()常量{返回头[first];}
};
//------------------------------------------------------------------------------------
int main()
{
清单l;
对于(int i=0;i<10;++i)
{
l、 向后推(新链接());
}
对于(int i=0;i<10;++i)
{
l、 向前推(新连杆());
}
标准::cout
这里比较两个指针的值。数组中的所有指针都默认初始化,因此它们的值是不确定的,因此访问值的行为是不确定的

您可能打算从_address
查找存储特定指针对象的索引,而不是指向的对象,因为指向的对象不在
head
指向的数组中。为此,您必须添加一整串
&

 Link*& front() const // return a reference to the pointer object, not a copy

 //  take a reference to the pointer as an argument, add const for good measure
 std::ptrdiff_t index_from_address(Link*& l) const
// compare the addresses of the pointers, rather than values
 { return &l - &head[0]; }

您在调试器中发现了什么?我不知道这应该是什么类型的数据结构。您在哪里初始化头[0]和头[first]?为什么第一个节点有索引15?这个代码甚至没有任何意义。@Ziezi:但是你没有一个
链接
数组。你有一个
链接*
数组。据我所知,这是一个绝对合理的问题的完美答案。仍然无法理解为什么投票失败和结束?无论如何,谢谢你!
 Link*& front() const // return a reference to the pointer object, not a copy

 //  take a reference to the pointer as an argument, add const for good measure
 std::ptrdiff_t index_from_address(Link*& l) const
// compare the addresses of the pointers, rather than values
 { return &l - &head[0]; }