C++ テンプレートテクニックの p.198 の CRTP の説明。Curiously Reccursive Template Pattern の略であると書いてあるが、Reccursive というのはスペルミスで、Recursive ではないだろうか。
この日記をshinh 度チェッカーに掛けたらすごい値が出るような気がしたので試してみた。
あなたの shinh 度は 125% です!
あなたは shinh そのものです。こうなってしまっては、もう救いの道はありません…
- あなたにかかっている shinh 疑惑
- void
- static
- template
- std
- (),
- 以下のような発言を心がけましょう
- 書籍
- 分から
- 実践
- 2009
- 購入
まあこの日記を書き込んだ時点で値が少し変わってしまうような気もしますが、とりあえず現時点で125%でした。重症目指して頑張ろうかと思います。
C++ 勉強する。
std::list でも動くようにとか、他にも色々。
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <list>
#include <vector>
template <typename Type,typename Iterator>
static void quicksort_recur(Iterator begin,Iterator end
,int (*compare)(Type,Type))
{
Iterator first2last = begin,last2first = end
,pivot_pos = begin,pivot_pos_ = begin;
Type pivot;
pivot_pos_++;
while(pivot_pos != end && !compare(*pivot_pos,*pivot_pos_)){
pivot_pos++;
pivot_pos_++;
}
if(pivot_pos == end){
return;
}
pivot = (0 > compare(*pivot_pos,*pivot_pos_))?*pivot_pos_
:*pivot_pos;
while(first2last != last2first){
while(0 < compare(pivot,*first2last)){
first2last++;
}
while(0 >= compare(pivot,*last2first) && first2last != last2first){
last2first--;
}
std::iter_swap(first2last,last2first);
}
quicksort_recur(begin,--last2first,compare);
quicksort_recur(first2last,end,compare);
}
template <typename Type,typename Container>
void quicksort(Container &array,int (*compare)(Type,Type))
{
quicksort_recur(array.begin(),--array.end(),compare);
}
template <typename Container>
void print_container(Container container)
{
typename Container::iterator iter = container.begin();
while(iter != container.end()){
std::cout << *iter;
iter++;
if(iter != container.end()){
std::cout << ",";
}
}
std::cout << std::endl;
}
template <typename Container>
void add_random_elements(Container &container,size_t elements)
{
while(elements--){
container.push_back(rand()%64);
}
}
int compare_integer(int a,int b)
{
return a-b;
}
int main(void)
{
std::vector<int> array;
std::list<int> list;
srand((unsigned)time(NULL));
add_random_elements(array,32);
add_random_elements(list,32);
std::cout << "Before" << std::endl;
print_container(array);
print_container(list);
quicksort(array,compare_integer);
quicksort(list,compare_integer);
std::cout << "After" << std::endl;
print_container(array);
print_container(list);
return 0;
}
まだ良く分からないのでとりあえずクイックソートを書いてみた。テンプレート使用。
#include <iostream>
#include <vector>
template <typename Type,typename Iterator>
static void quicksort_recur(Iterator begin,Iterator end
,int (*compare)(Type,Type))
{
Iterator first2last = begin,last2first = end
,pivot_pos = begin,pivot_pos_ = begin;
Type pivot;
pivot_pos_++;
while(pivot_pos != end && !compare(*pivot_pos,*pivot_pos_)){
pivot_pos++;
pivot_pos_++;
}
if(pivot_pos == end){
return;
}
pivot = (0 > compare(*pivot_pos,*pivot_pos_))?*pivot_pos_
:*pivot_pos;
while(1){
while(0 < compare(pivot,*first2last)){
first2last++;
}
while(0 >= compare(pivot,*last2first)
&& first2last != last2first){
last2first--;
}
if(first2last == last2first){
last2first--;
break;
}
std::iter_swap(first2last,last2first);
}
quicksort_recur(begin,last2first,compare);
quicksort_recur(first2last,end,compare);
}
template <typename Type,typename Container>
void quicksort(Container &array,int (*compare)(Type,Type))
{
quicksort_recur
(array.begin(),array.end()-1,compare);
}
template <typename Vector>
void print_vector(Vector array)
{
typename Vector::iterator iter = array.begin();
while(1){
std::cout << *iter;
iter++;
if(iter != array.end()){
std::cout << ",";
}
else{
break;
}
}
std::cout << std::endl;
}
int compare_integer(int a,int b)
{
return a-b;
}
int main(void)
{
std::vector<int> array;
array.push_back(16);
array.push_back(5);
array.push_back(76);
array.push_back(23);
array.push_back(64);
array.push_back(24);
array.push_back(43);
array.push_back(7);
std::cout << "Before : ";
print_vector(array);
quicksort(array,compare_integer);
std::cout << "After : ";
print_vector(array);
return 0;
}