C++杂碎&敲过的代码

来源:互联网 发布:安卓云呼轰炸机软件 编辑:程序博客网 时间:2024/05/23 19:16

编译环境Eclipse C++

  • c++是在C语言的基础上增加了面向对象的思想。
  • 增加字符串数组以及字符串类型:
    char strs []= “i am icanactnow !”;
    string strs =”i am superman !”;

字符串指针

#include <iostream>using namespace std;int main() {    char *str = "s13un";//字符指针存放字符串    cout << str << endl;    return 0;}

字符串指针

#include <iostream>using namespace std;int main() {    char str1[] = "hello" ;    char str2[20], *p1 , *p2;    p1= str1 ;    p2= str2 ;    for(;*p1!='\0';p1++,p2++)        *p2 = *p1;        *p2 = '\0';    cout << str1<<str2 << endl;    return 0;}

函数指针

#include <iostream>using namespace std;int main() {//函数指针调用函数int max (int x, int y);//声明函数要写的向声明一样全//定义一个函数指针,注意体会格式,以及使用方式int (*p)(int x,int y);p= max;//将函数的首地址赋值给函数指针pint x = p(6,2);    cout << x << endl;    return 0;}int max (int x,int y){return  x>y? x:y;}

指针数组

#include <iostream>using namespace std;int main() {//指针数值//指针数组存放着数组元素的首地址 char *p[4]= {"sun1","yang","fan","!"}; char *p1; p1= p[0];//思考这块为什么不能写成 p1 = p; cout<< p1 <<endl;    return 0;}

验证指针数组存放的是元素的首地址

#include <iostream>using namespace std;int main() { char *p[4]= {"sun1","yang","fan","!"}; char *p1;// p1= p; cout<< ++p[0] <<endl; return 0;}//输出结果是:un1

指向指针的指针

// 仔细体会运行结果。#include <iostream>using namespace std;int main() { char **p1; char *p[4]= {"sun1","yang","fan","!"}; p1= p+2; cout<< p<<'\n'<<*p<<'\n'<<**p<<'\n' <<endl; return 0;}//运行结果 0x22ff20sun1s

const 指针

  • const 指针分三种 指向常量的指针/常指针/指向常量的常指针。
  • 分析:const离谁近谁就不变。
//指向常量的指针int a=12,b=15;const int *p = &a; //const离int近,所以常量不能修改。*p =15;//非法,指针为const类型不允许通过指针修改地址上的值。a = 15;//合法,const 指针并不能限制整形变量。要组织整形变量改变,则 const int a = 12;
//常指针char *const p1 = "icanactnow";//const离指针近所以指针不能修改。p1 = "error"; //由上面得,此语句不合法。
//指向常量的常指针int a= 6;int b= 7;const int * const p1 = &a;//都不能变。p1 = &b ;//非法*p1 = 8;//非法,试图通过指针修改常量a = 9;//合法,所有的const相关的只是通过指针相关的受影响。

引用

//简单示例int a = 6;int &b = a; //则b为a的引用,b为a的别名。
//错误示例int a[5];int &b = a;//不能建立数组引用int &b = a[1];//不能建立数组元素引用int &c = b;//不能建立引用的引用。int *p = b;//不能建立引用的指针。
  • 示例
#include <iostream>using namespace std;int main() {    //用引用实现两个数字的调换    void swap (int & ,int &); //函数声明    int a= 7 , b=8;    swap (a,b);    cout << a<<b<< endl; // prints !!!Hello World!!!    return 0;} void swap (int &c ,int &d){     int temp;     temp = c;     c= d;     d=temp; }

插播对指针输出字符串的理解

int i =12;int *p= &i;cout << *P << endl; //输出12 ,p 输出 地址。
char * p = "sun";cout << *p << endl;//输出s ,P输出sun.

解释:对于字符串指针存放的是首地址,要输出时首先判断如果是字符型的指针就将从首地址开始的字符依次输出知道’\0’结束。所以会出现如上所示情况。

new与delete

#include <iostream>using namespace std;struct student {    string name;    int number;};int main() {    student * p ;    p = new  student; //开辟一个类型为student类型的结构体存储空间,将返回的地址赋值给结构体指针。    p->name= "sun";//通过结构体指针给结构体成员赋值。    p->number = 01;    cout << p->name<< endl; // prints !!!Hello World!!!    delete  p;// 释放p的内存空间    return 0;}

typedef

  • 声明一个类型名来定义一个已存在的类型
typedef int HAHAHA;//用HAHAHA代替int类型
  • typedef用于结构体
typedef struct{  ...  ...} TYPE ;//TYPE student;//定义结构体变量TYPE *p;//定义了指向结构体的指针
0 0