澳门新蒲京娱乐


【澳门新蒲京娱乐】添加字段,MySQL联合索引功能与用法实例分析

shell操作mysql数据库深入解析

STL与泛型编程,forward_list查找插入数据方法示例澳门新蒲京娱乐:

std::forward_list介绍

二 STL种类布局基础介绍

STL6大部件——《STL源码剖析》

  1. 容器:各样数据结构,如Vector,List,Deque,Set,Map,用来存放数据,STL容器是1种Class
    Template,就体量来说,这一片段很像冰山载海面包车型地铁比率。
  2. 分配器:担任空间安排与治本,从落到实处的角度来看,配置器是二个贯彻了动态空间布署、空间管理、空间释放的Class
    Template。
  3. 算法:种种常用算法如Sort,Search,Copy,Erase,从落到实处的角度来看,STL算法是1种Function
    Templates。
  4. 迭代器:扮演容器与算法之间的胶合剂,是所谓的“泛型指针”,共有多种档案的次序,以及其余衍生变化,从完毕的角度来看,迭代器是一种将Operators*,Operator->,Operator++,Operator–等生死相依操作予以重载的Class
    Template。全数STL容器都附带有友好专门项目标迭代器——是的,只有容器设计者才领会怎样遍历本人的成分,原生指针(Native
    pointer)也是1种迭代器。
  5. 适配器:一种用来修饰容器(Containers)或仿函数(Functors)或迭代器(Iterators)接口的事物,例如:STL提供的Queue和Stack,就算看似容器,其实只可以算是一种容器配接器,因为
    它们的底层完全信赖Deque,全体操作有底层的Deque供应。改换Functor接口者,称为Function
    艾达pter;改换Container接口者,称为Container
    艾达pter;改动Iterator接口者,称为Iterator
    Adapter。配接器的落实技术很难一言蔽之,必须逐一剖析。
  6. 仿函数:行为看似函数,可用作算法的某种政策(Policy),从得以实现的角度来看,仿函数是壹种重载了Operator()的Class
    或 Class Template。一般函数指针可身为狭义的仿函数。

澳门新蒲京娱乐 1

64.png

次第实例:

澳门新蒲京娱乐 2

65.png

forward_list的首要症结是无法在常量时间内任性访问跋扈成员,对成员的拜会须求线性时间代价;以及存款和储蓄链接音信供给费用内部存款和储蓄器,非常是当包罗多量的小圈圈成员时。forward_list处于效用思虑,有意不提供size()成员函数。获取forward_list所蕴涵的分子个数供给用std::distance(_begin,
_end)算法。forward_list中的每种成分保存了固定前三个要素及后1个成分的新闻,无法张开直接随机走访操作。

四 分配器之测试

#include <list>
#include <stdexcept>
#include <string>
#include <cstdlib>      //abort()
#include <cstdio>       //snprintf()
#include <algorithm>    //find()
#include <iostream>
#include <ctime> 

#include <cstddef>
#include <memory>   //內含 std::allocator  
    //欲使用 std::allocator 以外的 allocator, 得自行 #include <ext\...> 
#ifdef __GNUC__     
#include <ext\array_allocator.h>
#include <ext\mt_allocator.h>
#include <ext\debug_allocator.h>
#include <ext\pool_allocator.h>
#include <ext\bitmap_allocator.h>
#include <ext\malloc_allocator.h>
#include <ext\new_allocator.h>  
#endif

