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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5897319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:07:36  来源:igfitidea点击:

How to use std::sort to sort an array in C++

c++sorting

提问by Xeo

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++ 是否提供了一些可以获取数组开始和结束索引的函数?

回答by Xeo

In C++0x/11 we get std::beginand std::endwhich are overloaded for arrays:

在 C++0x/11 中,我们得到std::beginstd::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;
}

回答by Naszta

#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()); 

回答by j_random_hacker

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.

即使您确实知道大小,以这种方式对其进行编码也是一个好主意,因为如果稍后更改数组大小,它将减少出现错误的可能性。

回答by Mayank

You can sort it std::sort(v, v + 2000)

你可以排序 std::sort(v, v + 2000)

回答by wahid Butt

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


}

回答by rashedcs

you can use sort() in C++ STL. sort() function Syntax :

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

 sort(array_name, array_name+size)      

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

回答by Mahedi Hasan Durjoy

C++ sorting using sort function

使用 sort 函数的 C++ 排序

#include <bits/stdc++.h>
 using namespace std;

vector <int> v[100];

int main()
{
  sort(v.begin(), v.end());
}

回答by Mahedi Hasan Durjoy

Use the C++ std::sortfunction:

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

#include <algorithm>
using namespace std;

int main()
{
  vector<int> v(2000);
  sort(v.begin(), v.end());
}

回答by risheek reddy

It is as simple as that ... C++ is providing you a function in STL (Standard Template Library) called sortwhich runs 20% to 50% faster than the hand-coded quick-sort.

就这么简单...... C++ 为您提供了一个名为 STL(标准模板库)的函数,sort它的运行速度比手动编码的快速排序快 20% 到 50%。

Here is the sample code for it's usage:

这是它的用法示例代码:

std::sort(arr, arr + size);

回答by user5465465465

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