简体   繁体   English

没有匹配函数'sort(int [2000],int)'| 排序数组时

[英]No matching function for 'sort(int [2000], int)'| while sorting array

I wanted to sort array in a simple way but I do get this error below. 我想以一种简单的方式对数组进行排序,但我确实在下面得到了这个错误。 How to deal with that? 怎么处理?

**No matching function for call to 'sort(int [2000], int)'|**

#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int v[2000];
    std::sort(v, 2000);
    cout << "Hello world!" << endl;
    return 0;
}

The correct statement is: 正确的说法是:

std::sort(v, v + 2000);

The function takes two iterators, the beginning and end of the range to sort. 该函数需要两个迭代器,即要排序的范围的开头和结尾。 A pointer is a random-access iterator, so it can be used by a function that expects one. 指针是一个随机访问迭代器,因此可以由期望它的函数使用它。 In this case, v + 2000 points past the end of the array and correctly stands for the end of the range. 在这种情况下, v + 2000点超过数组的末尾并正确代表范围的结束。

You have two possibilities: 你有两种可能性:

std::sort(v, v + 2000);

Or 要么

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

The first approach only works with arrays, the latter works with std::vector , std::array and lot's of other containers. 第一种方法仅适用于数组,后者适用于std::vectorstd::array和其他容器的批次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM