1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <list> 5 using namespace std; 6 7 //重载函数 8 int ok(void) 9 { 10 cout << "ok1" << endl; 11 return 0; 12 } 13 14 int ok(int m) 15 { 16 cout << "OK: " << m << endl; 17 return 0; 18 } 19 20 //对于类下的静态成员,因为其是共享的,所以其他对象都可以改变静态成员 21 class zhu { 22 friend int set_score(void); //友元函数,可以访问类里面的私有数据 23 private: //不能由外部直接访问,可以由public部分函数访问 24 char *name; 25 int num; 26 int score; 27 static int add(int num) { 28 return ++num; 29 } 30 public: 31 static int pl; 32 int pk; 33 static int del(int num) //静态变量函数不能访问非静态变量以及非静态函数 34 { 35 num = add(num); 36 num = add(num); 37 return --num; 38 } 39 int input_arg(char *name0, int num0, int score0); 40 int is_upto_60(void); 41 zhu() = default; //构造函数 42 ~zhu(); //析构函数 43 44 }; 45 46 zhu zhu0; 47 zhu zhu1; 48 49 //zhu::zhu(void) //num = i 50 //{ 51 // cout << "class zhu create" << endl; 52 //} 53 54 zhu::~zhu(void) 55 { 56 cout << "class zhu delete"<< endl; 57 } 58 int set_score(void) 59 { 60 char a[6] = "zhuyj"; 61 zhu1.name = a; 62 zhu1.num = 2; 63 zhu1.score = 55; 64 return 0; 65 } 66 int zhu::input_arg(char *name0, int num0, int score0) 67 { 68 name = name0; 69 num = num0; 70 score = score0; 71 return 0; 72 } 73 74 int zhu::is_upto_60(void) 75 { 76 if (score >= 60) 77 return 1; 78 else 79 return 0; 80 } 81 82 int zhu::pl = 0; 83 int main(void) { 84 vector<string> vb{ "jsdfk","sdhj"}; 85 86 int a{1}; 87 int b = a + 1; 88 cout << "b = " << b << endl; 89 string c = "abcdf"; 90 string h; 91 string d(c); 92 string e("sdhj"); 93 string f(6, 'g'); 94 string g = e + f; 95 cout << "d = " << d << endl; 96 cout << "g = " << g << endl; 97 if (h.empty()) 98 cout << "h is empty" << endl; 99 if (g.empty()) 100 cout << "g is empty" << endl; 101 else 102 cout << "g's size is " << g.size() << endl; 103 string s1; 104 for (auto c : g) 105 cout << c << endl; 106 cout << "input string" << endl; 107 //while (getline(cin, s1)) 108 // cout << s1 << endl; 109 ok(); 110 ok(3); 111 //类 112 char y[10] = "zhuyanjun"; 113 zhu0.input_arg(y, 1, 65); 114 if (zhu0.is_upto_60()) 115 cout << "zhu0 ok" << endl; 116 else 117 cout << "zhu0 NO" << endl; 118 if (zhu1.is_upto_60()) 119 cout << "zhu1 ok" << endl; 120 else 121 cout << "zhu1 NO" << endl; 122 zhu::pl = 1; //静态的可以这么赋值,因为整个文件只有一个pl,独立于具体实例化的类存在 123 //zhu0.pl = 2; //这样也是可以的 124 //zhu::pk = 9; //错误,pk变量必须依赖实例化的类存在 125 cout << "pl = " << zhu0.pl << endl; 126 int m; 127 m = zhu::del(5); 128 cout << "m = " << m << endl; 129 //容器 130 vb.push_back("shu"); 131 //使用迭代器访问容器内元数 132 auto y1 = vb.begin(); 133 auto y2 = vb.end(); //迭代器end指向的是容器尾元数之后的元数(不存在) 134 cout << "vb'begin is " << *y1<< endl; 135 cout << "vb'end is " << *(--y2) << endl; 136 //vb.pop_back(); 137 auto h0 = find(vb.cbegin(), vb.cend(),"shu"); 138 cout << "shu " << (h0 == vb.cend() ? "is not present" : "is present") << endl; 139 vb.erase(y2); 140 vb.clear(); 141 if (!vb.empty()) { 142 auto f1 = vb.front(); 143 auto f2 = vb.back(); 144 cout << "vb'begin is " << f1 << endl; 145 cout << "vb'end is " << f2 << endl; 146 } 147 148 list<int> ilist(10, 42); //建立双链表ilist,有10个int, 值都是42 149 ilist.resize(15); //将5个0添加到ilist链表尾部 150 ilist.resize(25, -1); //将10个-1添加到ilist尾部 151 ilist.resize(5); //删除ilist尾部的20个元数 152 153 154 while (1); 155 return 0; 156 }