#include
#include
#include
using namespace std;
template
bool compare(Elem lhs, Elem rhs){
if (lhs<=rhs) return false;
else return true;
}
//template
//typedef bool(*comparefunc)(Elem lhs, Elem rhs); 不能利用模板来重定义类型。
template
void bubblesort(Elem* arr, int n, bool(*func)(Elem lhs, Elem rhs) = compare)
//下面这个语句中相当于对函数指针起了两个名字,应该是上面这样。
//实际上函数指针的类型表示是bool (*)(Elem ,Elem),func已经是函数指针名了。
//void bubblesort(Elem* arr, int n, bool(*func)(Elem lhs, Elem rhs)
comparefunc= compare)
{
Elem temp;
for (int i = 0; i < n-1; i++){ //一要保证数组不越界,二要保证保证对所有数据进行排序。
for (int j = i + 1; j < n ; j++){
if (arr[j] < arr[i]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
class Credit{
public:
Credit():value(0){ //利用初始化参数列表来初始化字段。
}
Credit(int value){
this->value = value;
}
int getValue(){
return this->value;
}
void setValue(int value){ //setValue表示的是一个赋值过程,并不要求将结果返回。所以返回值类型为void类型。
this->value = value;
}
bool operator
return (this->value < other.getValue()) ? true : false;
}
bool operator==(Credit& other){
return (this->value == other.getValue()) ? true : false;
}
bool operator<=(Credit& other){
//在进行运算符重载时,定义的重载函数的返回值等信息要与原运算符的本质保持一致,这里应该返回的是布尔类型。
//下面的语句提示Credit没有value成员。是因为在类中只能访问当前对象(用this指针引用的对象的private数据成员,对于与当前类同类型的其他实例在本类的成员函数中无法直接访问其private数据成员。所以说在当前类本身中可以访问类的private数据成员,只是针对类的当前实例,而不是该类型的所有实例。 //return (this->value <=other.value) ? true : false;
return (this->value <= other.getValue()) ? true : false; //也可以写成 return (this
//当类本身的比较复杂时,这样写可充分利用已有代码,并且当其他运算符要进行变动时,这里一般不需要改动。
}
private:
int value;
};
int main(){
int arr[10] = { 3, 5, 7, 11, 78, 34, 67, 45, 23, 4 };
bubblesort(arr, 10);
for (int num : arr){
cout << num << " ";
}
Credit creditSet[23];
srand(time(NULL)); //time是个内联函数,用static修饰,参数和返回值都是一个64位的整数,但是srand的参数是unsigned int。
for (int i = 0; i < 23; i++){ //注意这里的23可不能用sizeof(creditSet/sizeof(Credit))来表示,因为有内存对齐的影响。
creditSet[i].setValue(rand() % 100);
}
cout << endl;
bubblesort(creditSet, 23);
for (int i = 0; i < 23; i++){ //注意这里的23可不能用sizeof(creditSet/sizeof(Credit))来表示,因为有内存对齐的影响。
cout<
}
return 0;
}
一些参考资料
C/C++怎样产生随机数:这里要用到的是rand()函数, srand()函数,C语言/C++里没有自带的random(int number)函数。
(1) 如果你只要产生随机数而不需要设定范围的话,你只要用rand()就可以了:rand()会返回一随机数值, 范围在0至RAND_MAX 间。RAND_MAX定义在stdlib.h, 其值为2147483647。(注:是否包括区间端点????)
(2) 如果你要随机生成一个在一定范围的数,你可以在宏定义中定义一个random(int number)函数,然后在main()里面直接调用random()函数:
例如:随机生成10个0~100的数:
#include
#include
#define random(x) (rand()%x)void main()
{
for(int x=0;x<10;x++)
printf("%d/n",random(100));
}
(3)但是上面两个例子所生成的随机数都只能是一次性的,如果你第二次运行的时候输出结果仍和第一次一样。这与srand()函数有关。srand()用来设置rand()产生随机数时的随机数种子。在调用rand()函数产生随机数前,必须先利用srand()设好随机数种子(seed), 如果未设随机数种子, rand()在调用时会自动设随机数种子为1。上面的两个例子就是因为没有设置随机数种子,每次随机数种子都自动设成相同值1 ,进而导致rand()所产生的随机数值都一样。
srand()函数定义 : void srand (unsigned int seed);
通常可以利用geypid()或time(0)的返回值来当做seed如果你用time(0)的话,要加入头文件#include
在stdlib.h 中这两个函数的原型是:
int rand();
void srand (unsigned int);
扩充:
x = rand()%11; /*产生1~10之间的随机整数*/
y = rand()%51 - 25; /*产生-25 ~ 25之间的随机整数*/
注:根据要求的两个数据之间的差值确定rand函数后面取余多少。比如产生【a,b]的数,则取余的数是b-a,若a=-7,b=-2,取余后一共有b-a+1种值,则取余的数为b-a+1,结果在(0,b-a)之间,共b-a+1种值。
取余后的值中0对应区间中的小值,所以delta=a-0
z = ((double)rand()/RAND_MAX)*(b-a) + a;/*产生区间[a,b]上的随机数*/