C++のsort関数の使い方

30596 ワード

C++のsort関数の使い方
  • 紹介
  • 最も簡単なソート
  • sortによる降順ソート
  • 独自のgreaterを使用してソート
  • 手書きcmp関数
  • cmp関数ステップ
  • 構造体を並べ替える
  • .
  • 文字をソート
  • 降順
  • 昇順
  • 文字列をソート
  • 紹介する
    sortで配列をソートするには、次のヘッダファイルが必要です.
    #include 
    

    sortはデフォルトで配列を昇順ソートし、カスタム関数で複数の機能ソートを実現したり、文字列をソートしたりすることができます.
    最も簡単なソート
    a[]={5,4,3,2,1}を昇順に並べ替える
    #include 
    #include 
    #include 
    using namespace std;
    int a[] = {5, 4, 3, 2, 1};
    int main()
    {
    	sort(a, a + 5);
    	for (int i = 0; i <= 4; i++)
    	{
    		cout << a[i] << " ";
    	}
    	cout << endl;
    	return 0;
    }
     
    

    a[0]からa[4]を並べ替えたい場合は、最長の配列を4とし、sort(a, a + 5);、つまりNに並ぶようにsort(a, a + N + 1);と書く必要があります.
    sortによる降順ソート
    実装には2つの方法がありますが、まず1つ目を紹介します.
    セルフgreaterでソート
    #include 
    #include 
    #include 
    using namespace std;
    int a[] = {1, 2, 4, 5, 3};
    int main()
    {
    	sort(a, a + 5, greater<int>());
    	for (int i = 0; i <= 4; i++)
    	{
    		cout << a[i] << " ";
    	}
    	cout << endl;
    	return 0;
    }
     
    
    greater()ここの()は省けませんが、は対応する配列のタイプのようですか?初学はまず暗記してからにしましょう.まだlessリットルの順位があるようですが、個人的にはこれは書いても書いても書いても同じではないかと思います.知りたいなら自分で調べてみましょう.
    手書きcmp関数
    bool cmp(int x, int y)
    {
    	return x > y;
     } 
    

    ここでどのように並べ替えてcmp関数の中のパラメータxを覚えることができて、yは以下のx > yに対応して、大きいから小さいまで、もしx < yに変えたら小さいから大きいまでです.注意>= <=と書いてはいけません.等しい番号は現れません.原因は不明です.
    //    
    #include 
    #include 
    #include 
    using namespace std;
    int a[] = {1, 2, 4, 5, 3};
    bool cmp(int x, int y)
    {
    	return x > y;
     } 
    int main()
    {
    	sort(a, a + 5, cmp);
    	for (int i = 0; i <= 4; i++)
    	{
    		cout << a[i] << " ";
    	}
    	cout << endl;
    	return 0;
    }
     
    

    cmp関数のステップアップ
    %7の余数で降順に並べ替えたい場合は?
    bool cmp(int x, int y)
    {
    	if(x % 7 != y % 7)
    	{
    		return x % 7 > y % 7;
    	}
    	else
    	{
    		return x > y;
    	}
     } 
    

    説明します:cmp関数に等号が現れないので、2つの数x、yの余数が同じようにどのように値を返しますか?答えはxyにしてもいいはずなので、ここでは自分で何度もやってみれば理解できます.
    #include 
    #include 
    #include 
    using namespace std;
    int a[] = {14, 24, 44, 54, 16};//          ,    
    bool cmp(int x, int y)
    {
    	if(x % 7 != y % 7)
    	{
    		return x % 7 > y % 7;
    	}
    	else
    	{
    		return x > y;
    	}
     } 
    int main()
    {
    	sort(a, a + 5, cmp);
    	for (int i = 0; i <= 4; i++)
    	{
    		cout << a[i] << " ";
    	}
    	cout << endl;
    	return 0;
    }
     
    

    構造体のソート
    説明:5人の名前と成績を入力し、成績の大きさで降順に並べ替えます.
    #include 
    #include 
    #include 
    using namespace std;
    struct stu
    {
    	int score;
    	char name[20];
    };
    bool cmp(stu x, stu y)
    {
    	if (x.score != y.score)
    	{
    		return x.score > y.score;
    	}
    	else
    	{
    		return x.score > y.score;
    	}
    }
    int main()
    {
    	stu a[10];
    	for (int i = 1; i <= 5; i++)
    	{
    		cin >> a[i].name >> a[i].score;
    	}
    	sort (a + 1, a + 5 + 1, cmp);
    	for (int i = 1; i <= 5; i++)
    	{
    		cout << a[i].name << ":" << a[i].score << endl;
    	}
    	return 0;
    }
     
    

    テストデータzhangsan 80 lisi 89 wangwu 85 zhaoliu 89 tianqi 100
    出力結果
    tianqi:100 zhaoliu:89 lisi:89 wangwu:85 zhangsan:80
    文字のソート
    エラープレゼンテーション
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    char c[] = "bacde";
    int main()
    {
    	sort(c, c + strlen(c));
    	for (int i = 0; i < strlen(c); i++)
    	{
    		cout << c[i] << " ";
    	}
    	return 0;
    }
    

    出力は何もありません(スペースかもしれません)
    正しいデモ
    降順
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    char c[] = "bacde";
    int main()
    {
    	sort(c, c + strlen(c), greater<char>());
    	for (int i = 0; i < strlen(c); i++)
    	{
    		cout << c[i] << " ";
    	}
    	return 0;
    }
    

    出力結果e d c b a
    昇順
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    char c[] = "bacde";
    int main()
    {
    	sort(c, c + strlen(c), less<char>());
    	for (int i = 0; i < strlen(c); i++)
    	{
    		cout << c[i] << " ";
    	}
    	return 0;
    }
     
    

    出力結果a b c d e
    文字列のソート
    あとで補充しましょう