第九天学C++总结

  学c++看的是《c++primer plus》这本书,感觉没有一条主线,学了也不知道这些能干吗。

  第一天看来cin,cout和一些基本函数,如rand(),sleep(),math();

int first_demo;
cin>>first_demo;
cout<<"hello world"<<endl;

  第二天看了数据类型,其中包括数据类型的最大最小值,测量方法(sizeof),以及int型中,多种进制的转换。转义序列\a....

//类型大小
int i_max = INT_MAX;
long l_max = LONG_MAX;
short s_max = SHORT_MAX;
char c_max = CHAR_MAX;

//转义序列
cout<<"Hello \" world!"<<endl;

  第三天字符串,其中对cin>>str和getline,get看了很久,解决了前者不能录入空格的问题。然后是数组的拼接和复制、指针的new、枚举、结构体、指针加法、共用体。

//录入带空格的字符串
    string str;
    //1、cin elliot MN
    cin>>str;
    //2、getline
    getline(cin,str);
    //3、get不可以


//指针的new
    int *p = new int;

//结构体
    struct demo{
        int i;
        string s;
        char c;
    }
    demo first_demo = {1,"2",c};
//共用体
    union demo{
        int a;
        double b;
    };
    demo n = {1};
    cout<<n.a;

//枚举
    enum demo{red=4,yellwo,blue,white};
    cout<<demo(red);//结果为4,和python的字典有点像,但没字典好用




  第四天看的很少,已经定义的数组不能赋值到现在还是没头绪。

//正确赋值
char n[10];
for(int i = 70;i<80;i++){
        n[i-70] = char(i);
        cout<<char(i);
    }
//错误赋值
n = "Hello World!";

  第五天学了cin.fail,EOF,cin.put,学的很少。

int a;
    if(cin>>a){
      cout<<"True"<<endl;
    }
    else{
        cout<<"False"<<endl;
    }

  第六条学cctype、switch、break、continue、file、函数参数传递指针,其中cctype是类似islower之类的函数,file重要的是定义ostream和fstream,其他的和cin、cout差不多形式。

    #include <fstream>
    //向a.txt写入Hello World
    ofstream writeFile;
    writeFile.open("F:/Null/a.txt");
    writeFile<<"Hello World!";
    

    //从a.txt读取Hello World,但读不到空格
    string x;
    ifstream readFile;
    readFile.open("F:/Null/a.txt");
    while(readFile.good()){
        readFile>>x;
        cout<<x;
    }

  第七天学了函数,除了基本类型的函数还有结构体函数、指针函数、二维数组函数(只能规定列)、函数调用函数、const限定符。

//结构体函数,按值传递
struct demo{
    int i;
    char c;
};

demo first_demo(demo d);
int main()
{
    demo f_demo;
    f_demo=first_demo(f_demo);
    cout<<f_demo.c<<endl;
    cout<<f_demo.i;
    return 0;
}

demo first_demo(demo d){
    d.c = 'a';
    d.i = 1;
    return d;
}    
//结构体数组,按地址传递--指针的方法,更改main函数内结构体的值
struct demo{
        int i;
        char c;
   };
void first_demo(demo *d);
int main()
{
    demo f_demo;
   first_demo(&f_demo);
    cout<<f_demo.c<<endl;
    cout<<f_demo.i;
    return 0;
}
void first_demo(demo *d){
    d->c = 'c';
    d->i = 5;
}
//用指针调用函数
int add_fun(int a,int b,int (*p)(int,int));
int add_primer(int a,int b);

int main()
{
    int x =10;
    int y =20;
    int total;
    total = add_fun(10,20,add_primer);
    cout<<"total:"<<total<<endl;
    return 0;
}

int add_fun(int a,int b,int (*p)(int,int))
{
    return (*p)(a,b);
}
int add_primer(int a,int b)
{
    return a+b;
}
//二维数组的函数传递
void show_array(int arr[][5],int n);

int main(){
int n =4;
int a[n][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}};
    show_array(a,n);
    return 0;
}

void show_array(int arr[][5],int n){
    for(int i = 0 ;i<n;i++){
            cout<<"\n\n这是第"<<i<<"行"<<endl;
        for(int j = 0;j<5;j++){
            cout<<"arr["<<i<<"]["<<j<<"]:"<<arr[i][j]<<"\t";
        }
    }
}

第八第九天学了内联函数、引用传递、结构体的引用传递。

//内联函数:代码量较少的时候使用,是代码的替换而非代码的调用
inline function(){};

//引用传递,只能在定义的时候引用
int a=100;
int & b = a;

总的来说九天感觉没有看到什么东西,自己特别想学的是用c++涉及到网上数据的处理,想proxyee下载磁力链接那样,但是毫无头绪。不知道自己学的和这些有什么关系。

猜你喜欢

转载自blog.csdn.net/qq_41375702/article/details/87442397