动态数组排序算法编译器错误 [英] sort algorithm compiler error with dynamic array

查看:148
本文介绍了动态数组排序算法编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难让std :: begin()与动态分配的数组(指针)一起工作,在这里它似乎与堆栈分配的数组可以正常工作。

I'm having difficulty getting std::begin() to work with a dynamically allocated array (pointer), where it seems to work fine with a stack-allocated one.

这有效:

int numbers[100];

// Fill array with numbers

std::sort(std::begin(numbers), std::end(numbers));

这不是

int* numbers = new int[10000000];

// Fill array with numbers

std::sort(std::begin(numbers), std::end(numbers));

这是结果错误。

ptests.cpp:120:33: error: no matching function for call to ‘begin(int*&)’
     std::sort(std::begin(numbers), std::end(numbers));
                                 ^
ptests.cpp:120:33: note: candidates are:
In file included from /usr/include/c++/4.8/utility:74:0,
                 from /usr/include/c++/4.8/algorithm:60,
                 from ptests.cpp:1:
/usr/include/c++/4.8/initializer_list:89:5: note: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)
     begin(initializer_list<_Tp> __ils) noexcept
     ^
/usr/include/c++/4.8/initializer_list:89:5: note:   template argument deduction/substitution failed:
ptests.cpp:120:33: note:   mismatched types ‘std::initializer_list<_Tp>’ and ‘int*’
     std::sort(std::begin(numbers), std::end(numbers));
                                 ^
In file included from /usr/include/c++/4.8/string:51:0,
                 from /usr/include/c++/4.8/random:41,
                 from /usr/include/c++/4.8/bits/stl_algo.h:65,
                 from /usr/include/c++/4.8/algorithm:62,
                 from ptests.cpp:1:
/usr/include/c++/4.8/bits/range_access.h:48:5: note: template<class _Container> decltype (__cont.begin()) std::begin(_Container&)
     begin(_Container& __cont) -> decltype(__cont.begin())

是否可以将动态指针转换为类型begin ()期望?

Would it be possible to cast the dynamic pointer to the type begin() expects? Any advice would be appreciated!

推荐答案

std::end(numbers)

数字变量是

int *

它是什么类型。指向整数的指针并没有告诉任何人它指向多少 int 。您将其分配为指向10000000 int s。但是,一旦分配了它,最终得到的就是指向 int 的指针,仅此而已。跟踪该指针到底指向您的内容取决于您的代码。如果要编写,您将使用完全相同的指针来完成操作,只需:

That's what it's type is. And there's nothing about this pointer to an integer that tells anyone about how many ints it's pointing to. You allocated it to point to 10000000 ints. But once you've allocated it, all you end up with is a pointer to an int, and nothing more. It is up to your code to keep track of what exactly this pointer is pointing you. You'll wind up exactly with the same pointer if you were to write, simply:

int n;

int *numbers=&n;

数字指针与您创建的一个。它只是指向 int 的指针。没事,没什么。

This numbers pointer is completely identical to the one you've created. It's just a pointer to an int. Nothing more, nothing less.

std :: begin() std :: end()不适用于普通指针,例如您指向 int 的指针,因为正如我刚才所说,没有任何关系指向某个对象的指针,该指针指示其指向的连续对象数。可能是一个 int 。可能是两个 int s。也许一百万。或者,如果指针是 nullptr ,则可能什么也没有。

std::begin() and std::end() do not work for ordinary pointers, like your pointer to an int here, because, as I've just said, there's nothing about a pointer to some object that indicates how many consecutive objects it's pointing to. It could be one int. It could be two ints. Maybe a million. Or maybe nothing it all, if the pointer is a nullptr.

如果要对动态分配的内容进行排序 int 数组,只需直接传递开始和结束指针即可:

If you want to sort your dynamically-allocated int array, just pass the beginning and the ending pointer directly:

std::sort(number, numbers+10000000);

这篇关于动态数组排序算法编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