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;
}