How to use standard template library std::sort()
to sort an array declared as int v[2000]
;
如何使用标准模板库std::sort()对声明为int v的数组进行排序[2000];
Does C++ provide some function that can get the begin and end index of an array?
c++是否提供了一些函数来获取数组的开始和结束索引?
11 个解决方案
#1
90
In C++0x/11 we get std::begin
and std::end
which are overloaded for arrays:
在c++ 0x/11中,我们得到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:
如果您没有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;
}
#2
60
#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:
在c++中11:
#include <algorithm>
#include <array>
std::array<int, 2000> v;
// Fill the array by values
std::sort(v.begin(),v.end());
#3
26
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.
即使您知道它的大小,以这种方式编写它也是一个好主意,因为如果稍后更改数组大小,它将减少出现错误的可能性。
#4
15
You can sort it std::sort(v, v + 2000)
你可以对它进行排序:sort(v, v + 2000)
#5
3
//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;
}
}
#6
2
you can use sort() in C++ STL. sort() function Syntax :
您可以在c++ STL中使用sort()。()函数的语法:
sort(array_name, array_name+size)
So you use sort(v, v+2000);
#7
2
C++ sorting using sort function
c++排序函数
#include <bits/stdc++.h>
using namespace std;
vector <int> v[100];
int main()
{
sort(v.begin(), v.end());
}
#8
0
sorting method without std::sort
:
排序方法没有std:::
// 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;
}
#9
0
Use the C++ std::sort
function:
使用c++ std::排序函数:
#include <algorithm>
using namespace std;
int main()
{
vector<int> v(2000);
sort(v.begin(), v.end());
}
#10
0
It is as simple as that.....c++ is providing you a built-in function in STL(Standard Template Library) called sort(arr_name ,arr_name+arr_size)
which runs 20% to 50% faster than the hand-coded quick-sort. Also you better include the header file
就是这么简单……c++在STL(标准模板库)中提供了一个名为sort(arr_name,arr_name+arr_size)的内置函数,比手工编写的快速排序快20%到50%。您还应该包括头文件。
#include<bits/stdc++.h>
Here is the sample code for it's usage:
下面是使用的示例代码:
#include<isostream>
#include<bits/stdc++.h>
using namespace std;
main()
{
int n;cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}
#11
-1
you can use,
您可以使用,
std::sort(v.begin(),v.end());
#1
90
In C++0x/11 we get std::begin
and std::end
which are overloaded for arrays:
在c++ 0x/11中,我们得到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:
如果您没有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;
}
#2
60
#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:
在c++中11:
#include <algorithm>
#include <array>
std::array<int, 2000> v;
// Fill the array by values
std::sort(v.begin(),v.end());
#3
26
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.
即使您知道它的大小,以这种方式编写它也是一个好主意,因为如果稍后更改数组大小,它将减少出现错误的可能性。
#4
15
You can sort it std::sort(v, v + 2000)
你可以对它进行排序:sort(v, v + 2000)
#5
3
//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;
}
}
#6
2
you can use sort() in C++ STL. sort() function Syntax :
您可以在c++ STL中使用sort()。()函数的语法:
sort(array_name, array_name+size)
So you use sort(v, v+2000);
#7
2
C++ sorting using sort function
c++排序函数
#include <bits/stdc++.h>
using namespace std;
vector <int> v[100];
int main()
{
sort(v.begin(), v.end());
}
#8
0
sorting method without std::sort
:
排序方法没有std:::
// 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;
}
#9
0
Use the C++ std::sort
function:
使用c++ std::排序函数:
#include <algorithm>
using namespace std;
int main()
{
vector<int> v(2000);
sort(v.begin(), v.end());
}
#10
0
It is as simple as that.....c++ is providing you a built-in function in STL(Standard Template Library) called sort(arr_name ,arr_name+arr_size)
which runs 20% to 50% faster than the hand-coded quick-sort. Also you better include the header file
就是这么简单……c++在STL(标准模板库)中提供了一个名为sort(arr_name,arr_name+arr_size)的内置函数,比手工编写的快速排序快20%到50%。您还应该包括头文件。
#include<bits/stdc++.h>
Here is the sample code for it's usage:
下面是使用的示例代码:
#include<isostream>
#include<bits/stdc++.h>
using namespace std;
main()
{
int n;cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}
#11
-1
you can use,
您可以使用,
std::sort(v.begin(),v.end());