1 #include <Windows.h> //默认写在最上方 2 #include <iostream> //输入输出流 3 #include <locale.h> //宽字符 显示的头文件 ,以及函数 setlocale(LC_ALL,""); 4 5 #include <vector> //STL 6 #include <list> //STL 7 #include <deque> //STL 8 #include <set> //STL 9 #include <map> //STL 10 #include <hash_map> //STL 11 #include <functional> //STL 函数对象 12 #include <algorithm> //STL 算法,大概有100多个算法 13 14 #include <string.h> //字符串 15 #include <cstring> //字符串 16 #include <string> //字符串 17 18 #include <bitset> //二进制头文件 19 20 #include <cstddef> //size_t的头文件 21 22 #include <xutility> 23 24 #include <stdio.h> 25 #include <math.h> 26 #include <stdlib.h> 27 #include <errno.h> 28 #include <sysinfoapi.h> 29 30 31 32 33 34 using namespace std; 35 36 37 #define Conn(x,y) x##y 38 #define ToChar(x) #@x 39 #define ToString(x) #x 40 41 42 class TestObject 43 { 44 public: 45 TestObject() 46 { 47 this->m_iData1 = 10; 48 this->m_iData2 = 20; 49 this->m_iData3 = 30; 50 } 51 52 private: 53 int m_iData1; 54 int m_iData2; 55 int m_iData3; 56 }; 57 58 59 enum OpenModes 60 { 61 nInput, 62 nOutput, 63 nAppend, 64 nX = 1, 65 nY, 66 nZ = 2, 67 }; 68 69 typedef struct Student 70 { 71 Student() 72 { 73 nAge = 0; 74 nLen = 0; 75 } 76 77 int nAge; 78 int nLen; 79 }Hello; 80 81 //前包,后不包..一定要注意 end的迭代器 82 bool FindInt(vector<int>::iterator begin, vector<int>::iterator end, int &nValue) 83 { 84 bool find = false; 85 86 while (begin != end) 87 { 88 if (*begin == nValue) 89 { 90 find = true; 91 break; 92 } 93 else 94 { 95 begin++; 96 } 97 } 98 99 100 if (find) 101 { 102 return true; 103 } 104 else 105 { 106 return false; 107 } 108 109 110 }//这个就有点像STL里面的 find()函数 111 112 void printt(int elem) 113 { 114 cout << elem << endl; 115 cout << "aaaa" << endl; 116 } 117 118 119 int& add_one(int &x) 120 { 121 122 ++x; 123 return x; 124 125 } 126 127 int AddThree(int *p) 128 { 129 *p = *p + 10; 130 return *p; 131 132 } 133 134 void printValue_5(int(*x)[10], int rowSize) 135 { 136 for (int i = 0; i != rowSize;i++) 137 { 138 139 for (int j = 0; j != 10;++j) 140 { 141 cout << x[i][j] << " "; 142 } 143 cout << endl; 144 } 145 146 147 } 148 149 150 151 152 153 154 class CBook 155 { 156 157 public: 158 int price; 159 int num; 160 int sum; 161 162 int GetPrice() const; 163 164 165 }; 166 167 168 int CBook::GetPrice() const 169 { 170 int a = 1; 171 a = a + 10; //这个是可以进行写操作的 172 173 174 //this->num = 2;//const 放在()后面,说明,不允许修改this的数据.不能进行 写 操作 175 176 177 return 0; 178 } 179 180 CBook obj2; 181 182 183 //递归的实例 184 void DoA() 185 { 186 //cout << "Hello panzhengming" << endl; 187 //DoA(); 188 } 189 190 long Factorial(int n)//阶乘 191 { 192 //数学定义0的阶乘是 1.这是基准 193 194 if ( 0 == n) 195 { 196 return 1; 197 } 198 else 199 { 200 return n * Factorial(n - 1); 201 } 202 } 203 204 205 int main(int argc,char **argv) 206 { 207 setlocale(LC_ALL, ""); 208 209 /* 210 CBook obj; 211 obj.GetPrice(); 212 cout << obj.sum << endl;//局部对象,未初始化的变量都是垃圾就数据 213 cout << obj2.sum << endl;//全局对象,未初始化的变量都是默认值 214 */ 215 216 217 218 /*这段代码,我只是想演示 vector的长度是正常的计数,不是-1 219 vector<int> a; 220 a.push_back(1); 221 a.push_back(2); 222 int nSize = a.size(); 223 cout << nSize << endl; 224 */ 225 226 227 228 /* 229 vector<int> vec1; 230 vec1.clear(); 231 vec1.push_back(10); 232 vec1.push_back(20); 233 vec1.push_back(30); 234 vec1.push_back(40); 235 vec1.push_back(50); 236 //vec1数据 237 for (size_t i = 0; i < vec1.size();i++) 238 { 239 cout << vec1[i] << endl; 240 } 241 242 cout << "------------------" << endl; 243 //vec2数据 244 vector<int> vec2(vec1);//用vec1初始化vec2 245 for (size_t i = 0; i < vec2.size(); i++) 246 { 247 cout << vec2[i] << endl; 248 } 249 250 cout << "--------------------------" << endl; 251 vector<int> vec3(20,5);//20个数据,每个元素的值都是5 252 for (size_t i = 0; i < vec3.size(); i++) 253 { 254 cout << vec3[i] << endl; 255 } 256 257 258 //vector<Foo> a; 259 //vector<Foo> b(10, 1);//10表示vector数量,1表示Foo构造函数的参数 260 261 262 vector<int>::iterator first = vec1.begin(); 263 vector<int>::iterator last = vec1.end(); 264 if (first != last) 265 { 266 int number = *first + *(first + 1); 267 cout << number << endl; 268 269 first++; 270 } 271 272 */ 273 274 275 /* 276 vector<int> vec1; 277 vec1.push_back(10); 278 vec1.push_back(20); 279 vec1.push_back(30); 280 vec1.push_back(40); 281 vec1.push_back(50); 282 283 const vector<int> vec2(vec1); 284 285 //常迭代器 286 vector<int>::iterator Iter = vec2.begin(); 287 288 while (Iter != vec2.end()) 289 { 290 //修改元素值 291 //*Iter = *Iter + 10000; 292 293 cout << *Iter << endl; 294 295 Iter++; 296 } 297 298 */ 299 300 /* 301 vector<string> list1; 302 list1.push_back("Beijing"); 303 list1.push_back("shanghai"); 304 list1.push_back("nnajing"); 305 list1.push_back("yancheng"); 306 list1.push_back("xiangshui"); 307 308 309 list1.resize(3); 310 for (vector<string>::iterator Iter = list1.begin(); Iter != list1.end();Iter++) 311 { 312 cout << *Iter << endl; 313 } 314 315 //vector<string>::iterator Iter = list1.begin(); 316 //while (Iter != list1.end()) 317 //{ 318 // cout << *Iter << endl; 319 // Iter++; 320 //} 321 322 323 324 //vector<int>::size_type count = list1.size(); 325 //vector<int>::size_type maxCount = list1.max_size(); 326 327 //cout << count << " " << maxCount << endl; 328 329 //if (list1.empty()) 330 //{ 331 // cout << "容器是空的" << endl; 332 //} 333 //else 334 //{ 335 // cout << "容器不是空的" << endl; 336 //} 337 //cout << list1.capacity() << endl; 338 //cout << list1.size() << endl; 339 340 //cout << list1.size() << endl; 341 //cout << list1.capacity() << endl; 342 343 //list1.resize(0); 344 //if (list1.empty()) 345 //{ 346 // cout << "empty()" << endl; 347 //} 348 */ 349 350 351 /* 352 vector<int> list1; 353 354 list1.push_back(11); 355 list1.push_back(22); 356 list1.push_back(33); 357 list1.push_back(44); 358 359 int a = list1.at(3); 360 cout << a << endl; 361 */ 362 363 364 /* 365 list<string> ilist; 366 ilist.push_back("apple"); 367 ilist.push_back("bill"); 368 ilist.push_back("cat"); 369 ilist.push_back("dog"); 370 ilist.push_back("egg"); 371 ilist.push_back("fish"); 372 ilist.push_back("girl"); 373 374 375 //ilist.pop_front(); 376 //ilist.pop_back(); 377 378 379 //string sValue("bill"); 380 //string svalue2 = "girl"; 381 //list<string>::iterator Iter1 = find(ilist.begin(), ilist.end(), sValue); 382 //list<string>::iterator Iter2 = find(ilist.begin(), ilist.end(), svalue2); 383 384 //if (Iter1 != ilist.end() && Iter2 != ilist.end()) 385 //{ 386 // cout << "找到了 " << sValue << endl; 387 // ilist.erase(Iter1, Iter2); 388 //} 389 //else 390 //{ 391 // cout << "没找到 " << sValue << endl; 392 //} 393 394 ilist.erase(ilist.begin(), ilist.end()); 395 if (ilist.empty()) 396 { 397 cout << "全部删除了,之所以能删除是因为 ilist.end()表示最后1个后1个, 所以前面是全部删除了" << endl; 398 } 399 else 400 { 401 cout << "no" << endl; 402 } 403 404 for (list<string>::iterator Iter = ilist.begin(); Iter != ilist.end();Iter++) 405 { 406 cout << *Iter << endl; 407 } 408 409 410 */ 411 412 413 414 415 /* 416 vector<int> a; 417 vector<int> b; 418 vector<int> c; 419 420 a.push_back(1000); 421 a.push_back(2000); 422 a.push_back(3000); 423 a.push_back(4000); 424 425 b.push_back(11); 426 b.push_back(22); 427 b.push_back(33); 428 429 c.push_back(66); 430 c.push_back(77); 431 c.push_back(88); 432 433 cout << "a---------" << endl; 434 for (vector<int>::iterator Itera = a.begin(); Itera != a.end();Itera++) 435 { 436 cout << *Itera << endl; 437 } 438 439 cout << "b----" << endl; 440 for (vector<int>::iterator Iterb = b.begin(); Iterb != b.end();Iterb++) 441 { 442 cout << *Iterb << endl; 443 } 444 445 446 cout << "------------------------------------------------开始交换" << endl; 447 448 //a.swap(b); 449 b.swap(a); 450 451 cout << "a---------" << endl; 452 for (vector<int>::iterator Itera2 = a.begin(); Itera2 != a.end(); Itera2++) 453 { 454 cout << *Itera2 << endl; 455 } 456 457 cout << "b----" << endl; 458 for (vector<int>::iterator Iterb2 = b.begin(); Iterb2 != b.end(); Iterb2++) 459 { 460 cout << *Iterb2 << endl; 461 } 462 463 cout << "赋值 -------------------------------------------------------------" << endl; 464 465 a = b; 466 467 for (vector<int>::iterator Itera2 = a.begin(); Itera2 != a.end(); Itera2++) 468 { 469 cout << *Itera2 << endl; 470 } 471 472 cout << "b----" << endl; 473 for (vector<int>::iterator Iterb2 = b.begin(); Iterb2 != b.end(); Iterb2++) 474 { 475 cout << *Iterb2 << endl; 476 } 477 478 cout << "赋值 -------------------------------------------------------------" << endl; 479 480 a.assign(c.begin(), c.end()); 481 for (vector<int>::iterator Itera2 = a.begin(); Itera2 != a.end(); Itera2++) 482 { 483 cout << *Itera2 << endl; 484 } 485 486 cout << "赋值 -------------------------------------------------------------" << endl; 487 488 489 a.assign(5, 8888); 490 for (vector<int>::iterator Itera2 = a.begin(); Itera2 != a.end(); Itera2++) 491 { 492 cout << *Itera2 << endl; 493 } 494 495 496 cout << "----------------------------------------------容量 增长" << endl; 497 a.reserve(5); 498 cout << a.size() << " " << a.capacity() << endl; 499 */ 500 501 502 503 /* 504 cout <<"----------------------------------------------------------------deque类"<<endl; 505 506 507 deque<int> a; 508 a.push_back(3); 509 a.push_back(4); 510 511 a.push_front(2); 512 a.push_front(1); 513 514 for (deque<int>::iterator Iter = a.begin(); Iter != a.end();Iter++) 515 { 516 cout << *Iter << endl; 517 } 518 519 520 cout << "删除" << endl; 521 522 a.pop_back(); 523 for (size_t nCount = 0; nCount < a.size();nCount++) 524 { 525 cout << a[nCount] << endl; 526 } 527 528 */ 529 530 531 /* 532 533 list<int> a; 534 a.push_front(3); 535 a.push_front(2); 536 a.push_back(4); 537 a.push_front(1); 538 a.push_front(0); 539 540 for (list<int>::iterator Iter = a.begin(); Iter != a.end();Iter++) 541 { 542 cout << *Iter << " "; 543 } 544 545 cout << endl; 546 a.reverse(); 547 for (list<int>::iterator Iter = a.begin(); Iter != a.end(); Iter++) 548 { 549 cout << *Iter << " " ; 550 } 551 552 */ 553 554 555 556 557 //算法 558 /* 559 //自动排序的红黑树 560 //set<int> a; //less<int>是默认的函数对象 561 set<int, less<int>> a; //less<int>是默认的函数对象 562 //set<int, greater<int>> a; 563 564 a.insert(9); 565 a.insert(3); 566 a.insert(8); 567 a.insert(1); 568 a.insert(5); 569 a.insert(11); 570 571 set<int,less<int>>::iterator Iter = a.begin(); 572 573 574 575 while (Iter != a.end()) 576 { 577 cout << *Iter << endl; 578 Iter++; 579 } 580 581 //for_each(a.begin(), a.end(), 函数对象); 582 cout << "-------------------" << endl; 583 for_each(a.begin(), a.end(), printt);*/ 584 585 586 587 // vector<int> ivec; 588 // for (int i = 1; i <= 9;i++) 589 // { 590 // ivec.push_back(i); 591 // } 592 // 593 // ivec.push_back(4); 594 // ivec.push_back(4); 595 // ivec.push_back(4); 596 // ivec.push_back(4); 597 // 598 // 599 // 600 // int num = count(ivec.begin(), ivec.end(), 4); 601 // cout << num << "个4" << endl; 602 // cout << "-----------" << endl; 603 // 604 // vector<string> temp; 605 // temp.push_back("aaaaa"); 606 // temp.push_back("aaaaa"); 607 // temp.push_back("bbbbb"); 608 // temp.push_back("ccccc"); 609 // 610 // num = count(temp.begin(), temp.end(), "aaaaa"); 611 // cout << num << "个 aaaaa" << endl; 612 613 614 615 //min or max 616 // vector<int> temp; 617 // temp.clear(); 618 // for (int i = -10; i <= 10; i++) 619 // temp.push_back(i); 620 // 621 // 622 // vector<int>::iterator Iter = temp.begin(); 623 // //while (Iter != temp.end() ) 624 // //{ 625 // // cout << *Iter << endl; 626 // // Iter++; 627 // //} 628 // 629 // cout << *max_element(temp.begin(), temp.end()) << endl; 630 631 632 633 /* 634 list<int> ilist; 635 for (int i = 0; i < 10;i++) 636 { 637 ilist.insert(ilist.end(), i); 638 } 639 for (int i = 0; i < 10; i++) 640 { 641 ilist.insert(ilist.end(), i); 642 } 643 644 list<int>::iterator Iter = ilist.begin(); 645 646 list<int>::iterator IterPos; 647 IterPos = find(ilist.begin(), ilist.end(), 4); 648 */ 649 650 651 /* 652 string s("asdfaslasdfkljhlkj"); 653 string::size_type pos = s.find("asd"); 654 if (pos != string::npos) 655 { 656 cout << "找到了" << endl; 657 cout << pos << endl; 658 } 659 else 660 { 661 cout << "没找到" << endl; 662 } 663 */ 664 665 666 667 668 669 670 671 672 673 674 /* 675 deque<int> idep; 676 for (int i = 1; i < 10;i++) 677 { 678 idep.push_back(i); 679 } 680 681 682 deque<int>::iterator Iter = idep.begin(); 683 684 search()*/ 685 686 687 688 689 690 //交换算法 691 692 /* 693 vector<int> ivec; 694 deque<int> ideq; 695 ivec.clear(); 696 ideq.clear(); 697 for (int i = 0; i <= 9;i++) 698 { 699 ivec.push_back(i); 700 } 701 for (int i = 11; i <= 15; i++) 702 { 703 ideq.push_back(i); 704 } 705 706 707 708 709 swap_ranges(ivec.begin(), ivec.end(), ideq.begin()); 710 711 712 713 for (vector<int>::iterator Iter = ivec.begin(); Iter != ivec.end(); Iter++) 714 { 715 cout << *Iter << endl; 716 } 717 cout << "-------------------------" << endl; 718 for (deque<int>::iterator Iter = ideq.begin(); Iter != ideq.end(); Iter++) 719 { 720 cout << *Iter << endl; 721 }*/ 722 723 724 725 /* 726 vector<int> ivec1; 727 vector<int> ivec2; 728 729 for (int i = 0; i <= 9; i++) 730 { 731 ivec1.push_back(i); 732 } 733 for (int i = 11; i <= 30; i++) 734 { 735 ivec2.push_back(i); 736 } 737 738 739 ivec1.swap(ivec2); 740 741 742 for (vector<int>::iterator Iter = ivec1.begin(); Iter != ivec1.end(); Iter++) 743 { 744 cout << *Iter << endl; 745 } 746 cout << "-------------------------" << endl; 747 for (vector<int>::iterator Iter = ivec2.begin(); Iter != ivec2.end(); Iter++) 748 { 749 cout << *Iter << endl; 750 }*/ 751 752 753 /* 754 list<string> slist; 755 slist.push_back("hello"); 756 slist.push_back("hi"); 757 slist.push_back("good morning"); 758 759 760 replace(slist.begin(), slist.end(), "hello", "Doris"); 761 for (list<string>::iterator Iter = slist.begin(); Iter != slist.end();Iter++) 762 { 763 cout << *Iter << endl; 764 } 765 */ 766 767 768 769 /* 770 int source[] = { 1, 2, 2, 3, 12, 3, 1343665, 78, 875, 4, 5, 34, 5, 87, 4, 67341 }; 771 int Num = sizeof(source) / sizeof(source[0]); 772 cout << Num << endl; 773 774 775 list<int> ilist; 776 copy(source, source + Num, back_inserter(ilist)); 777 778 list<int>::iterator Iter = ilist.begin(); 779 while (Iter != ilist.end()) 780 { 781 cout << *Iter << " "; 782 Iter++; 783 } 784 cout << endl; 785 unique(ilist.begin(), ilist.end()); 786 for (list<int>::iterator Iter = ilist.begin(); Iter != ilist.end();Iter++) 787 { 788 cout << *Iter << " "; 789 } 790 */ 791 792 793 794 795 /* 796 string name("AnnaBella"); 797 string::size_type pos1 = name.find("nn"); 798 799 if (string::npos == pos1) 800 cout << "没找到,返回特定的code, string::npos" << endl; 801 else 802 cout << "找到了,下标是" << pos1 << endl;*/ 803 804 805 /* 806 807 string name = "r2d3"; 808 string numerics("tew9993"); 809 string::size_type pos = name.find_first_of(numerics); 810 if (string::npos == pos) 811 { 812 cout << "not find" << endl; 813 } 814 else 815 { 816 cout << "find " << endl; 817 }*/ 818 819 820 /* 821 string str = "C++ Primer 3rd ed"; 822 str.replace(11, 3, "4Fourth"); 823 cout << str << endl;*/ 824 825 826 827 828 829 /* 830 int arr[] = { 9, 1, 23, 4 ,8}; 831 int *p = arr; 832 833 cout << *arr << endl; //数组名就是指针 834 cout << *p << endl; 835 836 p = arr; 837 cout << *p << endl; 838 p = arr + 4; //指针or数组名 移动 839 cout << *p << endl; 840 841 p = p + 11; //越界 842 cout << *p << endl; 843 844 845 int *p1 = arr; 846 int *p2 = arr + 4; 847 ptrdiff_t n = p1 - p2; //ptrdiff_t 类型专门用于计算 2个指针之间的距离...只对指针有效,对数组名无效 848 cout << n << endl; 849 850 851 852 cout << "开始循环:" << endl; 853 const size_t arr_sz = 5; 854 int int_arr[arr_sz] = { 0, 1, 2, 3, 4 }; //长度是常量,所有会定义const arr_sz 855 856 //逗号表达式,最后1个值是固定的,类似迭代器的写法了,这里的pend相当于是数组的尾后地址..C++允许的...需要好好理解会 857 for (int *pBegin = int_arr, *pend = int_arr + arr_sz; pBegin != pend;++pBegin) 858 { 859 cout << *pBegin << endl; 860 }*/ 861 862 863 864 865 866 /* 867 int arr[] = { 9, 1, 23, 4, 8 }; 868 int *p = arr; 869 870 871 cout << arr << endl; 872 cout << p << endl; 873 874 p = arr + 3; 875 cout << p << " " << *p << " " << &(arr[3]) << endl; //的确是的:指针移动了,P就指向了新的地址*/ 876 877 878 879 /* 880 double temp = 1.2; 881 double *p = &temp; 882 883 884 const double temp2 = 2.3; 885 //p = &temp2; //普通指针不能指向常量指针 886 const double *p2; 887 p2 = &temp2; //常指针指向常对象 888 889 p2 = &temp; //常对象可以指向普通对象*/ 890 891 892 893 /* 894 string s("hello"); //可以修改,C++风格字符串 没有 895 896 char ca3[] = "hello"; //可以修改,C风格字符串 有隐含的 897 char ca2[] = { 'C', '+', '+', NULL }; //这也是C风格字符串 = NULL 2者等价 898 899 char ca1[] = { 'C', '+', '+' }; //这是字符数组,不是字符串,因为没有 900 901 902 char *cp2 = "panzhengming"; //指向字符串的指针 903 904 int n = 0; 905 while (*cp2) 906 { 907 n++; 908 cp2++; 909 910 if (n == 3) 911 { 912 cout << *cp2 << " "; 913 break; 914 } 915 916 917 } 918 919 false; 920 921 cout << strlen(cp2) << endl;*/ 922 923 924 925 926 927 /* 928 vector<vector<int>> temp; 929 temp.clear(); 930 931 vector<int> a; 932 a.clear(); 933 for (int i = 0; i < 10;i++) 934 { 935 a.push_back(i); 936 } 937 temp.push_back(a); 938 939 940 vector<vector<int>>::iterator Iter = temp.begin(); 941 while (Iter != temp.end()) 942 { 943 cout << *Iter << endl; 944 }*/ 945 946 947 948 949 950 /* 951 //命令行参数 952 cout << "一共" << argc << "个参数" << endl; 953 954 cout << argv[0] << endl; 955 cout << argv[1] << endl; 956 cout << argv[2] << endl; 957 cout << argv[3] << endl; 958 959 //但是一定要注意 argv的越界,空字符串的 特殊情况 960 */ 961 962 963 964 965 966 /* 967 这个列子很有意思,2次引用 968 int a = 1; 969 int &b = add_one(a); 970 b++; 971 cout << a << " " << b << endl;*/ 972 973 974 975 976 //hash_map 977 978 979 /* 980 注意这样取值是容易错误的 981 982 983 map<int, int> temp; 984 temp.clear(); 985 int str = temp[2]; 986 cout << str << endl; 987 */ 988 989 990 991 992 /*这个有点吊了,新标准的C++支持unicode和编译器设置 就可以使用 中文变量了 993 994 unsigned char 潘正明; 995 潘正明 = 123; 996 cout << int(潘正明) << endl; 997 998 */ 999 1000 1001 1002 /* 1003 位元算 1004 unsigned int bits = 02; 1005 cout << bits << endl; 1006 1007 bits = ~bits; 1008 cout << bits << endl; 1009 */ 1010 1011 1012 /* 1013 unsigned char b1 = 0145; 1014 unsigned char b2 = 0257; 1015 unsigned char result = b1 | b2; 1016 cout << (int)result << endl;*/ 1017 1018 1019 /*bitset<32> temp; 1020 int nSize = temp.size(); 1021 for (int i = 0; i < nSize;i = i + 2) 1022 { 1023 temp.set(i); 1024 } 1025 */ 1026 1027 1028 /* 1029 bitset<3> bs(7);//用 7的二进制填充bs对象 1030 cout << bs[0] << " " << bs[1] << " " << bs[2] << endl; 1031 //cout << bs[3] << endl;//运行时异常 1032 1033 1034 string strVal = "011"; 1035 bitset<3> bs1(strVal);//记住 string是反向填充 bitset对象 1036 cout << bs1[0] << " " << bs1[1] << " " << bs1[2] << endl; 1037 cout << bs1 << endl;//也可以直接输出bitset对象*/ 1038 1039 1040 /* 1041 二进制 1042 1043 cout << "-------------------------------------------->开始执行程序啦" << endl; 1044 int *p = new int(111); 1045 delete p; 1046 p = 0; 1047 cout << p << endl; 1048 */ 1049 1050 /* 1051 sizeof操作 1052 1053 int nLen = 11; 1054 cout << sizeof(nLen) << endl; 1055 cout << sizeof(int) << endl; 1056 1057 int x[] = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 }; 1058 1059 for (int i = 0;i<sizeof(x)/sizeof(int);i++) 1060 { 1061 cout << x[i] << " "; 1062 } 1063 1064 cout <<endl << "自定义数据类型Student的存储结构大小: " << sizeof(Student) << endl;*/ 1065 1066 1067 /*逗号表达式 1068 int x[] = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 }; 1069 for (int k = 0, cnt = 10; k < 10;++k,--cnt) 1070 { 1071 x[k] = cnt; 1072 } 1073 1074 //类似 1075 int cnt = 10; 1076 for (int k = 0; k < 10;++k) 1077 { 1078 x[k] = cnt; 1079 cnt--; 1080 }*/ 1081 1082 /* 1083 1084 //枚举可以直接打印 1085 cout << nAppend << endl; 1086 cout << nY << endl; 1087 cout << nZ << endl; 1088 */ 1089 1090 /* 1091 指针属于非引用参数 1092 int a = 10; 1093 AddThree(&a); 1094 */ 1095 1096 1097 /* 1098 二维数组 1099 int m[][10] = 1100 { 1101 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 1102 { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, 1103 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 } 1104 }; 1105 1106 printValue_5(m, 3); 1107 */ 1108 1109 1110 /*递归 ,普通的循环就是迭代,遍历的意思 1111 //DoA(); 1112 int nRet = Factorial(5); 1113 cout << nRet << endl; 1114 for (int i = 0; i <= 10;i++) 1115 { 1116 cout << i<<"! = "<<Factorial(i) << endl; 1117 } 1118 */ 1119 1120 1121 1122 /* 1123 cout << "****************" << endl; 1124 cout << ::atoi("") << endl;//给""字符串,那么返回的是0 1125 */ 1126 1127 1128 1129 1130 /*STL与指针 1131 vector<string*> spvec; 1132 string str; 1133 cout << "Enter some strings (Ctrl+Z to end)" << endl; 1134 while (cin >> str) 1135 { 1136 string *pstr = new string; 1137 *pstr = str; 1138 spvec.push_back(pstr); 1139 } 1140 1141 cout << "现在开始输出:" << endl; 1142 vector<string*>::iterator Iter = spvec.begin(); 1143 while (Iter != spvec.end()) 1144 { 1145 cout << *Iter << " " << **Iter << endl; 1146 1147 ++Iter; 1148 } 1149 1150 1151 //内存管理,释放内存 1152 Iter = spvec.begin();//当前的Iter不是第一次声明的那个Iter .移动过了,所以要重新获取 1153 while (Iter != spvec.end()) 1154 { 1155 delete *Iter; 1156 Iter++; 1157 } 1158 1159 */ 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 //以下是WindowsAPI编程 1172 1173 1174 //这个很有趣,哈哈哈 1175 //MessageBox(NULL, TEXT("panzhengming"), TEXT("命令行直接调用WindowsAPI"), MB_OK); 1176 /* 1177 LPSTR szString = "windows data type ,string"; 1178 CHAR lpString[120]; 1179 CopyMemory(lpString, szString, lstrlen(szString) + 1); 1180 MessageBox(NULL, lpString, "aaa", MB_OK); 1181 */ 1182 1183 /*读取文件 1184 HANDLE hFileRead; 1185 hFileRead = CreateFile(L"1.cpp", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 1186 if (hFileRead == INVALID_HANDLE_VALUE) 1187 { 1188 printf("Can not open file ..error: %x ",GetLastError()); 1189 system("pause"); 1190 return -1; 1191 } 1192 1193 const int buf_size = 256; 1194 CHAR buffer[buf_size]; 1195 DWORD nIn; 1196 while (ReadFile(hFileRead, buffer, buf_size, &nIn, NULL) && nIn > 0) 1197 { 1198 printf("%s ", buffer); 1199 } 1200 1201 */ 1202 1203 1204 1205 /* 1206 //读取文件属性 1207 WIN32_FILE_ATTRIBUTE_DATA wData; 1208 if (!GetFileAttributesEx(L"1.cpp", GetFileExInfoStandard, &wData)) 1209 { 1210 system("pause"); 1211 return -1; 1212 } 1213 1214 PFILETIME lptime = &wData.ftCreationTime; 1215 FILETIME ftLocal; 1216 SYSTEMTIME st; 1217 FileTimeToLocalFileTime(lptime, &ftLocal); 1218 FileTimeToSystemTime(&ftLocal, &st); 1219 printf("%d年%d月%d日, %d时%d分%d秒", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); 1220 //cout << wData.ftCreationTime << " " << wData.ftLastAccessTime << " " << wData.ftLastWriteTime << endl; 1221 printf(" %d",wData.ftCreationTime); 1222 */ 1223 1224 1225 /* 1226 //获取文件目录 1227 TCHAR szSystemDir[MAX_PATH]; //MAX_PATH windows定义目录长度最多260 1228 GetSystemDirectory(szSystemDir, MAX_PATH); 1229 printf("%s", szSystemDir);//--获取windows的系统安装目录,因为有人可能装在别的盘符里 1230 */ 1231 1232 1233 /* 1234 //宽字节 1235 char只能存储 ASIC英文字符,不能保存中文字符,也就可以保存其他国家的字符 1236 使用printf()输出 1237 wchar_t c2 = L'中'; L不能省略,表示宽字符,新编的C语言有wchar_t类型 1238 使用wprintf(L"%c",c2)输出 1239 --------------------------------------- 1240 1241 1242 1243 setlocale(LC_ALL, "");//使用宽字符输出的时候,要设置本地化设置,头文件是 #include <locale.h> 1244 wchar_t c2 = L'中'; 1245 wprintf(L"%c", c2);//好像是2个字符算1个 1246 1247 */ 1248 1249 1250 1251 /* 1252 ansi 和 unicode 相互转化 1253 1254 setlocale(LC_ALL, "");//本地区域,这样就可以使用 unicode 1255 DWORD dwNum; 1256 char sText[] = "ANSI多字节字符串 转 宽字符串"; 1257 dwNum = MultiByteToWideChar(CP_ACP, 0, sText, -1, NULL, 0);//先计算需要多大的存储空间 1258 wchar_t *pwText; 1259 pwText = new wchar_t[dwNum]; 1260 if (!pwText) 1261 { 1262 delete[] pwText; 1263 return 1; 1264 } 1265 MultiByteToWideChar(CP_ACP, 0, sText, -1, NULL, dwNum); 1266 wprintf(L"m->w:%s", pwText); 1267 MessageBoxW(NULL, pwText, L"Text", MB_OK); 1268 delete[] pwText; 1269 1270 1271 wchar_t wText[] = L"宽字符串 转 多字节字符串"; 1272 dwNum = WideCharToMultiByte(CP_OEMCP, 0, wText, -1, NULL, 0, NULL, NULL); 1273 char *psText; 1274 psText = new char[dwNum]; 1275 if (!psText) 1276 { 1277 delete[] psText; 1278 return 2; 1279 } 1280 WideCharToMultiByte(CP_OEMCP, 0, wText, -1, psText, dwNum, NULL, NULL); 1281 printf(" w->m %s", psText); 1282 MessageBoxA(NULL, psText, "Text", MB_OK); 1283 */ 1284 1285 1286 1287 /* 1288 1289 遍历逻辑卷,方法1 逻辑卷名 1290 1291 const int bufsize = 1024; 1292 wchar_t szLogicalDriverStrings[bufsize]; 1293 ZeroMemory(szLogicalDriverStrings, bufsize); 1294 1295 GetLogicalDriveStrings(bufsize - 1, szLogicalDriverStrings); 1296 //printf("%s ", szLogicalDriverStrings);//这个是不能正常打印szLogicalDriverStrings ,因为szLogicalDriverStrings的字符格式里面包含 ,所以printf()只能显示第一个盘符 1297 PCHAR szDrive; 1298 szDrive = (PCHAR)szLogicalDriverStrings; 1299 do 1300 { 1301 printf("%s ", szDrive); 1302 szDrive = szDrive + 4; //这个地方是我手动写的,我转化不了下面的字符类型 1303 //szDrive = szDrive + (lstrlen(szDrive) + 1); 1304 } while (*szDrive != ' '); 1305 */ 1306 1307 1308 /* 1309 1310 遍历逻辑卷,方法2 设备名 1311 1312 1313 const int buf_size = 1024; 1314 TCHAR buf[buf_size]; 1315 HANDLE hVol; 1316 bool bFlag; 1317 1318 hVol = FindFirstVolume(buf, buf_size); 1319 if (hVol == INVALID_HANDLE_VALUE) 1320 { 1321 printf("no found volumes ! "); 1322 return 1; 1323 } 1324 1325 printf("%s ",buf); 1326 1327 */ 1328 1329 1330 /* 1331 驱动器属性 1332 GetDriveType(); 1333 GetVolumeInformation(); 1334 1335 这2个API没写代码,很简单,不会就百度下.感觉很好理解这个 1336 */ 1337 1338 /* 1339 磁盘容量 1340 磁盘->簇->扇区->字节 1341 1342 1343 1344 DWORD dwTotalClusters; 1345 DWORD dwFreeClusters; 1346 1347 DWORD dwSectPerCulust; 1348 DWORD dwBytesPerSect; 1349 1350 //需要传入盘符号 1351 BOOL ret = GetDiskFreeSpace(TEXT("c:"), &dwSectPerCulust, &dwBytesPerSect, &dwFreeClusters, &dwTotalClusters); 1352 if (!ret) 1353 { 1354 printf("11111111"); 1355 } 1356 printf(" 使用GetDiskFreeSpace()uhoqu磁盘空间信息: "); 1357 printf("总的簇数量: %d ", dwTotalClusters); 1358 printf("空闲的簇数量: %d ", dwFreeClusters); 1359 printf("每个簇的扇区数量是: %d ", dwSectPerCulust); 1360 printf("每个扇区字节数是: %d ", dwBytesPerSect); 1361 1362 //GetDiskFreeSpaceEx(); ke 1363 */ 1364 1365 1366 1367 /* 1368 文件操作的4种方式 1369 1.C 1370 2.C++ 1371 3.API 1372 4.MFC 1373 */ 1374 1375 1376 /* 1377 1378 文件的 删除,移动/重命名,拷贝 1379 1380 1381 DeleteFile(); 1382 MoveFile(); 1383 CopyFile() 1384 */ 1385 1386 1387 /* 1388 1389 文件内容拷贝 1390 //c 1391 fopen() 1392 fread() 1393 fwrite() 1394 fclose() 1395 1396 1397 //windows 1398 CreateFile() 1399 ReadFile() 1400 WriteFile() 1401 CopyFile() 1402 1403 */ 1404 1405 1406 1407 /* 1408 目录与模块 1409 1410 1411 1412 CreateDirectory() 1413 GetCurrentDirectory() 1414 SetCurrentDirectory() 1415 GetModuleFileName() 1416 1417 1418 LoadLibrary() 1419 */ 1420 1421 1422 1423 /* 1424 遍历目录 1425 FindFirstFile() 1426 FindNextFile() 1427 WIN32_FIND_DATA 1428 1429 HANDLE 1430 */ 1431 1432 1433 /* 1434 文件属性和时间 1435 FileTimeToLocalFileTime()//文件时间转化成本地时间 1436 FileTimeToSystemTime()//本地时间转化成系统时间 1437 GetFileAttributesEx() 1438 //低位,高位,位元算 1439 1440 1441 GetFileAttributes() 1442 SetFileAttributes() 1443 1444 */ 1445 1446 1447 1448 /* 1449 1450 内存映射文件 1451 1452 HANDEL hFile 1453 CreateFile() 1454 CreateFileMapping() 1455 MapViewOfFile() 1456 1457 1458 */ 1459 1460 1461 1462 1463 1464 /* 1465 1466 系统信息1 1467 1468 OSVERSIONINFO ovex; 1469 TCHAR szVersionInfo[1024]; 1470 *szVersionInfo = NULL; 1471 ovex.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 1472 if (!GetVersionEx(&ovex)) //为啥这个函数编译不过去 1473 { 1474 printf("error "); 1475 return 1; 1476 } 1477 1478 1479 */ 1480 1481 1482 /* 1483 系统信息2 1484 吗的,显示的不完全呀 1485 1486 1487 1488 1489 TCHAR szDirName[MAX_PATH]; 1490 GetSystemDirectory(szDirName,MAX_PATH); 1491 printf("系统目录: %s ", szDirName); //好像不同的版本,目录路径 不一样 1492 1493 GetWindowsDirectory(szDirName, MAX_PATH); 1494 printf("windows目录: %s ", szDirName); //window目录 1495 1496 DWORD dwComputerNameLen = MAX_COMPUTERNAME_LENGTH + 1; 1497 GetComputerName(szDirName, &dwComputerNameLen); //计算机名 1498 printf("计算机名: %s ", szDirName); 1499 1500 GetUserName(szDirName, &dwComputerNameLen); //用户名 1501 printf("用户名: %s ", szDirName); 1502 1503 1504 BOOL fret; 1505 int aMouseInfo[3]; 1506 fret = SystemParametersInfo(SPI_GETMOUSE, 0, &aMouseInfo, 0);// spi是外设的意思 1507 if (fret) 1508 { 1509 aMouseInfo[2] = 4 * aMouseInfo[2];//设置鼠标移动速度 1510 SystemParametersInfo(SPI_GETMOUSE, 0, aMouseInfo, SPIF_SENDCHANGE); 1511 } 1512 1513 printf("ok "); 1514 1515 */ 1516 1517 1518 1519 1520 1521 /* 1522 时间 1523 1524 1525 //获取时间 1526 SYSTEMTIME st; 1527 GetLocalTime(&st);//本地时间 1528 printf("Now:%d-%d-%d, %d:%d:%d ", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); 1529 1530 1531 //修改时间,通过结构体字段来实现 1532 st.wHour = 11; 1533 SetLocalTime(&st); 1534 printf("Now:%d-%d-%d, %d:%d:%d ", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); 1535 1536 1537 //开机到现在的持续时间 单位是毫秒 1538 DWORD c1 = GetTickCount(); //这个保存的时间太短,类型决定的 1539 ULONGLONG c2 = GetTickCount64(); //常用的是这个, 高版本的windows会有这个函数 1540 printf("%d %d", c1, c2); 1541 1542 1543 //随机数 1544 const int total = 10000; 1545 int nums[total]; 1546 srand(GetTickCount64()); //设置随机种子..还是不太了解 1547 1548 for (int i = 0; i < total;i++) 1549 { 1550 nums[i] = rand() % 1000; 1551 printf("%d ", nums[i]); 1552 } 1553 1554 1555 //快速排序 1556 //qsort(nums, total, sizeof(nums[0]), func_cmp); 这个没写,暂时不想写 1557 int nStartTime = GetTickCount(); 1558 int nEndTime = GetTickCount(); 1559 1560 */ 1561 1562 1563 /* 1564 注册表 读写 1565 1566 //读 1567 HKEY hKey; 1568 TCHAR tchData[64]; 1569 long lRet = RegOpenKey(HKEY_LOCAL_MACHINE, _T("hardware\description\system\centralprocessor\0"), &hKey); 1570 if (ERROR_SEVERITY_SUCCESS == lRet)//成功 1571 { 1572 DWORD dwSize = sizeof(tchData); 1573 lRet = RegQueryValueEx(hKey, _T("ProcessorNameString"), NULL, NULL, (LPBYTE)tchData, &dwSize); 1574 1575 if (ERROR_SEVERITY_SUCCESS == lRet) 1576 { 1577 printf("%s ", tchData); 1578 } 1579 } 1580 1581 RegCloseKey(hKey); //代码是这样的,但是编译不通过 1582 1583 1584 //写 1585 1586 HKEY hKey; 1587 RegCreateKey(HKEY_LOCAL_MACHINE,_T("xxxx\xxx\xxx"),&hKey); 1588 RegSetValue(hKey, NULL, REG_SZ, _T("Liebao"), 6); //这个API只能添加默认的 1589 DWORD dwAge = 30; 1590 RegSetValueEx(hKey, _T("新增的用RegSetValueEx()"), 0, REG_DWORD, (CONST BYTE*)&dwAge, 4); //这个可以新增新的key 1591 RegCloseKey(hKey); 1592 1593 */ 1594 1595 1596 1597 1598 /* 1599 内存信息 1600 1601 1602 MEMORYSTATUSEX memstatsex; 1603 memstatsex.dwLength = sizeof(memstatsex); 1604 1605 GlobalMemoryStatusEx(&memstatsex); 1606 printf("全部物理内存: %I64u 字节 ", memstatsex.ullTotalPhys); 1607 printf("可用物理内存: %I64u 字节 ", memstatsex.ullAvailPhys); 1608 printf("全部虚拟内存: %I64u 字节 ", memstatsex.ullTotalVirtual); 1609 printf("可用虚拟内存: %I64u 字节 ", memstatsex.ullAvailVirtual); 1610 printf("全部页面文件: %I64u 字节 ", memstatsex.ullTotalPageFile); 1611 printf("可用页面文件: %I64u 字节 ", memstatsex.ullAvailPageFile); 1612 1613 1614 */ 1615 1616 /* 1617 系统信息 1618 SYSTEM_INFO sinf; 1619 GetSystemInfo(&sinf); 1620 */ 1621 1622 1623 1624 1625 1626 /* 1627 1628 堆内存 1629 1630 1631 SYSTEM_INFO si; 1632 HANDLE hHeap1, hHeap2; 1633 DWORD dwHeapNum; 1634 1635 GetSystemInfo(&si); 1636 hHeap1 = HeapCreate(HEAP_NO_SERIALIZE, si.dwPageSize * 2, si.dwPageSize * 10);//堆的页面最小,最大设置 1637 if (hHeap1 == NULL) 1638 { 1639 printf("创建堆失败 "); 1640 return 1; 1641 } 1642 printf("创建堆成功,初识页面2页,最大是10页 "); 1643 1644 1645 hHeap2 = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);//如果不写明的,初始的大小向上取值,最大不限制 1646 printf("创建堆成功,初识页面1页,最大不限制 "); 1647 1648 dwHeapNum = GetProcessHeaps(0, NULL); 1649 if (0 == dwHeapNum) 1650 { 1651 printf("进程至少有个默认堆"); 1652 return 1; 1653 } 1654 cout << dwHeapNum << endl; 1655 1656 1657 1658 PVOID lpMem1, lpMem2; 1659 lpMem1 = HeapAlloc(hHeap1, HEAP_ZERO_MEMORY, si.dwPageSize * 3); 1660 if (lpMem1 == NULL) 1661 { 1662 printf("error"); 1663 return 1; 1664 } 1665 1666 printf("在堆上分配内存成功,起始地址: 0x%x ", lpMem1); 1667 1668 1669 */ 1670 1671 1672 1673 1674 /* 1675 内存的常用操作 1676 */ 1677 1678 1679 1680 1681 1682 1683 1684 //算法 1685 /* 1686 1687 冒泡 1688 1689 1690 { 1691 1692 const int nSize = 10; 1693 //int list[nSize] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 }; 1694 int list[nSize] = { 11, 33, 55, 77, 999, 222, 44, 66, 88, 10 }; 1695 1696 1697 int a = sizeof(list)/sizeof(int); 1698 1699 1700 cout << "原始数据: "; 1701 for (int i = 0; i < nSize; i++) 1702 { 1703 cout << list[i] << " "; 1704 } 1705 cout << endl; 1706 cout << endl; 1707 1708 //外层循环是趟数 1709 for (int n = 0; n < nSize;n++) 1710 { 1711 cout << "第" << n + 1 << "趟: "; 1712 1713 //内层循环比较大小 1714 for (int i = 0; i < nSize - 1;i++) 1715 { 1716 if (list[i] > list[i+1]) 1717 { 1718 swap(list[i], list[i + 1]); 1719 } 1720 } 1721 1722 for (int i = 0; i < nSize; i++) 1723 { 1724 cout << list[i] << " "; 1725 } 1726 1727 cout << endl; 1728 1729 } 1730 cout << "-----------------------" << endl; 1731 for (int i = 0; i < nSize;i++) 1732 { 1733 cout << list[i] << " "; 1734 } 1735 1736 1737 } 1738 1739 1740 */ 1741 1742 1743 1744 1745 1746 /* 1747 #define 的用法,3个特殊的符号 1748 # 1749 ## 1750 #@ 1751 */ 1752 1753 1754 1755 //用指针访问对象的成员 1756 TestObject *p = new TestObject; 1757 cout << sizeof(p) << endl; 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 cout << endl; 1922 system("pause"); 1923 return 0; 1924 }