c++sortクラスの少しの違い


STLのsort()アルゴリズムでは,データ量が大きい場合はQuick Sortを用い,セグメント再帰ソートを行い,セグメント化後のデータ量がある敷居よりも小さくなると,Quick Sortの再帰呼び出しに過大な負荷がかからないようにInsertion Sortを用いる.再帰階層が深すぎる場合は、Heap Sortも使用されます.STL sortアルゴリズム(以上の3つのアルゴリズムの統合)--Introspective Sorting(内省式ソート).
I)Sort関数はヘッダファイルが#includeのc++標準ライブラリに含まれています!II)Sort関数には3つのパラメータがある:(1)1つ目はソートする配列の先頭アドレスである.(2)2つ目は終了するアドレス(最後にソートするアドレス)(3)3つ目のパラメータはソートの方法であり、大きいものから小さいものまで、小さいものから大きいものまで、3つ目のパラメータを書かなくてもよい.このときのデフォルトのソート方法は小さいものから大きいものまでである.
STLにはsort関数があり,配列を直接並べ替えることができ,複雑度はn*log 2(n)である.
最初のバージョンは[first,last]を昇順ソートし、デフォルトオペレータが「」の場合はint、float、char、および構造体をソートする降順ソートを使用します.
sort_main関数
#include "stdafx.h"
#include
#include
#include
#include 
using namespace std;

bool compare(const string &a, const string &b){
	return a > b;
}
bool cmp(const int &a, const int &b){
	return a > b;
}
int _tmain(int argc, _TCHAR* argv[])
{
	vector res = { "ABC", "ACB", "BAC", "BCA", "CAB", "CBA"};
	vector B = { 1, 2, 3, 4, 5 };
	for (int i = 0; i < res.size(); i++){
		cout << res[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < B.size(); i++){
		cout << B[i] << " ";
	}
	cout << endl;
	sort(B.begin(), B.end(), cmp);
	sort(res.begin(), res.end(), compare);
	for (int i = 0; i < res.size(); i++){
		cout << res[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < B.size(); i++){
		cout << B[i] << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}


出力:
ABC ACB BAC BCA CAB CBA 1 2 3 4 5 CBA CAB BCA BAC ACB ABC 5 4 3 2 1
sort_class関数
class Test{
public:
	static bool compare(const string &a, const string &b){
		return a > b;
	}
	static bool cmp(const int &a, const int &b){
		return a > b;
	}
	void test(){
		vector res = { "ABC", "ACB", "BAC", "BCA", "CAB", "CBA" };
		vector B = { 1, 2, 3, 4, 5 };
		for (int i = 0; i < res.size(); i++){
			cout << res[i] << " ";
		}
		cout << endl;
		for (int i = 0; i < B.size(); i++){
			cout << B[i] << " ";
		}
		cout << endl;
		sort(B.begin(), B.end(), cmp);
		sort(res.begin(), res.end(), compare);
		for (int i = 0; i < res.size(); i++){
			cout << res[i] << " ";
		}
		cout << endl;
		for (int i = 0; i < B.size(); i++){
			cout << B[i] << " ";
		}
		cout << endl;
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	Test t;
	t.test();
	system("pause");
	return 0;
}

出力:
ABC ACB BAC BCA CAB CBA
1 2 3 4 5
CBA CAB BCA BAC ACB ABC
5 4 3 2 1
ここでmain関数とsort_class関数、少し違いがあります.
sort_main関数は
bool compare(const string &a, const string &b){return a > b;}
sort_class関数は
static bool compare(const string &a, const string &b){return a > b;}
sort_class関数はstaticを明記していないので、エラーが発生します(深く研究していないので、先に記録してください)
エラー4 error C 2780:「void std::sort(_RanIt,_RanIt)」:2つのパラメータを入力する必要がありますが、3つのパラメータが提供されます.
エラー3 error C 3867:「Test::compare」:関数呼び出し欠落パラメータリスト;「&Test::compare」を使用してメンバーへのポインタを作成してください
gccでコンパイル、sort_class関数にstaticが明記されていない場合もエラーが発生します
In file included from a.cc:2: ./solution.h:10:32: error: reference to non-static member function must be called sort(res.begin(), res.end(), compare); 1 error generated.
gccでコンパイル、sort_class関数はstaticを宣言しますが、パラメータがconstとして明記されていないとエラーが発生します.
static bool compare(string &a, string &b)){return a > b;}
In file included from a.cc:1:In file included from/home/web/newjudge/include/nc_tools.h:5:In file included from/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/algorithm:62:/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_algo.h:2287:28: error: binding of reference to type 'basic_string' to a value of type 'const basic_string' drops qualifierswhile (__comp(*__first, __pivot))~~~~~~/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_algo.h:2319:19: note: in instantiation of function template specialization 'std::__unguarded_partition<:__normal_iterator>> >, std::basic_string, bool (*)(std::basic_string &, std::basic_string &)>' requested here return std::__unguarded_partition(__first + 1, __last, *__first, __comp);/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_algo.h:2360:11: note: in instantiation of function template specialization 'std::__unguarded_partition_pivot<:__normal_iterator std::vector=""std::allocator="">> >, bool (*)(std::basic_string &, std::basic_string &)>' requested here std::__unguarded_partition_pivot(__first, __last, __comp);/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_algo.h:5512:9: note: in instantiation of function template specialization 'std::__introsort_loop<:__normal_iterator std::vector=""std::allocator="">> >, long, bool (*)(std::basic_string &, std::basic_string &)>' requested here std::__introsort_loop(__first, __last, ./solution.h:10:3: note: in instantiation of function template specialization 'std::sort<:__normal_iterator std::vector=""std::allocator="">> >, bool (*)(std::basic_string &, std::basic_string &)>' requested here sort(res.begin(), res.end(), compare); In file included from a.cc:1: In file included from/home/web/newjudge/include/nc_tools.h:5: In file included from/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/algorithm:62:/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_algo.h:2290:18: error: binding of reference to type 'basic_string' to a value of type 'const basic_string' drops qualifiers while (__comp(__pivot, *__last)) 2 errors generated.