一些复习C++的小笔记
要点如下:
1)如果在函数中需要对实体进行改变,并且影响返回后的结果,需要加一个&的符号。加就完事了。
2) 如果不用&,那么也需要用*,此时就需要输入的参数加一个&。有一丢的麻烦。
// An highlighted block
#include <iostream>
#include <vector>
using namespace std;
typedef struct stu
{
string name;
string county;
int score;
double gpa;
};
stu* add(string name, string county, int score, double gpa)
{
stu *stu_ptr = new stu;
stu_ptr->name = name;
stu_ptr->county = county;
stu_ptr->score = score;
stu_ptr->gpa = gpa;
return stu_ptr;
}
void swap(vector<stu*> &array, int x, int y)
{
stu* temp;
temp = array[x];
array[x] = array[y];
array[y] = temp;
}
bool compare(stu* x, stu* y)
{
return x->score <= y->score ? true:false;
}
void sort(vector<stu*> &array)
{
for(int i = array.size() - 1;i >= 0;i--)
{
for(int j = 0;j < i;j++)
{
if(compare(array[j],array[j+1]) == false)
{
swap(array,j,j+1);
}
}
}
}
int main()
{
vector<stu*> stu_array;
stu_array.push_back(add("Peter","China",98,3.45));
stu_array.push_back(add("May","China",95,3.4));
stu_array.push_back(add("Jimmy","China",90,3.15));
sort(stu_array);
cout<<stu_array[2]->score;
system("pause");
return 0;
}