回答編集履歴

2

ファンクタを使うコードを追記

2020/05/20 02:42

投稿

fana
fana

スコア11658

test CHANGED
@@ -81,3 +81,83 @@
81
81
  ```
82
82
 
83
83
  みたいなことでしょうか.
84
+
85
+
86
+
87
+ ---
88
+
89
+
90
+
91
+ 追記:
92
+
93
+ 処理速度に関しては,std::functionじゃなくて,比較処理者を具体的に用意して与えれば改善するのかも?
94
+
95
+ ```
96
+
97
+ //比較処理者
98
+
99
+ class Cmp
100
+
101
+ {
102
+
103
+ public:
104
+
105
+ Cmp() : m_Counter(0) {}
106
+
107
+ bool operator()( const int &lhs, const int &rhs ){ ++m_Counter; return lhs<rhs; }
108
+
109
+ int GetCounterVal() const { return m_Counter; }
110
+
111
+ void ResetCounterVal(){ m_Counter=0; }
112
+
113
+ private:
114
+
115
+ int m_Counter;
116
+
117
+ };
118
+
119
+
120
+
121
+ //
122
+
123
+ using Iter = std::vector<int>::iterator;
124
+
125
+ using CmpFun = Cmp&;//std::function<bool(const int &, const int &)>;
126
+
127
+
128
+
129
+ std::vector< void(*)( Iter,Iter,CmpFun ) > V
130
+
131
+ {
132
+
133
+ SelectionSort< Iter,CmpFun >,
134
+
135
+ InsertionSort< Iter,CmpFun >
136
+
137
+ };
138
+
139
+
140
+
141
+ std::vector<int> Unsorted{ 3,9,0,8,3,1 };
142
+
143
+ Cmp comparer; //比較処理者
144
+
145
+ for( auto f : V )
146
+
147
+ {
148
+
149
+ auto Vals = Unsorted;
150
+
151
+ comparer.ResetCounterVal();
152
+
153
+ f( Vals.begin(), Vals.end(), comparer );
154
+
155
+
156
+
157
+ for( auto v: Vals ){ std::cout << v << ", "; }
158
+
159
+ std::cout << "\ncounter=" << comparer.GetCounterVal() << std::endl;
160
+
161
+ }
162
+
163
+ ```

1

コード追記

2020/05/20 02:42

投稿

fana
fana

スコア11658

test CHANGED
@@ -54,6 +54,30 @@
54
54
 
55
55
  };
56
56
 
57
+
58
+
59
+ //Vを使ってみる
60
+
61
+ std::vector<int> Unsorted{ 3,9,0,8,3,1 };
62
+
63
+ for( auto f : V )
64
+
65
+ {
66
+
67
+ auto Vals = Unsorted;
68
+
69
+ int counter = 0;
70
+
71
+ f( Vals.begin(), Vals.end(), [&counter]( const int &lhs, const int &rhs )->bool{ ++counter; return lhs<rhs; } );
72
+
73
+
74
+
75
+ for( auto v: Vals ){ std::cout << v << ", "; }
76
+
77
+ std::cout << "\ncounter=" << counter << std::endl;
78
+
79
+ }
80
+
57
81
  ```
58
82
 
59
83
  みたいなことでしょうか.