namespace jj20
{
//pass A object to function template impl(),
//而 A 本身是個 class template, 帶有 type parameter T,  
//那麼有無可能在 impl() 中抓出 T, 創建一個 list<T, A<T>> object? 
//以下先暫時迴避上述疑問.

void test_list_with_special_allocator()
{
#ifdef __GNUC__ 
    cout << "\ntest_list_with_special_allocator().......... \n";

    //不能在 switch case 中宣告,只好下面這樣.               //1000000次 
    list<string, allocator<string>> c1;                     //3140
    list<string, __gnu_cxx::malloc_allocator<string>> c2;   //3110
    list<string, __gnu_cxx::new_allocator<string>> c3;      //3156
    list<string, __gnu_cxx::__pool_alloc<string>> c4;       //4922
    list<string, __gnu_cxx::__mt_alloc<string>> c5;         //3297
    list<string, __gnu_cxx::bitmap_allocator<string>> c6;   //4781                                                      

int choice;
long value;     

    cout << "select: "
         << " (1) std::allocator "
         << " (2) malloc_allocator "
         << " (3) new_allocator "
         << " (4) __pool_alloc "
         << " (5) __mt_alloc "
         << " (6) bitmap_allocator ";

    cin >> choice;
    if ( choice != 0 ) {
        cout << "how many elements: ";
        cin >> value;       
    }

char buf[10];           
clock_t timeStart = clock();                                
    for(long i=0; i< value; ++i)
    {
        try {
            snprintf(buf, 10, "%d", i);
            switch (choice) 
            {
                case 1 :    c1.push_back(string(buf));  
                            break;
                case 2 :    c2.push_back(string(buf));  
                            break;      
                case 3 :    c3.push_back(string(buf)); 
                            break;      
                case 4 :    c4.push_back(string(buf));  
                            break;      
                case 5 :    c5.push_back(string(buf));      
                            break;      
                case 6 :    c6.push_back(string(buf));  
                            break;              
                default: 
                    break;      
            }                   
        }
        catch(exception& p) {
            cout << "i=" << i << " " << p.what() << endl;   
            abort();
        }
    }
    cout << "a lot of push_back(), milli-seconds : " << (clock()-timeStart) << endl;    


    //test all allocators' allocate() & deallocate();
    int* p;     
    allocator<int> alloc1;  
    p = alloc1.allocate(1);  
    alloc1.deallocate(p,1);     

    __gnu_cxx::malloc_allocator<int> alloc2;  
    p = alloc2.allocate(1);  
    alloc2.deallocate(p,1);     

    __gnu_cxx::new_allocator<int> alloc3;   
    p = alloc3.allocate(1);  
    alloc3.deallocate(p,1);     

    __gnu_cxx::__pool_alloc<int> alloc4;    
    p = alloc4.allocate(2);  
    alloc4.deallocate(p,2);     //我刻意令參數為 2, 但這有何意義!! 一次要 2 個 ints? 

    __gnu_cxx::__mt_alloc<int> alloc5;  
    p = alloc5.allocate(1);  
    alloc5.deallocate(p,1);     

    __gnu_cxx::bitmap_allocator<int> alloc6;    
    p = alloc6.allocate(3);  
    alloc6.deallocate(p,3);     //我刻意令參數為 3, 但這有何意義!! 一次要 3 個 ints? 
#endif          
}                                                           
}

亲自去做代码:

一 认知header、版本、主要财富

1.C++ Standard Library(C++标准库)
2.Standard Template Library(STL标准模板库)
STL标准模板库占用了C++标准库的大大多。

规范库以header files情势展现
(一)C++规范库的header files
不带副档名(.h),比方#include<vector>
(二)新式C header files 不带副档名.h,比如#include<cstdio>
(3)旧式C header files 带有副档名.h,举例#include<stdio.h>
(肆)新式headers内的机件封装于namespace“std”:using namespace std;using
std::cout;(for example)
(5)旧式headers内的机件不封装于namespace“std”

最主要参照C++库参考链接:
1.CPlusPlus.com
2.CppReference.com
3.gcc.gnu.org

以上正是这篇小说的全体内容了,希望本文的剧情对我们的读书或许干活能拉动一定的鼎力相助,假使有失常态大家能够留言交换,谢谢大家对台本之家的支撑。

三 容器之分类与各类测试

小Tips:

澳门新蒲京娱乐 3

66.png

如图所示容器是一种[)的构造,迭代器指向最终三个多少的下一个地址,所以*(c.end())是不容许的。

STL中的常用容器包罗:
(一)顺序性容器(array、vector、deque、list、forward-list);
(二)关联容器(map/multimap、set/multiset、unordered
set/multiset、unordered map/multimap);
(3)容器适配器(queue、stac)

参照链接:深切深入分析C++
STL中的常用容器
顺序性容器:

  • array:array是三个恒定大小的逐条容器,不能够动态退换大小,array内的成分在内部存款和储蓄器中以严酷的线性顺序存储与普通数组评释存储空间尺寸[]的点子是同样有效的,只是参预了部分成员函数和全局函数[get
    (array)、operators
    (array)],以便当作规范容器使用。零大小的array是立见成效的,可是不得以被成员函数front、back、data直接引用,array的swap是二个线性操作调换全体的因素,平时是那么些低效的
