一、输出语句
#include <iostream> #include <cstdio> //C++风格 int main(void) { //输出语句 std::cout << "hello world!" << std::endl; printf("hello world!\n"); return 0; }
二、命名空间
#include <iostream> namespace ns1{ void func(void){ std::cout << "ns1的func" << std::endl; } } namespace ns2{ void func(void){ std::cout << "ns2的func" << std::endl; } } int main(void) { //func();//名字空间成员不能直接访问 //通过"空间::"访问 ns1::func(); ns2::func(); return 0; }
#include <iostream> //标准名字空间指令,以后省略"std::" using namespace std; namespace ns1{ void func(void){ cout << "ns1的func" << endl; } } namespace ns2{ void func(void){ cout << "ns2的func" << endl; } } int main(void) { using namespace ns1;//名字空间指令 func();//ns1的func using namespace ns2;//名字空间指令 //func();//歧义错误 ns2::func(); return 0; }
#include <iostream> //标准名字空间指令,以后省略"std::" using namespace std; namespace ns1{ void func(void){ cout << "ns1的func" << endl; } } namespace ns2{ void func(void){ cout << "ns2的func" << endl; } } int main(void) { using ns1::func;//名字空间声明 func(); //using ns2::func; //func();//歧义错误 using namespace ns2;//名字空间指令 func();//ns1的func,局部优先 ns2::func();//ns2的func return 0; }
#include <iostream> using namespace std; namespace ns1{ int num = 100; } namespace ns2{ int num = 200; } //int num = 300;//全局作用域 namespace { //无名名字空间 int num = 300; } int main(void) { cout << num << endl;//300 using ns1::num;//名字空间声明 cout << num << endl;//100 cout << ns2::num << endl;//200 cout << ::num << endl;//300 return 0; }
三、结构体
#include <iostream> using namespace std; struct Teacher{ //成员变量 char name[20]; int age; double salary; //成员函数 void who(void){ cout << "我叫" << name << ",今年" << age << "岁,工资是" << salary << endl; } }; int main(void) { /*struct*/ Teacher wangjl = {"王建立",48,800.5}; wangjl.who(); return 0; }
四、联合体
#include <iostream> #include <cstdio> using namespace std; int main(void) { union{//匿名联合 unsigned int ip1; unsigned char ip2[4]; }; ip1 = 0x12345678; for(int i=0;i<4;i++){ printf("%#x ",ip2[i]); } printf("\n"); return 0; }
五、枚举
#include <iostream> using namespace std; int main(void) { enum Color{RED,YELLOW,BLUE}; cout << RED << ',' << YELLOW << ',' << BLUE << endl;//0 1 2 /*enum*/ Color c; //c = 2;//C:ok C++:error c = BLUE;//C:ok C++:ok cout << c << endl;//2 return 0; }
六、字符串
#include <iostream> #include <cstring> using namespace std; int main(void) { //定义 string s = "hello"; cout << "s=" << s << endl; //拷贝 string s2; s2 = s; cout << "s2=" << s2 << endl; //连接 string s3 = " world"; s3 = s + s3; cout << "s3=" << s3 << endl; s += " world";//s = s + " world" cout << "s=" << s << endl; //比较 if(s == s3){ cout << "比较成立" << endl; } else{ cout << "比较不成立" << endl; } //获取字符串中某个字符 s[0] = 'H'; s[6] = 'W'; cout << s << endl;//Hello World //获取字符串长度 cout << s.size() << endl;//11 cout << s.length() << endl;//11 //转换为C风格的字符串 cout << strlen(s.c_str()) << endl;//11 return 0; }
#include <iostream> using namespace std; int main(void) { string str; cout << "请输入一个字符串:" << endl; //cin >> str;//碰到空白符结束 getline(cin,str);//碰到"\n"结束 int count = 0; for(int i=0;i<str.size();i++){ if(str[i]=='A' || str[i]=='a'){ ++count; } } cout << "包含A/a的个数是:" << count << endl; return 0; }
#include <iostream> using namespace std; int main(void) { string str; cin >> str; int len = str.size(); //"abcdef"==>"fedcba" for(int i=0; i<len/2; i++){ str[i] = str[i] ^ str[len-1-i]; str[len-1-i] = str[i] ^ str[len-1-i]; str[i] = str[i] ^ str[len-1-i]; } cout << str << endl; return 0; //char c1 = 3; //char c2 = 5; //c1 = c1 ^ c2; //c2 = c1 ^ c2; //c1 = c1 ^ c2; }