一.内存
1、栈区(stack):由编译器自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。 2、堆区(heap):动态内存分配例如malloc,一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。注意它与数据结构中的堆是两回事,分配方式类似于链表。 3、全局区(静态区,static):全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域, 未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。 - 程序结束后有系统释放 4、文字常量区:常量字符串就是放在这里的。 程序结束后由系统释放 5、程序代码区:存放函数体的二进制代码。
二.命名空间
防止命名冲突
1 #include2 using namespace std; 3 4 namespace mfc 5 { 6 int inflag; 7 void g(int a){cout << a << endl;} 8 } 9 10 namespace owl11 {12 int inflag;13 void g(int b){cout << b << endl;}14 }15 16 int main()17 {18 mfc::inflag = 1;19 owl::inflag = 2;20 cout << mfc::inflag << endl;//121 cout << owl::inflag << endl;//222 23 mfc::g(1);//124 owl::g(2);//225 26 return 0;27 }
1 #include2 using namespace std; 3 4 namespace mfc 5 { 6 int inflag; 7 void g(int a){cout << a << endl;} 8 } 9 10 namespace owl11 {12 int inflag;13 void g(int b){cout << b << endl;}14 }15 using mfc::inflag;16 int main()17 {18 inflag = 1;19 owl::inflag = 2;20 cout << inflag << endl;//121 cout << owl::inflag << endl;//222 return 0;23 }
1 #include2 using namespace std; 3 4 namespace mfc 5 { 6 int inflag; 7 void g(int a){cout << a << endl;} 8 } 9 10 namespace owl11 {12 int inflag;13 void g(int b){cout << b << endl;}14 }15 using namespace mfc;16 17 int main()18 {19 inflag = 1;20 owl::inflag = 2;21 cout << inflag << endl;//122 cout << owl::inflag << endl;//223 g(1);//124 return 0;25 }
三.操作符
1 #include2 #include 3 #include //操作符 4 using namespace std; 5 6 int main() 7 { 8 double a = 3.1415; 9 double b = 2.2222;10 string str;11 12 //cout << right右对齐,cout << left左对齐,默认右对齐13 cout << setw(8) << a << endl;//空格空格3.1415,设置字段位数14 cout << setfill('c') << setw(8) << a << endl;//cc3.1415,填充字符15 cout << setw(8) << a << endl;//cc3.141516 cout << setprecision(2) << a << endl;//3.1,设置字段精度17 cout << setprecision(2) << fixed << a << endl;//3.14,小数点后精度18 cout << showpoint << 2.2 << endl;//2.20,强制显示小数点及尾部0,可以用noshowpoint取消19 cout << scientific << 33.33 << endl;//3.33e+00120 21 cout << hex << 16 << endl; // 1022 cout << oct << 8 << endl; // 1023 cout << dec << 0x10 << endl; // 1624 25 return 0;26 }
四.文件
1 #include2 #include 3 using namespace std; 4 5 int main() 6 { 7 ifstream infile; 8 ofstream outfile; 9 10 int num;11 infile.open("E:\\1.txt");12 outfile.open("E:\\2.txt", ios::app);13 /*14 ios::out输出到文件删除原有内容15 ios::binary以二进制格式打开文件16 ios::app输出到文件保留原有内容17 */18 19 if (! infile)//测试文件是否成功打开20 {21 cerr << "Error" << endl;22 }23 while (infile >> num)24 {25 cout << num << endl;26 outfile << "Num = " << num << endl;27 /*28 Num = 129 Num = 230 Num = 331 Num = 432 Num = 533 */34 }35 36 infile.close();37 outfile.close();38 39 40 return 0;41 }
1 #include2 #include 3 using namespace std; 4 5 int main() 6 { 7 ifstream infile; 8 ofstream outfile; 9 10 int num;11 infile.open("E:\\1.txt");12 outfile.open("E:\\2.txt", ios::app);13 /*14 ios::out输出到文件删除原有内容15 ios::binary以二进制格式打开文件16 ios::app输出到文件保留原有内容17 */18 19 if (!infile)//测试文件是否成功打开20 {21 cerr << "Error" << endl;22 }23 while (infile >> num)24 {25 cout << num << endl;26 outfile << "Num = " << num << endl;27 /*28 Num = 129 Num = 230 Num = 331 Num = 432 Num = 533 */34 }35 36 long location = outfile.tellp();//当前指针37 location = 10L;38 outfile.seekp(location);//移动到第十个字节处39 location = outfile.tellp();//1040 outfile.seekp(location, ios::beg);//从头数,移动到第十个字节处41 location = outfile.tellp();//1042 outfile.seekp(location, ios::cur);//从当前,移动到第十个字节处43 location = outfile.tellp();//2044 outfile.seekp(5L, ios::end);//从尾数,移动到第十个字节处45 location = outfile.tellp();//546 47 48 location = infile.tellg();//当前指针49 location = 10L;50 infile.seekg(location);//移动到第十个字节处51 location = infile.tellg();//-152 infile.seekg(location, ios::beg);//从头数,移动到第十个字节处53 location = infile.tellg();//-154 infile.seekg(location, ios::cur);//从当前,移动到第十个字节处55 location = infile.tellg();//-156 infile.seekg(5L, ios::end);//从尾数,移动到第十个字节处57 location = infile.tellg();//-158 59 infile.close();60 outfile.close();61 62 63 return 0;64 }
1 #include2 #include 3 using namespace std; 4 5 class Student 6 { 7 public: 8 char name[500], score[500]; 9 };10 11 int main()12 {13 Student s;14 ofstream outfile("E:\\stu.dat", ios::out | ios::binary);15 16 while (cin >> s.name >> s.score)17 {18 if (strcmp(s.name, "exit") == 0)19 {20 break;21 }22 outfile.write((char*)&s, sizeof(s));23 }24 outfile.close();25 26 ifstream infile("E:\\stu.dat", ios::in | ios::binary);27 if (!infile)28 {29 cerr << "error" << endl;30 }31 32 //infile.get(c);读取一个字符33 //outfile.put(c);写入一个字符34 while (infile.read((char*)&s, sizeof(s)))35 {36 int ReadBytes = infile.gcount();37 cout << ReadBytes << " " << s.name << " " << s.score << endl;38 }39 infile.close();40 return 0;41 }
五.若干特性
1 #include2 using namespace std; 3 4 int main() 5 { 6 bool flag = false; 7 cout << flag << endl; // 0 8 cout << boolalpha << flag << endl; // false,boolalpha强制让bool类型输出true和false 9 return 0;10 }
1 #include2 #include 3 using namespace std; 4 5 int main() 6 { 7 //枚举 8 enum Num{MinSize = 0, Mid, MaxSize = 1000, Last}; 9 Num x, y;10 int i = MinSize, j = MaxSize;11 cout << i << " " << j << endl; // 0 100012 cout << Mid << " " << Last << endl;//1 100113 14 //结构体默认public15 struct Point116 {17 };18 Point1 p1, p2;19 20 typedef struct Point221 {22 }*P;23 P p3, p4;24 25 //const26 int b = 100, c = 1000;27 28 const int *a = &b;29 *a = 4;//error30 a = &c;31 b = 4;32 33 int *const d = &b;34 *d = 4;35 d = &c;//error36 37 const int *p1;int *p2;38 p1 = new int(3);39 p2 = new int(2);40 p1 = p2;//ok41 p2 = p1;//error42 p2 = (int *)p1;43 *p2 = 5;44 cout << *p1 << endl;//545 46 return 0;47 }
1 #include2 #include 3 using namespace std; 4 5 int main(int argc, char *argv[]) 6 { 7 //< <和> > 比 关系,比较,逻辑,赋值,三目操作符等级高 8 int n = 1, m = 0; 9 cout << n < m << endl;//error10 cout << (n < m) << endl;11 return 0;12 } 和>
1 #include2 #include 3 #include 4 using namespace std; 5 6 int v = 10; 7 int main(int argc, char *argv[]) 8 { 9 int v = 5;10 cout << v << endl; // 5 局部覆盖全局11 cout << ::v << endl; // 10 全局12 return 0;13 }
六.输入输出
cerr:打印出错信息,不使用缓冲区,直接从屏幕输出
clog:打印出错信息,使用缓冲区,缓冲区满或者刷新时才输出到屏幕
1 ofstream out("text.txt", "w", stdout); 2 3 if (y == 0) 4 { 5 cerr << "error" << endl; 6 } 7 else 8 { 9 cout << x / y; 输出到text.txt文件,不是打印10 }
七.typeid
1 #include2 #include 3 using namespace std; 4 5 int main() 6 { 7 float x; 8 cout << (typeid(x) == typeid(float)) << endl; // true 9 cout << typeid(x).name() << endl; //float10 return 0;11 }