Computer >> Máy Tính >  >> Lập trình >> C ++

Chương trình C ++ để sắp xếp các biến của bất kỳ kiểu dữ liệu nào

Chúng tôi được cung cấp các giá trị của các kiểu dữ liệu khác nhau như nó có thể là kiểu số nguyên, float, string, bool, v.v. và nhiệm vụ là sắp xếp các biến của bất kỳ kiểu dữ liệu nào bằng một phương thức hoặc hàm phổ biến và hiển thị kết quả.

Trong C ++, chúng ta có thể sử dụng std ::sort để sắp xếp bất kỳ loại mảng nào có sẵn trong thư viện mẫu chuẩn C ++ (STL). Theo mặc định, hàm sắp xếp sắp xếp phần tử mảng theo thứ tự tăng dần. Hàm Sort () có ba đối số -

Bắt đầu phần tử trong danh sách mảng, tức là từ nơi bạn muốn bắt đầu sắp xếp của mình

Phần tử kết thúc trong danh sách mảng, tức là cho đến nơi bạn muốn thực hiện việc sắp xếp của mình

Thay đổi cài đặt mặc định của hàm sắp xếp thành thứ tự giảm dần bằng cách chuyển hàm lớn hơn () để sắp xếp theo thứ tự giảm dần.

Ví dụ

Input-: int arr[] = { 2, 1, 5, 4, 6, 3}
Output-: 1, 2, 3, 4, 5, 6

Input-: float arr[] = { 30.0, 21.1, 29.0, 45.0}
Output-: 21.1, 29.0, 30.0, 45.0

Input-: string str =  {"tutorials point is best", "tutorials point", "www.tutorialspoint.com"}
Output-: tutorials point   tutorials point is best   www.tutorialspoint.com

Phương pháp tiếp cận được sử dụng trong chương trình dưới đây như sau -

  • Nhập các biến của các kiểu dữ liệu khác nhau như số nguyên, số float, chuỗi, v.v.
  • Áp dụng hàm sort () sẽ sắp xếp các phần tử của bất kỳ loại mảng nào
  • In kết quả

Thuật toán

Start
Step 1-> create template class for operating upon different type of data Template <class T>
Step 2-> Create function to display the sorted array of any data type
   void print(T arr[], int size)
   Loop For size_t i = 0 and i < size and ++i
      print arr[i]
   End
Step 3-> In main()
   Declare variable for size of an array int num = 6
   Create an array of type integer int arr[num] = { 10, 90, 1, 2, 3 }
   Call the sort function sort(arr, arr + num)
   Call the print function print(arr, num)
   Create an array of type string string str[num] = { "tutorials point is best",  "tutorials point", "www.tutorialspoint.com" }
   Call the sort function sort(str, str + num)
   Call the print function print(str, num)
   Create an array of type float float float_arr[num] = { 32.0, 12.76, 10.00 }
   Call the sort function sort(float_arr, float_arr+num)
   Call the print function print(float_arr, num)
Stop

Ví dụ

#include <bits/stdc++.h>
using namespace std;
// creating variable of template class
template <class T>
void print(T arr[], int size) {
   for (size_t i = 0; i < size; ++i)  
   cout << arr[i] << "   ";    
   cout << endl;
}
int main() {
   int num = 6;
   int arr[num] = { 10, 90, 1, 2, 3 };
   sort(arr, arr + num);
   print(arr, num);
   string str[num] = { "tutorials point is best", "tutorials point", "www.tutorialspoint.com" };
   sort(str, str + num);
   print(str, num);
   float float_arr[num] = { 32.0, 12.76, 10.00 };
   sort(float_arr, float_arr+num);
   print(float_arr, num);
   return 0;
} 

Đầu ra

0   1   2   3 10   90
tutorials point   tutorials point is best   www.tutorialspoint.com
10   12.76   32