Created
November 28, 2010 17:28
-
-
Save CrBoy/719111 to your computer and use it in GitHub Desktop.
For the first problem by pcyu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: p1 p2 | |
p1: p1.cpp | |
g++ -o p1 p1.cpp | |
p2: p2.cpp | |
g++ -o p2 p2.cpp | |
clean: | |
rm -f p1 p2 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <iterator> | |
using namespace std; | |
bool reverse_int(int a, int b){ | |
return a>b; | |
} | |
class XXX{ | |
private: | |
int *array; | |
int size; | |
public: | |
XXX(int n){ | |
size = n; | |
array = new int[n]; | |
for(int i=0;i<n;i++) | |
array[i] = i; | |
} | |
~XXX(){ | |
free(array); | |
} | |
void sorting(){ | |
// 這邊要怎麼用 qsort / STL algorithm sort 來寫 | |
sort(array, array+size, reverse_int); | |
} | |
void print(){ | |
copy(array, array+size, ostream_iterator<int>(cout, " ")); | |
cout << endl; | |
} | |
}; | |
int main(int argc, const char *argv[]) | |
{ | |
XXX xxx(10); | |
xxx.print(); | |
xxx.sorting(); | |
xxx.print(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <iterator> | |
using namespace std; | |
class Comparer{ | |
int *target; | |
public: | |
Comparer(int *d){this->target = d;} | |
bool operator()(int a, int b){ | |
return target[a] > target[b]; | |
} | |
}; | |
class A{ | |
private: | |
int *data; | |
int *index; | |
int size; | |
public: | |
A(int n){ | |
size = n; | |
data = new int[n]; | |
index = new int[n]; | |
for(int i=0;i<n;i++){ | |
data[i] = i; | |
index[i] = i; | |
} | |
} | |
~A(){ | |
free(data); | |
free(index); | |
} | |
void sorting(){ | |
sort(index, index+size, Comparer(data)); | |
} | |
void print(){ | |
cout << "data: "; | |
copy(data, data+size, ostream_iterator<int>(cout, " ")); | |
cout << endl; | |
cout << "index: "; | |
copy(index, index+size, ostream_iterator<int>(cout, " ")); | |
cout << endl; | |
} | |
}; | |
int main(int argc, const char *argv[]) | |
{ | |
A a(10); | |
a.print(); | |
a.sorting(); | |
a.print(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment