(六)STL周边浅谈

目录

一个万用的hash_function

Tuple用例

type traits

cout

movable元素对于deque速度现的影响

测试函数


一个万用的hash_function

将hash函数封装成类对象

  • hash_val进行拆解传入参数,进行循环
  • 每次拆解一个参数,都需要更新种子
  • TR1的工具,)0x9e3779b9是黄金比例数字0.618

 

测试 

当没有指定hash函数时,用默认的hash函数,但是针对特殊的类型,有自己特化的hash函数 

Tuple用例

测试

  • 实现,递归的继承实现自动添加任意多个函数,需要有终止条件无参数偏特化版本
  • 元组大小决定由类的继承关系决定

type traits

测试,traits回答type的类型

区别拷贝和‘搬运’构造函数 

cout

cout继承关系,把已有的类型放入cout才可以输出

movable元素对于deque速度现的影响

  • 深浅拷贝
  • 使用move后,保证原来的地址不在使用

 

测试函数

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

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ STL(Standard Template Library)是C++标准库中的一个重要组成部分,它提供了一套丰富的模板类和函数,用于支持常见的数据结构和算法操作。STL主要由六大组件组成,它们分别是: 1. 容器(Containers):包括vector、list、deque、set、map等,用于存储和管理数据。容器提供了不同的数据结构,如动态数组、链表、双端队列、集合和映射等。 2. 算法(Algorithms):包括排序、查找、合并等常见的算法操作。STL提供了一系列泛型算法,可以直接应用于各种容器,例如sort、find、merge等。 3. 迭代器(Iterators):用于遍历容器中的元素。迭代器提供了一种通用的访问容器元素的方式,使得算法能够独立于具体容器类型而操作。 4. 分配器(Allocators):用于管理内存的分配和释放。分配器提供了一种可替换的内存管理机制,可以根据需要自定义内存分配策略。 5. 函数对象(Function Objects):也称为仿函数,是可调用对象的抽象。STL中的很多算法可以接受函数对象作为参数,从而实现灵活的操作。 6. 适配器(Adapters):用于将一个容器类型转换为另一个容器类型。STL提供了一些适配器,例如stack、queue和priority_queue,它们基于已有的容器实现了不同的行为特性。 这些组件相互之间存在紧密的关联和依赖关系,共同构成了C++ STL的基础框架,提供了丰富而高效的编程工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值