重新实践c++primer上面的代码
又重新敲了敲c++primer上面的代码,觉得很有意思,讲的很细,c++真牛逼啊
1 #include <iostream> 2 #include <string> 3 #include <cctype> 4 #include <vector> 5 #include <cassert> 6 using namespace std; 7 using std::ostringstream; 8 9 int Add() 10 { 11 cout << "enter two numbers" << endl; 12 13 int v1 = 0, v2 = 0; 14 cin >> v1 >> v2; 15 cout << "the sum of" << v1 << "and" << v2 16 << "is" << v1 + v2 << endl; 17 return 0; 18 } 19 20 int Unsigned() 21 { 22 unsigned u = 10, u2 = 42; 23 cout << u2 - u << endl; 24 cout << u - u2 << endl; 25 26 int i = 10, i1 = 42; 27 cout << i - i1 << endl; 28 cout << i1 - i << endl; 29 30 u = 42; 31 i = 10; 32 cout << i - u << endl; 33 cout << u - i << endl; 34 35 u = 10; 36 i = -42; 37 cout << i + i << endl; 38 cout << u + i << endl; 39 40 // u = 10; 41 // cout << "hehe" << endl; 42 // while (u >= 0) 43 // { 44 // cout << u << endl; 45 // u--; 46 // } 47 return 0; 48 } 49 50 int reused = 42; 51 52 int Scope_Levels() 53 { 54 int unique = 0; 55 56 cout << reused << "" << unique << endl; 57 int reused = 0; 58 cout << reused << "" << unique << endl; 59 cout << ::reused << "" << unique << endl; 60 return 0; 61 } 62 63 int ReferenceTest() 64 { 65 int i = 0, &ri = i; 66 67 cout << i << " " << ri << endl; 68 i = 4; 69 cout << i << " " << ri << endl; 70 71 ri = 10; 72 cout << i << " " << ri << endl; 73 74 return 0; 75 } 76 77 int CompoundRef() 78 { 79 int i = 1024, *p = &i, &r = i; 80 cout << i << " " << *p << " " << r << endl; 81 82 int j = 42, *p2 = &j; 83 int *&pref = p2; 84 cout << *pref << endl; 85 86 pref = &i; 87 cout << *pref << endl; 88 89 *pref = 0; 90 cout << i << " " << *pref << " " << *p2 << endl; 91 92 return 0; 93 } 94 95 // int Convs() 96 // { 97 // int i = 42; 98 // cout << i << endl; 99 // 100 // if (i) 101 // i = 0; 102 // cout << i << endl; 103 // 104 // bool b = 42; 105 // cout << b << endl; 106 // 107 // int j = b; 108 // cout << j << endl; 109 // 110 // double pi = 3.14; 111 // cout << pi << endl; 112 // 113 // j = pi; 114 // cout << j << endl; 115 // 116 // unsigned char c = -1; 117 // i = c; 118 // cout << i << endl; 119 // 120 // return 0; 121 // } 122 123 int TestDoublePtr() 124 { 125 int ival = 1024; 126 int* pi = &ival; 127 int** ppi = π 128 129 cout << ival << " " << *pi << " " << **ppi << endl; 130 131 int i = 2; 132 int* p1 = &i; 133 134 *p1 = *p1 * *p1; 135 cout << "i = " << i << endl; 136 137 *p1 *= *p1; 138 cout << "i = " << i << endl; 139 return 0; 140 } 141 142 int TestDecltype() 143 { 144 int a = 0; 145 decltype(a) c = a; 146 decltype((a)) d = a; 147 148 c++; 149 cout << "a" << a << "c" << c << "d" << d << endl; 150 151 d++; 152 cout << "a" << a << "c" << c << "d" << d << endl; 153 154 int A = 0, B = 0; 155 decltype((A)) C = A; 156 decltype(A = B) D = A; 157 158 C++; 159 cout << A << C << D << endl; 160 D++; 161 cout << A << C << D << endl; 162 163 return 0; 164 } 165 166 int TestCctype() 167 { 168 string s("Hello World!!!"); 169 // for (auto c : s) 170 // cout << c << endl; 171 172 string::size_type punct_cnt = 0; 173 for (string::size_type c = 0; c != s.size(); c++) 174 if (ispunct(s[c])) 175 ++punct_cnt; 176 cout << punct_cnt << " " << s << endl; 177 178 string orig = s; 179 for (string::size_type c = 0; c != s.size(); c++) 180 s[c] = toupper(s[c]); 181 cout << s << endl; 182 183 s = orig; 184 string::size_type index = 0; 185 while (index != s.size() && !isspace(s[index])) 186 { 187 s[index] = toupper(s[index]); 188 index++; 189 } 190 cout << s << endl; 191 192 return 0; 193 } 194 195 int TestArrayScores() 196 { 197 vector<unsigned> grades; 198 const size_t sz = 11; 199 unsigned scores[sz] = {}; 200 unsigned grade; 201 202 while(cin >> grade) 203 { 204 if (grade <= 100) 205 scores[grade / 10]++; 206 grades.push_back(grade); 207 } 208 cout << "grades.size = " << grades.size() << endl; 209 210 for (vector<unsigned>::const_iterator g = grades.begin(); g != grades.end(); g++) 211 cout << *g << " "; 212 cout << endl; 213 214 for (size_t i = 0; i != sz; i++) 215 cout << scores[i] << " "; 216 cout << endl; 217 return 0; 218 219 } 220 221 int TestGetline() 222 { 223 string line; 224 while (getline(cin, line)) 225 cout << line << endl; 226 return 0; 227 } 228 229 int TestCstring() 230 { 231 string s1 = "A string example"; 232 string s2 = "A different string"; 233 234 if (s1 < s2) 235 cout << s1 << endl; 236 else 237 cout << s2 << endl; 238 239 const char ca1[] = "A string example"; 240 const char ca2[] = "A string example"; 241 242 if (strcmp(ca1, ca2) < 0) 243 cout << ca1 << endl; 244 else 245 cout << ca2 << endl; 246 247 const char *cp1 = ca1, *cp2 = ca2; 248 cout << strcmp(cp1, cp2) << endl; 249 cout << strcmp(cp2, cp1) << endl; 250 cout << strcmp(cp1, cp1) << endl; 251 252 cout << strlen(cp1) << endl; 253 254 const unsigned sz = 16 + 18 + 2; 255 //char largeStr[sz]; 256 //pass 257 return 0; 258 } 259 260 int TestIterator() 261 { 262 string str("some string"), orig = str; 263 if (!str.empty()) 264 cout << str[0] << endl; 265 if (!str.empty()) 266 str[0] = toupper(str[0]); 267 cout << str << endl; 268 269 str = orig; 270 271 if (str.begin() != str.end()) 272 { 273 string::iterator it = str.begin(); 274 *it = toupper(*it); 275 } 276 cout << str << endl; 277 278 str = orig; 279 for (string::size_type index = 0; index != str.size() && !isspace(str[index]); index++) 280 str[index] = toupper(str[index]); 281 cout << str << endl; 282 283 str = orig; 284 for (string::iterator it = str.begin(); it != str.end() && !isspace(*it); it++) 285 *it = toupper(*it); 286 cout << str << endl; 287 288 str = orig; 289 string::size_type index = 0; 290 while (index != str.size() && !isspace(str[index])) 291 { 292 str[index] = toupper(str[index]); 293 index++; 294 } 295 cout << str << endl; 296 297 string::iterator beg = str.begin(); 298 while (beg != str.end() && !isspace(*beg)) 299 { 300 *beg = toupper(*beg); 301 beg++; 302 } 303 cout << str << endl; 304 305 str = orig; 306 for (string::const_iterator c = str.begin(); c != str.end(); c++) 307 cout << *c; 308 cout << endl; 309 for (string::iterator c = str.begin(); c != str.end(); c++) 310 *c = '*'; 311 cout << str << endl; 312 313 str = orig; 314 for (string::size_type ix = 0; ix != str.size(); ix++) 315 cout << str[ix]; 316 for (string::size_type ix = 0; ix != str.size(); ++ix) 317 str[ix] = '*'; 318 cout << str << endl; 319 320 return 0; 321 322 } 323 324 int arr[10]; 325 int *p1[10]; 326 int(*p2)[10] = &arr; 327 328 typedef int arrT[10]; 329 330 arrT* func(int i); 331 int(*func(int i))[10]; 332 333 int odd[] = { 1, 3, 5, 7, 9 }; 334 int even[] = { 0, 2, 4, 6, 8 }; 335 336 int* elemPtr(int i) 337 { 338 return (i % 2) ? odd : even; 339 } 340 341 int(*arrPtr(int i))[5] 342 { 343 return (i % 2) ? &odd : &even; 344 } 345 346 int (&arrRef(int i))[5] 347 { 348 return (i % 2) ? odd : even; 349 } 350 351 int TestBefore1() 352 { 353 int* p = elemPtr(6); 354 int(*arrP)[5] = arrPtr(5); 355 int(&arrR)[5] = arrRef(4); 356 357 for (size_t i = 0; i < 5; i++) 358 cout << p[i]; 359 cout << endl; 360 for (size_t i = 0; i < 5; i++) 361 cout << (*arrP)[i]; 362 cout << endl; 363 for (size_t i = 0; i < 5; i++) 364 cout << arrR[i]; 365 return 0; 366 } 367 368 // struct ErrCode 369 // { 370 // ErrCode(int i) : num(i) {} 371 // string msg() 372 // { 373 // ostringstream s; 374 // s << "ErrCode" << num; 375 // return s.str(); 376 // } 377 // int num; 378 // }; 379 380 string::size_type sumLength(const string&, const string&); 381 string::size_type latgerLength(const string&, const string&); 382 383 string::size_type sumLength(const string& s1, const string& s2) 384 { 385 return s1.size() + s2.size(); 386 } 387 388 string::size_type largerLength(const string& s1, const string& s2) 389 { 390 return (s1.size() > s2.size()) ? s1.size() : s2.size(); 391 } 392 393 string::size_type(*getFcn(const string& fetch))(const string&, const string&) 394 { 395 if (fetch == "sum") 396 return sumLength; 397 return largerLength; 398 } 399 400 int TestBefore2() 401 { 402 cout << getFcn("sum")("hello", "world") << endl; 403 cout << getFcn("larger")("hello", "world") << endl; 404 return 0; 405 } 406 407 inline const string& 408 shorterString(const string& s1, const string& s2) 409 { 410 return s1.size() <= s2.size() ? s1 : s2; 411 } 412 413 int TestBefore3() 414 { 415 string s1("successes"), s2("failure"); 416 cout << shorterString(s1, s2) << endl; 417 cout << shorterString(s1, s2).size() << endl; 418 cout << (s1.size() < s2.size() ? s1 : s2) << endl; 419 return 0; 420 } 421 422 void print(const int ia[], size_t size) 423 { 424 #ifndef NDEBUG 425 cerr << "print(int *, size_t)" << " " << size << endl; 426 #endif 427 } 428 429 int TestErrorDeal() 430 { 431 string word = "foo"; 432 const string::size_type threshold = 5; 433 if (word.size() < threshold) 434 cerr << "Error: " << __FILE__ 435 << " : in function " << TestErrorDeal 436 << " at line " << __LINE__ << endl 437 << " Compiled on " << __DATE__ 438 << " at " << __TIME__ << endl 439 << " Word read was \"" << word 440 << "\": Length too short" << endl; 441 word = "something longer than five chars"; 442 assert(word.size() > threshold); 443 444 return 0; 445 }
这些东西一定是敲过一遍才觉得有趣,当然上边的东西也很初级,这基本上是前四章内重要的函数了,把它们弄在了一起,暂时先不整理了

浙公网安备 33010602011771号