如何使用std :: sort在C ++中对数组进行sorting

如何使用标准模板库std::sort()来对一个声明为int v[2000]的数组进行sortingint v[2000] ;

C ++是否提供了一些可以获得数组的开始和结束索引的函数?

在C ++ 0x / 11中,我们得到了std::beginstd::end ,它们对于数组是重载的:

 #include <algorithm> int main(){ int v[2000]; std::sort(std::begin(v), std::end(v)); } 

如果您无法访问C ++ 0x,则自己编写它们并不困难:

 // for container with nested typedefs, non-const version template<class Cont> typename Cont::iterator begin(Cont& c){ return c.begin(); } template<class Cont> typename Cont::iterator end(Cont& c){ return c.end(); } // const version template<class Cont> typename Cont::const_iterator begin(Cont const& c){ return c.begin(); } template<class Cont> typename Cont::const_iterator end(Cont const& c){ return c.end(); } // overloads for C style arrays template<class T, std::size_t N> T* begin(T (&arr)[N]){ return &arr[0]; } template<class T, std::size_t N> T* end(T (&arr)[N]){ return arr + N; } 
 #include <algorithm> static const size_t v_size = 2000; int v[v_size]; // Fill the array by values std::sort(v,v+v_size); 

在C ++ 11中 :

 #include <algorithm> #include <array> std::array<int, 2000> v; // Fill the array by values std::sort(v.begin(),v.end()); 

如果您不知道尺寸,可以使用:

 std::sort(v, v + sizeof v / sizeof v[0]); 

即使你知道这个大小,最好是这样编写代码,因为如果稍后数组大小被改变,它将减less错误的可能性。

你可以对它进行sortingstd::sort(v, v + 2000)

 //It is working #include<iostream> using namespace std; void main() { int a[5]; int temp=0; cout<<"Enter Values"<<endl; for(int i=0;i<5;i++) { cin>>a[i]; } for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } cout<<"Asending Series"<<endl; for(int i=0;i<5;i++) { cout<<endl; cout<<a[i]<<endl; } for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { if(a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } cout<<"Desnding Series"<<endl; for(int i=0;i<5;i++) { cout<<endl; cout<<a[i]<<endl; } } 

你可以在C ++ STL中使用sort()。 sort()函数语法:

  sort(array_name, array_name+size) So you use sort(v, v+2000); 

使用sortingfunction的C ++sorting

 #include <bits/stdc++.h> using namespace std; vector <int> v[100]; int main() { sort(v.begin(), v.end()); } 

使用C ++ std::sort函数:

 #include <algorithm> using namespace std; int main() { vector<int> v(2000); sort(v.begin(), v.end()); } 

sorting方法没有std::sort

 // sorting myArray ascending int iTemp = 0; for (int i = 0; i < ARRAYSIZE; i++) { for (int j = i + 1; j <= ARRAYSIZE; j++) { // for descending sort change '<' with '>' if (myArray[j] < myArray[i]) { iTemp = myArray[i]; myArray[i] = myArray[j]; myArray[j] = iTemp; } } } 

运行完整的例子:

 #include <iostream> // std::cout, std::endl /* http://en.cppreference.com/w/cpp/header/iostream */ #include <cstdlib> // srand(), rand() /* http://en.cppreference.com/w/cpp/header/cstdlib */ #include <ctime> // time() /* http://en.cppreference.com/w/cpp/header/ctime */ int main() { const int ARRAYSIZE = 10; int myArray[ARRAYSIZE]; // populate myArray with random numbers from 1 to 1000 srand(time(0)); for (int i = 0; i < ARRAYSIZE; i++) { myArray[i] = rand()% 1000 + 1; } // print unsorted myArray std::cout << "unsorted myArray: " << std::endl; for (int i = 0; i < ARRAYSIZE; i++) { std::cout << "[" << i << "] -> " << myArray[i] << std::endl; } std::cout << std::endl; // sorting myArray ascending int iTemp = 0; for (int i = 0; i < ARRAYSIZE; i++) { for (int j = i + 1; j <= ARRAYSIZE; j++) { // for descending sort change '<' with '>' if (myArray[j] < myArray[i]) { iTemp = myArray[i]; myArray[i] = myArray[j]; myArray[j] = iTemp; } } } // print sorted myArray std::cout << "sorted myArray: " << std::endl; for (int i = 0; i < ARRAYSIZE; i++) { std::cout << "[" << i << "] -> " << myArray[i] << std::endl; } std::cout << std::endl; return 0; } 

您可以使用,

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