尝试编译此代码时遇到问题:
#include <iostream>
#include <functional>
using namespace std;
template <typename T>
size_t partition(T arr[], size_t size, function<bool(T)> lambda) {
int index = 0;
for(int i = 0; i < size; i++) {
if(fun(arr[i]) == true) {
//jeśli element spełnia predykat zamien go z arr[index] i ustaw index na 'i'
int tmp = arr[index];
arr[index] = arr[i];
arr[i] = tmp;
index = i;
}
else { //na początku gdy index = 0, powiększ
if(index == 0)
index = i;
}
}
return index;
}
template <typename T>
void printTable(T arr[], size_t size) {
cout << "[ ";
for(int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << "] ";
}
template <typename T>
bool less10(T a) {
return a < 10;
}
int main() {
int arri[] = {1,20,3,50,6,7};
size_t sizi = sizeof(arri)/sizeof(arri[0]);
printTable(arri, sizi);
size_t fi = partition(arri,sizi,
(function<bool(int)>)less10<int>);
printTable(arri,sizi);
cout << "index: " << fi << endl;
cout << endl;
double arrd[] = {1,20,3,50,6,7};
size_t sizd = sizeof(arrd)/sizeof(arrd[0]);
printTable(arrd, sizd);
function<bool(double)> lambda = [] (double x) -> bool {return x > 10;};
size_t fd = partition(arrd,sizd,lambda);
printTable(arrd,sizd);
cout << "index: " << fd << endl;
}
这里有错误输出:
6 C:\Documents and Settings\Grzegorz\Pulpit\Funkcje.cpp `function' has not been declared
6 C:\Documents and Settings\Grzegorz\Pulpit\Funkcje.cpp expected `,' or `...' before '<' token
6 C:\Documents and Settings\Grzegorz\Pulpit\Funkcje.cpp ISO C++ forbids declaration of `parameter' with no type
C:\Documents and Settings\Grzegorz\Pulpit\Funkcje.cpp In function `int main()':
43 C:\Documents and Settings\Grzegorz\Pulpit\Funkcje.cpp `function' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
43 C:\Documents and Settings\Grzegorz\Pulpit\Funkcje.cpp expected primary-expression before "bool"
43 C:\Documents and Settings\Grzegorz\Pulpit\Funkcje.cpp expected `)' before "bool"
50 C:\Documents and Settings\Grzegorz\Pulpit\Funkcje.cpp expected primary-expression before "bool"
50 C:\Documents and Settings\Grzegorz\Pulpit\Funkcje.cpp expected `;' before "bool"
51 C:\Documents and Settings\Grzegorz\Pulpit\Funkcje.cpp `lambda' undeclared (first use this function)