2014-03-24 40 views
0

我想以簡單的方式排序數組,但我確實得到下面的錯誤。如何處理?'sort(int [2000],int)'沒有匹配的函數|而排序陣列

**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; 
} 

回答

1

正確的說法是:

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

這個函數有兩個迭代器,開始和範圍的結束進行排序。指針是一個隨機訪問迭代器,所以它可以被一個需要的函數使用。在這種情況下,v + 2000指向數組末尾並正確代表範圍的末尾。

+0

@chris:據此編輯。 – 2014-03-24 20:50:16

1

你有兩種可能性:

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

或者

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

第一種方法只適用於數組,後者的作品與std::vectorstd::array和其它容器的很多的。

相關問題