![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++
beck23ham
这个作者很懒,什么都没留下…
展开
-
C++加号运算符重载-全局函数、成员函数实现
#include using namespace std;class Complex{public: Complex(double r=0.0,double i=0.0):m_r(r),m_i(i){} //构造函数 void print(void) { cout<<'('<<m_r<<'+'<<m_i<<"i)"<<endl; } //方式二:成员函数,优先选择成员函数原创 2018-01-02 01:00:05 · 1462 阅读 · 0 评论 -
静态类型转换
#include #include using namespace std;int main(void){ double f=3.14; int n=static_cast(f); cout<<n<<endl; char* p="hello"; //n=static_cast(p); //编译报错 // n=(int)p; //不报错,潜在风险 p=static_cast(原创 2018-01-29 07:25:29 · 616 阅读 · 0 评论 -
引用的应用
#include using namespace std;void swap1(int a,int b){ int c=a; a=b; b=c;}void swap2(int* a,int* b){ int c=*a; *a=*b; *b=c;}void swap3(int& a,int& b){ int c=a; a=b; b=c;}void swap4原创 2018-01-14 23:44:36 · 156 阅读 · 0 评论 -
引用
#include using namespace std;void foo(int& r){ r=20;}void bar(int* n){ *n=20;}void bar1(int m){ m=20;}int& fun(int& r){ return r;}int main(void) { int n=10; int& r=n; //初始化 r=20原创 2018-01-14 21:21:10 · 121 阅读 · 0 评论 -
动态内存分配
#include #include using namespace std;int main(void) { int* p1=(int*)malloc(sizeof(int)); *p1=123; cout<<*p1<<endl; free(p1); p1=new int(123); cout<<*p1<<endl; delete p1; p1=new int[5]原创 2018-01-14 20:48:31 · 94 阅读 · 0 评论 -
内联函数
#include using namespace std;#define SQUARE(x) (x*x) //预编译时替换 (替换文本) inline int square(int x) //编译时替换(替换二进制文件) { return x*x;}int main(void){ cout<<SQUARE(10)<<endl; cout<<SQUARE(3+7)<<endl;原创 2018-01-14 20:27:07 · 184 阅读 · 0 评论 -
缺省参数
#include using namespace std;//缺省参数只能用在函数的声明中void bar(int n,double f=1.2,char c='A'); //哑元 void func(int) { cout<<"func(int)"<<endl;}void func(char) { cout<<"func(char)"<<endl;} int main(原创 2018-01-14 19:39:06 · 227 阅读 · 0 评论 -
重载
#include using namespace std;int add(int x,int y) { cout<<"add(int,int)"<<endl; return x+y;}double add(double x,double y){ cout<<"add(double,double)"<<endl; return x+y;}double add(double x原创 2018-01-14 17:30:38 · 163 阅读 · 0 评论 -
c++结构体和联合
#include using namespace std;struct Student //c++的结构体里可以定义函数,c不可以,相当于类 { char name[128]; int age; void print(void) { cout<<name<<","<<age<<endl; }}; int main(void){ struct Student s={"原创 2018-01-14 12:09:34 · 265 阅读 · 0 评论 -
C++插入排序
二、插入排序1 算法 1) 从第一个元素开始,该元素可以认为已经被排好序; 2) 取出下一个元素,在已经排序的元素序列中从后向前扫描; 3) 若该元素大于取出的元素,则将该元素移(覆盖)到下一位置; 4) 若该元素小于等于取出的元素,则将取出的元素插入到该元素之后; 5) 重复步骤2,直至处理完所有的...原创 2018-06-24 16:53:49 · 2393 阅读 · 0 评论