C + + namespaces, differences between C and C + + strings, enumeration types

Source: Internet
Author: User
Tags strcmp

 1: Name space
The difference between 2:C and C + + string and basic operation
3: Enum type
Name Space

#include <string>#include<ctype.h>#include<vector>#include<iostream>#include<fstream>//using declarations states our intent to use these names from the namespace Stdusing namespacestd;namespaceone{stringName ="Namesapce one's name";}namespacetwo{stringName ="NAMESAPCE The name of the";}stringName ="the global name";intMain () {
Using one::name;//No, redefine within the same scope, conflict with name below cout<< One::name <<Endl; cout<< Two::name <<Endl; stringName ="Local name"; cout<< name <<Endl; cout<<:: Name << Endl;//The global has no scope. Everything that's not in the namespace is placed in an anonymous namespace,//Anonymous is not a name, directly with a double colon, representing the global or the outside /*System ("pause");*/ return 0;}

C-style strings and C + + differences

#include <string>#include<ctype.h>#include<vector>#include<iostream>#include<fstream>//using declarations states our intent to use these names from the namespace Stdusing namespacestd;voidPrintstring (Char*S1,Char*S2);intMain () {Charcs1[ -] ="I am cool";//a maximum of 29 characters, the last must be a trailing character, if defined as csa[9] will be an error, do not count the end character in    Charcs2[ -] ="You are cool";    Printstring (CS1, CS2);    strcpy (CS2, CS1);    Printstring (CS1, CS2); cout<<"CS1 compared to CS2, the return value is 0,-1,1"&LT;&LT;STRCMP (CS1, CS2) <<Endl; cout<<"the length of the CS2 is (not including the end character)"<<strlen (CS2) <<Endl; cout<<"attach the CS1 to the CS1 back"<<strcat (CS1, CS2) <<Endl; cout<< CS1 <<Endl; return 0;}voidPrintstring (Char*S1,Char*S2) {cout<<"s1="&LT;&LT;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 Stdusing namespacestd;voidPrintstring (stringS1,stringS2);intMain () {stringCPPS1 ="I am cool"; stringCPPS2 ="You are cool";

String::size_type index= cpps1.find ("i");//Find where the first character appears
cout << index << Endl;

Const CHAR*S1 = CPPS1.C_STR ();//C + + style conversion to C-style printstring (CPPS1, CPPS2); Cpps1=CPPS2; Printstring (CPPS1, CPPS2);/*cout << "CS1 compared to CS2 results, the return value is 0,-1,1" <<strcmp (CS1, CS2) << Endl;*/cout<<"the length of the CS2 is (not including the end character)"<<cpps2.size () <<Endl; cout<<"attach the CS1 to the CS1 back"<< cpps1+cpps2<<Endl; cout<< Cpps1 <<Endl; cout<<"attach the CS1 to the CS1 back"<< (CPPS1+=CPPS2) <<Endl; cout<< Cpps1 <<Endl; return 0;}voidPrintstring (stringS1,stringS2) {cout<<"s1="&LT;&LT;S1 <<"s2="<< S2 <<Endl;}

When two strings are mixed, they automatically turn into C + + style

Enum type

#include <string>#include<ctype.h>#include<vector>#include<iostream>#include<fstream>//using declarations states our intent to use these names from the namespace Stdusing namespacestd;enumcolor{black,white,green,yellow,red};enumcolor_me{Black_me, White_me, Green_me, Yellow_me, red_me};intMain () {intv; Color C; //the number in C looks like an integer, but it's a color type, smaller than the range of integers.Color_me C2; C=BLACK; cout<< c << Endl;//Output 0v = c;//It is possible to copy a small range of black to int typecout << v <<Endl; //C = v;//Changing the int type to black type is not possible, narrowing the range. //C2 = c;//two different enumeration types are also different from each other and cannot be assigned to one another .    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.