zoukankan      html  css  js  c++  java
  • c++名字空间,C与C++字符串的区别,枚举类型

    1:命名空间
    2:C与C++字符串的区别和基本操作
    3:枚举类型
    命名空间

    #include <string> #include <ctype.h> #include <vector> #include <iostream> #include <fstream> // using declarations states our intent to use these names from the namespace std using namespace std; namespace one{ string name = "namesapce one的name"; } namespace two{ string name = "namesapce two的name"; } string name = "全局的name"; int main() {
      //using one::name;//不行,同一范围内重定义,与下面的name有冲突 cout
    << one::name << endl; cout << two::name << endl; string name = "局部name"; cout << name << endl; cout << ::name << endl; //全局没有范围。没有放在名字空间中的东西都成为放在匿名名字空间, //匿名就是没有名字,直接用双冒号表示,表示全局的或者外面的 /* system("pause");*/ return 0; }

    c风格字符串和c++的区别

    #include <string>    
    #include <ctype.h>
    #include <vector>
    #include <iostream>  
    #include <fstream>
    
    
    // using declarations states our intent to use these names from the namespace std    
    using namespace  std;
    void printString(char *s1, char *s2);
    int main()
    {
        char cs1[30] = "i am cool"; //最多存29个字符,最后一个必须为结尾符,如果定义为csa[9]会出错,没把结尾符算进去
        char cs2[30] = "you are cool";
        
        printString(cs1, cs2);
        strcpy(cs2, cs1);
        printString(cs1, cs2);
    
        cout << "cs1与cs2比较后的结果,返回值为0,-1,1"<<strcmp(cs1, cs2)<< endl;
        cout << "cs2的长度为(不包括结尾符) "<<strlen(cs2) << endl;
        cout << "将cs1的附加到cs1后面"<<strcat(cs1, cs2) << endl;
        cout << cs1 << endl;
        return 0;
    }
    
    void printString(char *s1,char *s2)
    {
        cout << "s1= "<<s1 << "   s2= "<< s2 << endl;
    }
    #include <string>    
    #include <ctype.h>
    #include <vector>
    #include <iostream>  
    #include <fstream>
    
    
    // using declarations states our intent to use these names from the namespace std    
    using namespace  std;
    void printString(string s1, string s2);
    int main()
    {
        string cpps1 = "i am cool";
        string cpps2 = "you are cool";

        string::size_type index= cpps1.find("i");//查找第一个字符出现的位置
        cout << index << endl;

      const char*s1 = cpps1.c_str();// c++风格转换为c风格
        printString(cpps1, cpps2);
        cpps1 = cpps2; 
        printString(cpps1, cpps2);
    
    /*    cout << "cs1与cs2比较后的结果,返回值为0,-1,1"<<strcmp(cs1, cs2)<< endl;*/
        cout << "cs2的长度为(不包括结尾符) "<<cpps2.size() << endl;
        cout << "将cs1的附加到cs1后面"<< cpps1+cpps2<< endl;
        cout << cpps1 << endl;
        cout << "将cs1的附加到cs1后面" << (cpps1+=cpps2) << endl;
        cout << cpps1 << endl;
        return 0;
    }
    
    void printString(string s1,string s2)
    {
        cout << "s1= "<<s1 << "   s2= "<< s2 << endl;
    }

    两种字符串混合使用时,会自动转成c++风格

    枚举类型

    #include <string>    
    #include <ctype.h>
    #include <vector>
    #include <iostream>  
    #include <fstream>
    
    
    // using declarations states our intent to use these names from the namespace std    
    using namespace  std;
    enum Color{BLACK,WHITE,GREEN,YELLOW,RED};
    enum Color_me{ BLACK_me, WHITE_me, GREEN_me, YELLOW_me, RED_me };
    
    int main()
    {
        int v;
        Color c; //c里面的数字虽然看上去像整型,但是是Color型,比整型的范围要小
        Color_me c2;
        c = BLACK;
        cout << c << endl; //输出0
        v = c; //将范围小的BLACK复制到Int类型是可行的
        cout << v << endl;
        //c = v; //将int类型变为BLACK类型不可行,缩小了范围。
        //c2 = c; //两个不同的枚举类型之间的值得类型也是不同的,不可以相互赋值
    
        return 0;
    }
  • 相关阅读:
    [转]狼的故事8:生存就是坚持
    [转]狼的故事7:单枪匹马的代价
    如何在GridView的Footer内显示总计?
    javascript中如何正确将日期(Date)字符串转换为日期(Date)对象?
    无限级分类(非递归算法/存储过程版/GUID主键)完整数据库示例_(1)表结构
    [转]狼的故事12:王者的风范
    [转]狼的故事2:光线背后的嚎叫
    vs.net2008正式版发布并提供下载(英文版)
    [转]狼的故事11:以牙还牙
    [转]狼的故事3:百分之百的死亡
  • 原文地址:https://www.cnblogs.com/yican/p/3915019.html
Copyright © 2011-2022 走看看