Problem

1/1

パターン: スタート

Theory Click to read/hide

STL は C++ テンプレート クラスのセットであるため、STL を操作するにはこれらのクラスがどのように構造化されているかを知っておくことが望ましいです。
C++ では、テンプレートをサポートするために 2 つの新しいキーワードが追加されました。そして「タイプ名」。これらを使用すると、コンパイル時に必要な型に展開されるジェネリック関数を作成できます。たとえば、次の 2 つの値の最大値を取得するテンプレート関数は次のとおりです。

テンプレート <タイプ名 T>
T myMax(T x, T y)
{
   戻る (x > y)? x: y;
}
  
int main()
{
  cout << myMax<int >(3,  7) << endl;
  cout << myMax<double >(3.0,  7.0) << endl;
  cout << myMax<char >('g', 'e') << endl;
  
  戻り0;
}


 

Problem

バブルソートを実装するテンプレート関数を作成します。
 
<頭> <本体>
# 入力 出力
1 5
5 4 3 2 1
1 2 3 4 5
Write the program below
#include <iostream> 
using namespace std;         
int main() { 
   int n;
   cin >> n;
    int *a = new int[n];
    for (int i = 0; i < n; i++) 
        cin >> a[i]; 
    bubbleSort(a, n); 
    for (int i = 0; i < n; i++) 
        cout << a[i] << " "; 
    cout << endl; 
    return 0; 
}         

     

Program check result

To check the solution of the problem, you need to register or log in!