GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — STLSourceCodeNote/sample.cpp at master · CornPrincess/STLSourceCodeNote Expand file tree/ Copy pathsample.cppMore file actions 1924 lines (1719 loc) · 114 KB / Copy pathsample.cppFile metadata and controls1924 lines (1719 loc) · 114 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 // author : Hou Jie (侯捷) // date : 2015/10/28 // compiler : DevC++ 5.11 (MinGW with GNU 4.9.9.2) // // 說明:這是侯捷 E-learning video "C++標準庫" 的實例程式. // 該課程的所有測試都出現在此. // 每一個小測試單元都被放進一個 namespace 中, // 如此保持各單元間最大的獨立性. // 每個 namespace 上方皆有該單元相應的 #include <...> // 因此有可能整個程式重複含入 (included) 某些 headers. // 這無所謂,因為每個 standard headers 都有自我防衛機制,不讓自己被 included 二次. // // 本文件用到若干 C++ 2.0 New Features,所以你必須在你的集成環境 (IDE) 上設定 "C++11 on". // 如果你的編譯器報錯,應是因為我用到 (測試) GNU 標準庫的獨特 (非標準) 組件 (特別是 allocators), // 將報錯語句改為註解 (comments) 即可通過編譯. using namespace std; const long ASIZE = 500000L; //---------------------------------------------------- #include <iostream> #include <cstdio> //snprintf() #include <cstdlib> //RAND_MAX #include <cstring> //strlen(), memcpy() #include <string> using std::cin; using std::cout; using std::string; //以下 MyString 是為了測試 containers with moveable elements 效果. class MyString { public: static size_t DCtor; //累計 default-ctor 的呼叫次數 static size_t Ctor; //累計 ctor 的呼叫次數 static size_t CCtor; //累計 copy-ctor 的呼叫次數 static size_t CAsgn; //累計 copy-asgn 的呼叫次數 static size_t MCtor; //累計 move-ctor 的呼叫次數 static size_t MAsgn; //累計 move-asgn 的呼叫次數 static size_t Dtor; //累計 dtor 的呼叫次數 private: char* _data; size_t _len; void _init_data(const char *s) { _data = new char[_len+1]; memcpy(_data, s, _len); _data[_len] = '\0'; } public: //default ctor MyString() : _data(NULL), _len(0) { ++DCtor; } //ctor MyString(const char* p) : _len(strlen(p)) { ++Ctor; _init_data(p); } // copy ctor MyString(const MyString& str) : _len(str._len) { ++CCtor; _init_data(str._data); //COPY } //move ctor, with "noexcept" MyString(MyString&& str) noexcept : _data(str._data), _len(str._len) { ++MCtor; str._len = 0; str._data = NULL; //避免 delete (in dtor) } //copy assignment MyString& operator=(const MyString& str) { ++CAsgn; if (this != &str) { if (_data) delete _data; _len = str._len; _init_data(str._data); //COPY! } else { // Self Assignment, Nothing to do. } return *this; } //move assignment MyString& operator=(MyString&& str) noexcept { ++MAsgn; if (this != &str) { if (_data) delete _data; _len = str._len; _data = str._data; //MOVE! str._len = 0; str._data = NULL; //避免 deleted in dtor } return *this; } //dtor virtual ~MyString() { ++Dtor; if (_data) { delete _data; } } bool operator<(const MyString& rhs) const //為了讓 set 比較大小 { return std::string(this->_data) < std::string(rhs._data); //借用事實:string 已能比較大小. } bool operator==(const MyString& rhs) const //為了讓 set 判斷相等. { return std::string(this->_data) == std::string(rhs._data); //借用事實:string 已能判斷相等. } char* get() const { return _data; } }; size_t MyString::DCtor=0; size_t MyString::Ctor=0; size_t MyString::CCtor=0; size_t MyString::CAsgn=0; size_t MyString::MCtor=0; size_t MyString::MAsgn=0; size_t MyString::Dtor=0; namespace std //必須放在 std 內 { template<> struct hash<MyString> //這是為了 unordered containers { size_t operator()(const MyString& s) const noexcept { return hash<string>()(string(s.get())); } //借用現有的 hash<string> (in ...\include\c++\bits\basic_string.h) }; } //----------------- //以下 MyStrNoMove 是為了測試 containers with no-moveable elements 效果. class MyStrNoMove { public: static size_t DCtor; //累計 default-ctor 的呼叫次數 static size_t Ctor; //累計 ctor 的呼叫次數 static size_t CCtor; //累計 copy-ctor 的呼叫次數 static size_t CAsgn; //累計 copy-asgn 的呼叫次數 static size_t MCtor; //累計 move-ctor 的呼叫次數 static size_t MAsgn; //累計 move-asgn 的呼叫次數 static size_t Dtor; //累計 dtor 的呼叫次數 private: char* _data; size_t _len; void _init_data(const char *s) { _data = new char[_len+1]; memcpy(_data, s, _len); _data[_len] = '\0'; } public: //default ctor MyStrNoMove() : _data(NULL), _len(0) { ++DCtor; _init_data("jjhou"); } //ctor MyStrNoMove(const char* p) : _len(strlen(p)) { ++Ctor; _init_data(p); } // copy ctor MyStrNoMove(const MyStrNoMove& str) : _len(str._len) { ++CCtor; _init_data(str._data); //COPY } //copy assignment MyStrNoMove& operator=(const MyStrNoMove& str) { ++CAsgn; if (this != &str) { if (_data) delete _data; _len = str._len; _init_data(str._data); //COPY! } else { // Self Assignment, Nothing to do. } return *this; } //dtor virtual ~MyStrNoMove() { ++Dtor; if (_data) { delete _data; } } bool operator<(const MyStrNoMove& rhs) const //為了讓 set 比較大小 { return string(this->_data) < string(rhs._data); //借用事實:string 已能比較大小. } bool operator==(const MyStrNoMove& rhs) const //為了讓 set 判斷相等. { return string(this->_data) == string(rhs._data); //借用事實:string 已能判斷相等. } char* get() const { return _data; } }; size_t MyStrNoMove::DCtor=0; size_t MyStrNoMove::Ctor=0; size_t MyStrNoMove::CCtor=0; size_t MyStrNoMove::CAsgn=0; size_t MyStrNoMove::MCtor=0; size_t MyStrNoMove::MAsgn=0; size_t MyStrNoMove::Dtor=0; namespace std //必須放在 std 內 { template<> struct hash<MyStrNoMove> //這是為了 unordered containers { size_t operator()(const MyStrNoMove& s) const noexcept { return hash<string>()(string(s.get())); } //借用現有的 hash<string> (in ...\4.9.2\include\c++\bits\basic_string.h) }; } //---------------------------------------------------- #include <iostream> #include <cstdio> //snprintf() #include <cstdlib> //RAND_MAX #include <string> #include <algorithm> #include <list> #include <forward_list> using std::cin; using std::cout; using std::string; using std::max; using std::min; namespace jj00 { bool strLonger(const string& s1, const string& s2) { return s1.size() < s2.size(); } void test_misc() { cout << "\ntest_misc().......... \n"; //以下這些是標準庫的眾多容器的 max_size() 計算方式. cout << size_t(-1) << endl; //4294967295 cout << size_t(-1)/sizeof(long) << endl; //1073741823 cout << size_t(-1)/sizeof(string) << endl; //1073741823 cout << size_t(-1)/sizeof(_List_node<string>) << endl; //357913941 cout << size_t(-1)/sizeof(_Fwd_list_node<string>) << endl; //536870911 cout << "RAND_MAX= " << RAND_MAX << endl; //32767 cout << min( {2,5,8,9,45,0,81} ) << endl; //0 cout << max( {2,5,8,9,45,0,81} ) << endl; //81 vector<int> v {2,5,8,9,45,0,81}; cout << "max of zoo and hello : " << max(string("zoo"), string("hello")) << endl; //zoo cout << "longest of zoo and hello : " << max(string("zoo"), string("hello"), strLonger) << endl; //hello cout << hash<MyString>()(MyString("Ace")) << endl; //1765813650 cout << hash<MyString>()(MyString("Stacy")) << endl; //2790324277 cout << "MyString(zoo) < MyString(hello) ==> " << (MyString("zoo") < MyString("hello")) << endl; //0 cout << "MyString(zoo) == MyString(hello) ==> " << (MyString("zoo") == MyString("hello")) << endl; //0 cout << "MyStrNoMove(zoo) < MyStrNoMove(hello) ==> " << (MyStrNoMove("zoo") < MyStrNoMove("hello")) << endl; //0 cout << "MyStrNoMove(zoo) == MyStrNoMove(hello) ==> " << (MyStrNoMove("zoo") == MyStrNoMove("hello")) << endl; //0 //以上建構了 6 個 MyString objects 和 4 個 MyStrNoMove objects,都是暫時生命. } } //-------------------------------------------------- #include <typeinfo> //typeid() template<typename T> void output_static_data(const T& myStr) { cout << typeid(myStr).name() << " -- " << endl; cout << " CCtor=" << T::CCtor << " MCtor=" << T::MCtor << " CAsgn=" << T::CAsgn << " MAsgn=" << T::MAsgn << " Dtor=" << T::Dtor << " Ctor=" << T::Ctor << " DCtor=" << T::DCtor << endl; } #include <ctime> //clock_t, clock() template<typename M, typename NM> void test_moveable(M c1, NM c2, long& value) { char buf[10]; //測試 move cout << "\n\ntest, with moveable elements" << endl; typedef typename iterator_traits<typename M::iterator>::value_type V1type; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { snprintf(buf, 10, "%d", rand()); auto ite = c1.end(); c1.insert(ite, V1type(buf)); } cout << "construction, milli-seconds : " << (clock()-timeStart) << endl; cout << "size()= " << c1.size() << endl; output_static_data(*(c1.begin())); timeStart = clock(); M c11(c1); cout << "copy, milli-seconds : " << (clock()-timeStart) << endl; timeStart = clock(); M c12(std::move(c1)); cout << "move copy, milli-seconds : " << (clock()-timeStart) << endl; timeStart = clock(); c11.swap(c12); cout << "swap, milli-seconds : " << (clock()-timeStart) << endl; //測試 non-moveable cout << "\n\ntest, with non-moveable elements" << endl; typedef typename iterator_traits<typename NM::iterator>::value_type V2type; timeStart = clock(); for(long i=0; i< value; ++i) { snprintf(buf, 10, "%d", rand()); auto ite = c2.end(); c2.insert(ite, V2type(buf)); } cout << "construction, milli-seconds : " << (clock()-timeStart) << endl; cout << "size()= " << c2.size() << endl; output_static_data(*(c2.begin())); timeStart = clock(); NM c21(c2); cout << "copy, milli-seconds : " << (clock()-timeStart) << endl; timeStart = clock(); NM c22(std::move(c2)); cout << "move copy, milli-seconds : " << (clock()-timeStart) << endl; timeStart = clock(); c21.swap(c22); cout << "swap, milli-seconds : " << (clock()-timeStart) << endl; } //----------------- long get_a_target_long() { long target=0; cout << "target (0~" << RAND_MAX << "): "; cin >> target; return target; } string get_a_target_string() { long target=0; char buf[10]; cout << "target (0~" << RAND_MAX << "): "; cin >> target; snprintf(buf, 10, "%d", target); return string(buf); } int compareLongs(const void* a, const void* b) { return ( *(long*)a - *(long*)b ); } int compareStrings(const void* a, const void* b) { if ( *(string*)a > *(string*)b ) return 1; else if ( *(string*)a < *(string*)b ) return -1; else return 0; } //--------------------------------------------------- #include <array> #include <iostream> #include <ctime> #include <cstdlib> //qsort, bsearch, NULL namespace jj01 { void test_array() { cout << "\ntest_array().......... \n"; array<long,ASIZE> c; clock_t timeStart = clock(); for(long i=0; i< ASIZE; ++i) { c[i] = rand(); } cout << "milli-seconds : " << (clock()-timeStart) << endl; // cout << "array.size()= " << c.size() << endl; cout << "array.front()= " << c.front() << endl; cout << "array.back()= " << c.back() << endl; cout << "array.data()= " << c.data() << endl; long target = get_a_target_long(); timeStart = clock(); ::qsort(c.data(), ASIZE, sizeof(long), compareLongs); long* pItem = (long*)::bsearch(&target, (c.data()), ASIZE, sizeof(long), compareLongs); cout << "qsort()+bsearch(), milli-seconds : " << (clock()-timeStart) << endl; // if (pItem != NULL) cout << "found, " << *pItem << endl; else cout << "not found! " << endl; } } //--------------------------------------------------- #include <vector> #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() #include <iostream> #include <ctime> #include <algorithm> //sort() namespace jj02 { void test_vector(long& value) { cout << "\ntest_vector().......... \n"; vector<string> c; char buf[10]; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { try { snprintf(buf, 10, "%d", rand()); c.push_back(string(buf)); } catch(exception& p) { cout << "i=" << i << " " << p.what() << endl; //曾經最高 i=58389486 then std::bad_alloc abort(); } } cout << "milli-seconds : " << (clock()-timeStart) << endl; cout << "vector.max_size()= " << c.max_size() << endl; //1073747823 cout << "vector.size()= " << c.size() << endl; cout << "vector.front()= " << c.front() << endl; cout << "vector.back()= " << c.back() << endl; cout << "vector.data()= " << c.data() << endl; cout << "vector.capacity()= " << c.capacity() << endl << endl; string target = get_a_target_string(); { timeStart = clock(); auto pItem = find(c.begin(), c.end(), target); cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end()) cout << "found, " << *pItem << endl << endl; else cout << "not found! " << endl << endl; } { timeStart = clock(); sort(c.begin(), c.end()); cout << "sort(), milli-seconds : " << (clock()-timeStart) << endl; timeStart = clock(); string* pItem = (string*)::bsearch(&target, (c.data()), c.size(), sizeof(string), compareStrings); cout << "bsearch(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != NULL) cout << "found, " << *pItem << endl << endl; else cout << "not found! " << endl << endl; } c.clear(); test_moveable(vector<MyString>(),vector<MyStrNoMove>(), value); } } //--------------------------------------------------- #include <list> #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() #include <algorithm> //find() #include <iostream> #include <ctime> namespace jj03 { void test_list(long& value) { cout << "\ntest_list().......... \n"; list<string> c; char buf[10]; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { try { snprintf(buf, 10, "%d", rand()); c.push_back(string(buf)); } catch(exception& p) { cout << "i=" << i << " " << p.what() << endl; abort(); } } cout << "milli-seconds : " << (clock()-timeStart) << endl; cout << "list.size()= " << c.size() << endl; cout << "list.max_size()= " << c.max_size() << endl; //357913941 cout << "list.front()= " << c.front() << endl; cout << "list.back()= " << c.back() << endl; string target = get_a_target_string(); timeStart = clock(); auto pItem = find(c.begin(), c.end(), target); cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end()) cout << "found, " << *pItem << endl; else cout << "not found! " << endl; timeStart = clock(); c.sort(); cout << "c.sort(), milli-seconds : " << (clock()-timeStart) << endl; c.clear(); test_moveable(list<MyString>(),list<MyStrNoMove>(), value); } } //--------------------------------------------------- #include <forward_list> #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() #include <iostream> #include <ctime> namespace jj04 { void test_forward_list(long& value) { cout << "\ntest_forward_list().......... \n"; forward_list<string> c; char buf[10]; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { try { snprintf(buf, 10, "%d", rand()); c.push_front(string(buf)); } catch(exception& p) { cout << "i=" << i << " " << p.what() << endl; abort(); } } cout << "milli-seconds : " << (clock()-timeStart) << endl; cout << "forward_list.max_size()= " << c.max_size() << endl; //536870911 cout << "forward_list.front()= " << c.front() << endl; string target = get_a_target_string(); timeStart = clock(); auto pItem = find(c.begin(), c.end(), target); cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end()) cout << "found, " << *pItem << endl; else cout << "not found! " << endl; timeStart = clock(); c.sort(); cout << "c.sort(), milli-seconds : " << (clock()-timeStart) << endl; c.clear(); } } //--------------------------------------------------- #include <deque> #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() #include <iostream> #include <ctime> namespace jj05 { void test_deque(long& value) { cout << "\ntest_deque().......... \n"; deque<string> c; char buf[10]; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { try { snprintf(buf, 10, "%d", rand()); c.push_back(string(buf)); } catch(exception& p) { cout << "i=" << i << " " << p.what() << endl; abort(); } } cout << "milli-seconds : " << (clock()-timeStart) << endl; cout << "deque.size()= " << c.size() << endl; cout << "deque.front()= " << c.front() << endl; cout << "deque.back()= " << c.back() << endl; cout << "deque.max_size()= " << c.max_size() << endl; //1073741821 string target = get_a_target_string(); timeStart = clock(); auto pItem = find(c.begin(), c.end(), target); cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end()) cout << "found, " << *pItem << endl; else cout << "not found! " << endl; timeStart = clock(); sort(c.begin(), c.end()); cout << "sort(), milli-seconds : " << (clock()-timeStart) << endl; c.clear(); test_moveable(deque<MyString>(),deque<MyStrNoMove>(), value); } } //--------------------------------------------------- #include <set> #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() #include <iostream> #include <ctime> namespace jj06 { void test_multiset(long& value) { cout << "\ntest_multiset().......... \n"; multiset<string> c; char buf[10]; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { try { snprintf(buf, 10, "%d", rand()); c.insert(string(buf)); } catch(exception& p) { cout << "i=" << i << " " << p.what() << endl; abort(); } } cout << "milli-seconds : " << (clock()-timeStart) << endl; cout << "multiset.size()= " << c.size() << endl; cout << "multiset.max_size()= " << c.max_size() << endl; //214748364 string target = get_a_target_string(); { timeStart = clock(); auto pItem = find(c.begin(), c.end(), target); //比 c.find(...) 慢很多 cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end()) cout << "found, " << *pItem << endl; else cout << "not found! " << endl; } { timeStart = clock(); auto pItem = c.find(target); //比 std::find(...) 快很多 cout << "c.find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end()) cout << "found, " << *pItem << endl; else cout << "not found! " << endl; } c.clear(); test_moveable(multiset<MyString>(),multiset<MyStrNoMove>(), value); } } //--------------------------------------------------- #include <map> #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() #include <iostream> #include <ctime> namespace jj07 { void test_multimap(long& value) { cout << "\ntest_multimap().......... \n"; multimap<long, string> c; char buf[10]; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { try { snprintf(buf, 10, "%d", rand()); //multimap 不可使用 [] 做 insertion c.insert(pair<long,string>(i,buf)); } catch(exception& p) { cout << "i=" << i << " " << p.what() << endl; abort(); } } cout << "milli-seconds : " << (clock()-timeStart) << endl; cout << "multimap.size()= " << c.size() << endl; cout << "multimap.max_size()= " << c.max_size() << endl; //178956970 long target = get_a_target_long(); timeStart = clock(); auto pItem = c.find(target); cout << "c.find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end()) cout << "found, value=" << (*pItem).second << endl; else cout << "not found! " << endl; c.clear(); } } //--------------------------------------------------- #include <unordered_set> #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() #include <iostream> #include <ctime> namespace jj08 { void test_unordered_multiset(long& value) { cout << "\ntest_unordered_multiset().......... \n"; unordered_multiset<string> c; char buf[10]; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { try { snprintf(buf, 10, "%d", rand()); c.insert(string(buf)); } catch(exception& p) { cout << "i=" << i << " " << p.what() << endl; abort(); } } cout << "milli-seconds : " << (clock()-timeStart) << endl; cout << "unordered_multiset.size()= " << c.size() << endl; cout << "unordered_multiset.max_size()= " << c.max_size() << endl; //357913941 cout << "unordered_multiset.bucket_count()= " << c.bucket_count() << endl; cout << "unordered_multiset.load_factor()= " << c.load_factor() << endl; cout << "unordered_multiset.max_load_factor()= " << c.max_load_factor() << endl; cout << "unordered_multiset.max_bucket_count()= " << c.max_bucket_count() << endl; for (unsigned i=0; i< 20; ++i) { cout << "bucket #" << i << " has " << c.bucket_size(i) << " elements.\n"; } string target = get_a_target_string(); { timeStart = clock(); auto pItem = find(c.begin(), c.end(), target); //比 c.find(...) 慢很多 cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end()) cout << "found, " << *pItem << endl; else cout << "not found! " << endl; } { timeStart = clock(); auto pItem = c.find(target); //比 std::find(...) 快很多 cout << "c.find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end()) cout << "found, " << *pItem << endl; else cout << "not found! " << endl; } c.clear(); test_moveable(unordered_multiset<MyString>(),unordered_multiset<MyStrNoMove>(), value); } } //--------------------------------------------------- #include <unordered_map> #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() #include <iostream> #include <ctime> namespace jj09 { void test_unordered_multimap(long& value) { cout << "\ntest_unordered_multimap().......... \n"; unordered_multimap<long, string> c; char buf[10]; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { try { snprintf(buf, 10, "%d", rand()); //multimap 不可使用 [] 進行 insertion c.insert(pair<long,string>(i,buf)); } catch(exception& p) { cout << "i=" << i << " " << p.what() << endl; abort(); } } cout << "milli-seconds : " << (clock()-timeStart) << endl; cout << "unordered_multimap.size()= " << c.size() << endl; cout << "unordered_multimap.max_size()= " << c.max_size() << endl; //357913941 long target = get_a_target_long(); timeStart = clock(); auto pItem = c.find(target); cout << "c.find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end()) cout << "found, value=" << (*pItem).second << endl; else cout << "not found! " << endl; } } //--------------------------------------------------- #include <ext\slist> //注意, 上一行並沒有引發警告訊息如 #include <ext\hash_set> 所引發者: //...\4.9.2\include\c++\backward\backward_warning.h //[Warning] ... #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() #include <iostream> #include <ctime> namespace jj10 { void test_slist(long& value) { cout << "\ntest_slist().......... \n"; __gnu_cxx::slist<string> c; char buf[10]; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { try { snprintf(buf, 10, "%d", rand()); c.push_front(string(buf)); } catch(exception& p) { cout << "i=" << i << " " << p.what() << endl; abort(); } } cout << "milli-seconds : " << (clock()-timeStart) << endl; } } //--------------------------------------------------- /* 以下測試 hash_multiset, hash_multimap 過程中遇到阻礙: headers <hash_set> 和 <hash_map> 各有兩個, 分別在 ...\4.9.2\include\c++\backward 和 ...\4.9.2\include\c++\ext, 不知要用哪一組! 用 <ext\...> 那一組會有問題 ...\4.9.2\include\c++\backward\hashtable.h [Error] no match for call to '(const hasher {aka const __gnu_cxx::hash<std::basic_string<char> >}) (const key_type&)' 用 <backward\...> 那一組有相同的問題. so, 放棄測試 (no insertion or push_back or ...). */ #include <ext\hash_set> //...\4.9.2\include\c++\backward\backward_warning.h //[Warning] #warning This file includes at least one deprecated or antiquated header //which may be removed without further notice at a future date. //Please use a non-deprecated interface with equivalent functionality instead. //For a listing of replacement headers and interfaces, consult the file backward_warning.h. //To disable this warning use -Wno-deprecated. [-Wcpp] #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() #include <iostream> #include <ctime> namespace jj11 { void test_hash_multiset(long& value) { cout << "\ntest_hash_multiset().......... \n"; __gnu_cxx::hash_multiset<string> c; char buf[10]; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { try { snprintf(buf, 10, "%d", rand()); //! c.insert(string(buf)); } catch(exception& p) { cout << "i=" << i << " " << p.what() << endl; abort(); } } cout << "milli-seconds : " << (clock()-timeStart) << endl; } } //--------------------------------------------------- #include <ext\hash_map> //...\4.9.2\include\c++\backward\backward_warning.h //[Warning] #warning This file ... (如上個函數所言) #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() #include <iostream> #include <ctime> namespace jj12 { void test_hash_multimap(long& value) { cout << "\ntest_hash_multimap().......... \n"; __gnu_cxx::hash_multimap<long, string> c; char buf[10]; clock_t timeStart = clock(); for(long i=0; i< value; ++i) { try { snprintf(buf, 10, "%d", rand()); //c.insert(... } catch(exception& p) { cout << "i=" << i << " " << p.what() << endl; abort(); } } cout << "milli-seconds : " << (clock()-timeStart) << endl; timeStart = clock(); //! auto ite = c.find(... cout << "milli-seconds : " << (clock()-timeStart) << endl; } } //--------------------------------------------------- #include <set> #include <stdexcept> #include <string> #include <cstdlib> //abort() #include <cstdio> //snprintf() |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |