美文网首页
[陈宗权C++]C++第1天AM--C++基础_C++风格字符串

[陈宗权C++]C++第1天AM--C++基础_C++风格字符串

作者: Optimization | 来源:发表于2020-01-27 18:24 被阅读0次

    参考:

    重点:

    1.常量引用

    ////达内C++教程\03_标准C++编程_陈宗权_7day\标准C++编程_day01AM_C++基础_C++风格字符串_名称空间_引用_TEST1
    //// cin/cout是对象,不是关键字
    //
    //#include<iostream>
    //#include<string>
    ////自己的程序放在自己的名字空间中,可以是张三家的二狗,也可以是李四家的二狗
    //using namespace std;
    //// using 谁里面的什么东西A::B:表示A范围内的B,“::”:称为域操符
    ////using std::cout;
    ////using std::string;
    ////using std::endl;
    //
    //namespace zgx {
    //  string name = "郑国贤";
    //  int age = 27;
    //}
    //
    //namespace wlj {
    //  char name[20] = "翁丽金";
    //  double salary = 10000;
    //}
    //
    //using namespace zgx;
    //using namespace wlj;
    //char name[20] = "ZZZ";
    //int main()
    //{
    //  //std::cout <<"我喜欢"<<name << std::endl;//歧义
    //  std::cout << "请输入你的姓名和年龄: \n";
    //  string name;
    //  int age;
    //  cin >> name >> age;
    //  cout << name << " 你好,你出生于" << 2020 - age << "年" << std::endl;
    //
    //  std::cout << "我是"<<zgx::name<<",年龄: "<<zgx::age<< std::endl;
    //  std::cout << "我是" << wlj::name <<",月薪:"<<wlj::salary<< std::endl;
    //
    //  name = "zzz";
    //  std::cout << name << std::endl;
    //  std::cout << ::name << std::endl;//没放名称空间中的东西,默认放在匿名名称空间中
    //  system("pause");
    //  //return 0;//main函数末尾的return 0;可有可无
    //}
    
    
    
    //达内C++教程\03_标准C++编程_陈宗权_7day\标准C++编程_day01AM_C++基础_C++风格字符串_名称空间_引用_TEST2
    #include<iostream>
    using namespace std;
    
    enum Course{UNIX,C,CPP,UC,VC};
    struct Student {
        string name;
        Course co;
    };
    
    enum Color {BLACK,RED,GREEN,YELLOW,BLUE,WHITE};
    
    
    int main() {
    
        //C风格字符串,赋值的话,strcpy(s1,...);连接strcat(s1,...);长度:strlen;查找:strchr,strstr;访问元素s2[i]
        char s1[100];
        //C++风格字符串,赋值= ;连接:+=;长度,s2.size(),s2.length();查找;s2.find();访问元素s2[i]
        string s2;
        //两者之间的沟通
        //C风格字符串==>C++风格字符串  |自动转
        //C++风格字符串==>C风格字符串s2.c_str(),类型是const char* 
        
        Course c;
        Student s;
        int n;
        c = Course::CPP;
        n = Course::CPP;
    
        Color clr = BLUE;
    
        ////////////////////////////////////////////////////////
        bool gender = true;
        bool sex = false;
        std::cout <<(gender?"帅哥":"meinv") << std::endl;
        std::cout << (sex ? "帅哥" : "meinv") << std::endl;
        // bool alpha:设置输出的为true or false
        std::cout << boolalpha << gender << "," << sex << std::endl;
    
    
        // 引用,给变量另起一个名字
        double d = 123.45;
        double& e = d;//e是d的别名,两者是同一个变量
        //double& f = 123.4;//
        const double&f = 123.4;//引用常量,必须加const
        const double&g = d + 5;
        std::cout << "&d:"<<&d << ",&e:" << &e << std::endl;
    
        //int&n = d;//类型不一致,不可以
        double&e2 = e;
        std::cout << "&e:" << &e << ",&e2:" << &e2 << std::endl;
        system("pause");
    
    }
    

    相关文章

      网友评论

          本文标题:[陈宗权C++]C++第1天AM--C++基础_C++风格字符串

          本文链接:https://www.haomeiwen.com/subject/iruethtx.html