C++简易

一.内存

1、栈区(stack):由编译器自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。 
2、堆区(heap):动态内存分配例如malloc,一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。注意它与数据结构中的堆是两回事,分配方式类似于链表。 
3、全局区(静态区,static):全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域, 未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。 - 程序结束后有系统释放 
4、文字常量区:常量字符串就是放在这里的。 程序结束后由系统释放 
5、程序代码区:存放函数体的二进制代码。

二.命名空间

防止命名冲突

 1 #include<iostream>
 2 using namespace std;
 3 
 4 namespace mfc
 5 {
 6     int inflag;
 7     void g(int a){cout << a << endl;}
 8 }
 9 
10 namespace owl
11 {
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;//1
21     cout << owl::inflag << endl;//2
22 
23     mfc::g(1);//1
24     owl::g(2);//2
25 
26     return 0;
27 }
 1 #include<iostream>
 2 using namespace std;
 3 
 4 namespace mfc
 5 {
 6     int inflag;
 7     void g(int a){cout << a << endl;}
 8 }
 9 
10 namespace owl
11 {
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;//1
21     cout << owl::inflag << endl;//2
22     return 0;
23 }
 1 #include<iostream>
 2 using namespace std;
 3 
 4 namespace mfc
 5 {
 6     int inflag;
 7     void g(int a){cout << a << endl;}
 8 }
 9 
10 namespace owl
11 {
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;//1
22     cout << owl::inflag << endl;//2
23     g(1);//1
24     return 0;
25 }

三.操作符

 1 #include<iostream>
 2 #include <string>
 3 #include <iomanip>//操作符
 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.1415
16     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+001
20 
21     cout << hex << 16 << endl; // 10
22     cout << oct << 8 << endl;  // 10
23     cout << dec << 0x10 << endl; // 16
24 
25     return 0;
26 }

四.文件

 1 #include<iostream>
 2 #include<fstream>
 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 = 1
29         Num = 2
30         Num = 3
31         Num = 4
32         Num = 5
33         */
34     }
35 
36     infile.close();
37     outfile.close();
38 
39 
40     return 0;
41 }
 1 #include<iostream>
 2 #include<fstream>
 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 = 1
29         Num = 2
30         Num = 3
31         Num = 4
32         Num = 5
33         */
34     }
35 
36     long location = outfile.tellp();//当前指针
37     location = 10L;
38     outfile.seekp(location);//移动到第十个字节处
39     location = outfile.tellp();//10
40     outfile.seekp(location, ios::beg);//从头数,移动到第十个字节处
41     location = outfile.tellp();//10
42     outfile.seekp(location, ios::cur);//从当前,移动到第十个字节处
43     location = outfile.tellp();//20
44     outfile.seekp(5L, ios::end);//从尾数,移动到第十个字节处
45     location = outfile.tellp();//5
46 
47 
48     location = infile.tellg();//当前指针
49     location = 10L;
50     infile.seekg(location);//移动到第十个字节处
51     location = infile.tellg();//-1
52     infile.seekg(location, ios::beg);//从头数,移动到第十个字节处
53     location = infile.tellg();//-1
54     infile.seekg(location, ios::cur);//从当前,移动到第十个字节处
55     location = infile.tellg();//-1
56     infile.seekg(5L, ios::end);//从尾数,移动到第十个字节处
57     location = infile.tellg();//-1
58 
59     infile.close();
60     outfile.close();
61 
62 
63     return 0;
64 }
 1 #include<iostream>
 2 #include<fstream>
 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 #include<iostream>
 2 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 #include<iostream>
 2 #include<string>
 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 1000
12     cout << Mid << " " << Last << endl;//1 1001
13 
14     //结构体默认public
15     struct Point1
16     {
17     };
18     Point1 p1, p2;
19 
20     typedef struct Point2
21     {
22     }*P;
23     P p3, p4;
24 
25     //const
26     int b = 100, c = 1000;
27 
28     const int *a = &b;
29     *a = 4;//error
30     a = &c;
31     b = 4;
32 
33     int *const d = &b;
34     *d = 4;
35     d = &c;//error
36 
37     const int *p1;int *p2;
38     p1 = new int(3);
39     p2 = new int(2);
40     p1 = p2;//ok
41     p2 = p1;//error
42     p2 = (int *)p1;
43     *p2 = 5;
44     cout << *p1 << endl;//5
45 
46     return 0;
47 }
 1 #include <iostream>
 2 #include <string>
 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;//error
10     cout << (n < m) << endl;
11     return 0;
12 }
 1 #include <iostream>
 2 #include <string>
 3 #include <fstream>
 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 #include <iostream>
 2 #include <typeinfo>
 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; //float
10     return 0;
11 }
原文地址:https://www.cnblogs.com/wanderingzj/p/5292827.html