如何使用标准模板库 std::sort()对声明为 int v[2000];
std::sort()
int v[2000]
C + + 是否提供了一些函数来获取数组的开始和结束索引?
#include <algorithm> static const size_t v_size = 2000; int v[v_size]; // Fill the array by values std::sort(v, v + v_size);
In C++11:
#include <algorithm> #include <array> std::array<int, 2000> v; // Fill the array by values std::sort(v.begin(), v.end());
You can sort it std::sort(v, v + 2000)
std::sort(v, v + 2000)
If you don't know the size, you can use:
std::sort(v, v + sizeof v / sizeof v[0]);
Even if you do know the size, it's a good idea to code it this way as it will reduce the possibility of a bug if the array size is changed later.
In C++0x/11 we get std::begin and std::end which are overloaded for arrays:
std::begin
std::end
#include <algorithm> int main(){ int v[2000]; std::sort(std::begin(v), std::end(v)); }
If you don't have access to C++0x, it isn't hard to write them yourself:
// 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<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 << "Desending Series" << endl; for(int i = 0;i < 5; i++) { cout << endl; cout << a[i] << endl; } }
you can use,
std::sort(v.begin(),v.end());
sorting method without std::sort:
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; } } }
Run complete example:
#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; }
you can use sort() in C++ STL. sort() function Syntax :
sort(array_name, array_name+size) So you use sort(v, v+2000);
C++ sorting using sort function
#include <bits/stdc++.h> using namespace std; vector <int> v[100]; int main() { sort(v.begin(), v.end()); }
Use the C++ std::sort function:
#include <algorithm> using namespace std; int main() { vector<int> v(2000); sort(v.begin(), v.end()); }
It is as simple as that ... C++ is providing you a function in STL (Standard Template Library) called sort which runs 20% to 50% faster than the hand-coded quick-sort.
sort
Here is the sample code for it's usage:
std::sort(arr, arr + size);
//sort by number bool sortByStartNumber(Player &p1, Player &p2) { return p1.getStartNumber() < p2.getStartNumber(); } //sort by string bool sortByName(Player &p1, Player &p2) { string s1 = p1.getFullName(); string s2 = p2.getFullName(); return s1.compare(s2) == -1; }
With the Ranges library that is coming in C++20, you can use
ranges::sort(arr);
directly, where arr is a builtin array.
arr
sort() can be applied on both array and vector in C++ to sort or re-arrange elements .
1. C++ sort() in case of a vector:
// importing vector, algorithm & iostream
using namespace std;
int main() {
vector v = {5,4,3,2,8}; // depending on your vector size
sort(v.begin(), v.end());
cout<<v[1]; //testing the sorted element positions by printing
return 0;
}
2. C++ sort() in case of an array:
// including algorithm & iostream
int array[] = {10, 35, 85}; // array size 2000 in your case int n = sizeof(array)/sizeof(array[0]);
sort(array, array+3);
cout<<array[0];
Note: Both the above snippets were tested with modern C++ versions (11,17 & 20) before posting here .