C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

std::sortを使用してC++で配列をソートする方法


プログラミング言語では、並べ替えはデータに適用される基本的な機能であり、これらのデータを昇順または降順で並べ替えます。 C ++プログラムには、配列をソートするための関数std ::sort()があります。

sort(start address, end address)

ここで

Start address => The first address of the element.
Last address => The address of the next contiguous location of the last element of the array.

サンプルコード

#include <iostream>
#include <algorithm>
using namespace std;
void display(int a[]) {
   for(int i = 0; i < 5; ++i)
   cout << a[i] << " ";
}
int main() {
   int a[5]= {4, 2, 7, 9, 6};
   cout << "\n The array before sorting is : ";
   display(a);
   sort(a, a+5);
   cout << "\n\n The array after sorting is : ";
   display(a);
   return 0;
}

出力

The array before sorting is : 4 2 7 9 6

The array after sorting is : 2 4 6 7 9

  1. C#で配列クラスのSort()メソッドを使用するにはどうすればよいですか?

    Sort()メソッドは、配列の各要素のIComparable実装を使用して、1次元配列全体の要素を並べ替えます。 アレイを設定します。 int[] list = { 22, 12, 65, 9}; Sort()メソッドを使用して配列を並べ替えます。 Array.Sort(list); 以下は、Sort()メソッドの操作方法を学ぶための例です。 例 using System; namespace Demo {    class Program {       static void Main(string[] args) {  

  2. C#で配列を並べ替える方法は?

    以下は整数配列です。 int[] arr = { 99, 43, 86 }; 並べ替えるには、Sort()メソッドを使用します。 Array.Sort(arr); 以下は、Sort()メソッドを使用してC#で配列を並べ替える方法を示す完全なコードです。 例 using System; class Demo {    static void Main() {       int[] arr = { 99, 43, 86 };       // sort       Array.Sor