现在的位置: 首页 > 综合 > 正文

C++值传递、引用传递、指针传递以及STL模板堆的使用

2013年08月23日 ⁄ 综合 ⁄ 共 915字 ⁄ 字号 评论关闭

1、C++中数组作为实际参数传递的三种形式参数方法

 

#include<iostream>
using namespace std;

void change(int& a,int b){   //通过引用,引用是原变量的别名,变量值返回时会受影响
 
 a=5;
}

void change1(int* a,int b){   //通过指针进行传递,变量值返回时会受影响
 
 *a=0;
}

void change2(int a,int b) {   //变量值返回时不会受影响
 
 a=2;
}

int main(){
 
 int c=6,d=4;
 change(c,d);     //直接传递变量
 cout<<c<<endl;
 
 change1(&c,d);     //传递变量地址
 cout<<c<<endl;
 
 change2(c,d);     //传递变量
 cout<<c<<endl;
 
 return 0;
}

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

2、C++中的优先队列,默认是大堆,自定义为小堆,注意:如何重载opertor<函数。

 

/* 在struct结构中重载operator< 函数
#include<iostream>
#include<string>
#include<queue>
using namespace std;

struct ss{
 string name;
 double score;

 

//自定义struct元素类型的operator<重载函数,使得优先队列中,将小元素放在队列的首部
//返回true的排在后面
 friend bool operator<( const ss& a, const ss& b){    return a.score> b.score ;
 }

};

int main(){
 
 struct ss stu[]={{"suting",120.0},{"xiaowang",123.0},{"xiaoli",110}};
 priority_queue<ss> q;

 for(int i=0; i< sizeof stu/sizeof stu[0] ; i++)
  q.push(stu[i]);

 cout<<q.top().name<<endl;
 
}
*/

抱歉!评论已关闭.