![](https://upload-images.jianshu.io/upload_images/5663508-9e9784d381d98cd6.png)

1.png
  • vector:vector是壹种动态数组,在内部存储器中具有一连的贮存空间,帮忙高效随机走访。由于具备接二连三的储存空间,所以在插入和删除操作方面,作用比相当慢。vector有四个构造函数,默许的构造函数是结构一个发轫长度为0的内存空间,且分配的内部存款和储蓄器空间是以2的翻番动态拉长的,即内存空间增进是服从20,二一,22,2三…..加强的,在push_back的经过中,若开采分配的内存空间不足,则重新分配1段连接的内部存款和储蓄器空间,其尺寸是现行反革命连连空间的二倍,再将原先空间中的成分复制到新的空中中,品质消耗不小,特别是当成分是非内部数据时(非内部数据往往构造及拷贝构造函数极其复杂)。
![](https://upload-images.jianshu.io/upload_images/5663508-1e2fb710a1a49a09.png)

2.png
  • deque:deque和vector类似,支持高效随机走访。二者最大的分别在于,vector只还好后面插入数据,而deque支持双端插入数据。deque的内部存款和储蓄器空间分布是小片的接连,小片间用链表相连,实际上里面有3个map的指针。deque空间的重新分配要比vector快,重新分配空间后,原有的要素是不供给拷贝的。
![](https://upload-images.jianshu.io/upload_images/5663508-3a5d16f590523f8a.png)

3.png
  • list:list是三个双向链表,因而它的内部存款和储蓄器空间是足以不总是的,通过指针来进展数据的拜会,那使list的自便存款和储蓄变得特别低效,由此list未有提供[]操作符的重载。但list能够很好地支撑放2地方的插入和删除,只需移动相应的指针就能够。
![](https://upload-images.jianshu.io/upload_images/5663508-e868e93d7a31888a.png)

4.png
  • forward_list:前向链表是用单链表完毕的,可在常量时间内在链表中做插入或删除操作。list比之forward_list,双向链表要消耗额外的上空存储每一种成分和在插入和删除成分时二个分寸的越来越高的大运支出,所以forward_list更有成效,就算只好前进遍历。forward_list是唯1的正统容器中有意识不给出size()成员函数的,那样是为了更迅捷而怀念,能够用distance(c.begin(),c.end())来获得forward_list的分寸,那将花费三个线性时间,而一旦同list同样实现size()成员函数的话,那样要花费一些非常的贮存空间[用以链表中的内部计数得出size()]和驱动插入和删除成分时有3个轻微的成效下跌,达成size()要费用一个常量的岁月。
![](https://upload-images.jianshu.io/upload_images/5663508-4602d81bfda12004.png)

5.png



关联容器:
  • map:map是1种关系容器,该容器用唯1的入眼字来映射相应的值,即具备key-value作用。map内部自行建造壹棵红黑树(1种自平衡二叉树),那棵树具备数据自动排序的功用,所以在map内部全部的多少都以有序的,以二叉树的款式进行集体。
![](https://upload-images.jianshu.io/upload_images/5663508-a01f17a2a5af4f4e.png)

9.png
  • set:set也是1种关联性容器,它同map同样,底层使用红黑树完成,插入删除操作时唯有移动指针就能够,不关乎内部存款和储蓄器的位移和拷贝,所以效能比较高。set中的成分都以唯1的,而且私下认可情形下会对成分进行升序排列。所以在set中,不可能直接改动成分值,因为那样会打乱原本科学的相继,要改换成分值必须先删除旧成分,再插入新因素。不提供直接存取元素的别的操作函数,只可以通过迭代器实行直接存取。
![](https://upload-images.jianshu.io/upload_images/5663508-7ff0f2f3f18d0cc6.png)

8.png
  • unordered set/multiset 、unordered
    map/multimap:散列容器比②叉树的积存方式可以提供越来越高的拜会作用,unordered库提供五个散列集合类unordered_set和unordered_multiset,STLport也提供hash_set和hash_multiset,它们的接口,用法与stl里的标准提到容器set/multiset同样,只是里面接纳散列表替代了贰叉树完结,因而查找复杂度由数降为常数。
![](https://upload-images.jianshu.io/upload_images/5663508-929f3f4fa33eceb0.png)

10.png



容器适配器:
  • queue:queue是一个行列,达成先进先出成效,queue不是正统的STL容器,却以专门的学业的STL容器为根基。queue是在deque的根基上包裹的。之所以采用deque而不选用vector是因为deque在剔除成分的时候释放空间,同时在再一次申请空间的时候无需拷贝全数因素。
![](https://upload-images.jianshu.io/upload_images/5663508-e7f8ad4d724844be.png)

7.png
  • stack:stack是促成先进后出的职能,和queue同样,也是在那之中封装了deque,那也是干什么称为容器适配器的原故呢(纯属臆度)。自身不直接保养被控体系的模板类,而是它存款和储蓄的容器对象来为它达成全体的效劳。stack的源代码原理和完毕情势均跟queue同样。
![](https://upload-images.jianshu.io/upload_images/5663508-daeb430a88472e66.png)

6.png

运用实例代码如下:
(1)array:

#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;  
}
}

澳门新蒲京娱乐 4

67.png

(2) vector

#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); 
}   
}

澳门新蒲京娱乐 5

68.png

(3) list

#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);                             
}   
}

澳门新蒲京娱乐 6

70.png

(4) forward_list

#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();   
}                                            
}

澳门新蒲京娱乐 7

71.png

(5) slist

#include <ext\slist>

#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;          
}                                                           
}

澳门新蒲京娱乐 8

72.png

(6) deque

#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);                               
}                                                           
}

澳门新蒲京娱乐 9

73.png

(7) multiset

#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);                         
}                                                            
}

澳门新蒲京娱乐 10

76.png

(8) multimap

#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();                          
}                                                            
}

澳门新蒲京娱乐 11

77.png

(9) unordered_multiset

#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);                                     
}                                                    
}

澳门新蒲京娱乐 12

78.png

(10) unordered_multimap

#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;      
}                                                            
}

澳门新蒲京娱乐 13

79.png

相关文章

No Comments, Be The First!
近期评论
    功能
    网站地图xml地图