侯捷STL学习笔记(二)

延续上一篇继续写:

1)测试deque
2)双向开口,是分段连续,感觉是连续的,其实不是。
3)内存上两边都可以扩充
4)每次512扩充,两边扩充申请buffer,buffer由指针指向
max_size由限制,使用全局的sort排序

这里写图片描述

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

这里写图片描述

测试stack
    1)栈,数据结构上和deque差不多
    2)两段插入插入删除受限的容器
    3)也有人叫做容器的适配器adapter
    4)没有提供容器的迭代器iterator,否则对迭代器的操作会破坏堆栈的结构
    也没有提供find,sort的功能;但是有时候让你实现堆栈的排序操作

这里写图片描述

#include <stack>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio>  //snprintf()
#include <iostream>
#include <ctime> 
namespace jj17
{
void test_stack(long& value)
{
    cout << "\ntest_stack().......... \n";

stack<string> c;    
char buf[10];

clock_t timeStart = clock();                                
    for(long i=0; i< value; ++i)
    {
        try {
            snprintf(buf, 10, "%d", rand());
            c.push(string(buf));                        
        }
        catch(exception& p) {
            cout << "i=" << i << " " << p.what() << endl;
            abort();
        }
    }
    cout << "milli-seconds : " << (clock()-timeStart) << endl;  
    cout << "stack.size()= " << c.size() << endl;
    cout << "stack.top()= " << c.top() << endl; 
    c.pop();
    cout << "stack.size()= " << c.size() << endl;
    cout << "stack.top()= " << c.top() << endl; 


    {
stack<string, list<string>> c;      //以 list 為底層 
    for(long i=0; i< 10; ++i) {
            snprintf(buf, 10, "%d", rand());
            c.push(string(buf));                        
    }
    cout << "stack.size()= " << c.size() << endl;
    cout << "stack.top()= " << c.top() << endl; 
    c.pop();
    cout << "stack.size()= " << c.size() << endl;
    cout << "stack.top()= " << c.top() << endl; 
    }   

    {
stack<string, vector<string>> c;    //以 vector 為底層 
    for(long i=0; i< 10; ++i) {
            snprintf(buf, 10, "%d", rand());
            c.push(string(buf));                        
    }
    cout << "stack.size()= " << c.size() << endl;
    cout << "stack.top()= " << c.top() << endl; 
    c.pop();
    cout << "stack.size()= " << c.size() << endl;
    cout << "stack.top()= " << c.top() << endl; 
    }

    {
stack<string, set<string>> c;   //以 set 為底層 
/*!
    for(long i=0; i< 10; ++i) {
            snprintf(buf, 10, "%d", rand());
            c.push(string(buf));                        
    }
    cout << "stack.size()= " << c.size() << endl;
    cout << "stack.top()= " << c.top() << endl; 
    c.pop();
    cout << "stack.size()= " << c.size() << endl;
    cout << "stack.top()= " << c.top() << endl; 

//[Error] 'class std::set<std::basic_string<char> >' has no member named 'push_back'
//[Error] 'class std::set<std::basic_string<char> >' has no member named 'back'
//[Error] 'class std::set<std::basic_string<char> >' has no member named 'pop_back'
*/
    }

//!stack<string, map(string>> c5;   ////以 map 為底層, [Error] template argument 2 is invalid
//!stack<string>::iterator ite1;    //[Error] 'iterator' is not a member of 'std::stack<std::basic_string<char> >'

}                                                           
}

测试queue

堆,数据结构上有deque衍生出来的
没有提供容器的迭代器iterator,否则对迭代器的操作会破坏堆栈的结构

这里写图片描述

#include <queue>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio>  //snprintf()
#include <iostream>
#include <ctime> 
namespace jj18
{
void test_queue(long& value)
{
    cout << "\ntest_queue().......... \n";

queue<string> c;    
char buf[10];

clock_t timeStart = clock();                                
    for(long i=0; i< value; ++i)
    {
        try {
            snprintf(buf, 10, "%d", rand());
            c.push(string(buf));                        
        }
        catch(exception& p) {
            cout << "i=" << i << " " << p.what() << endl;
            abort();
        }
    }
    cout << "milli-seconds : " << (clock()-timeStart) << endl;  
    cout << "queue.size()= " << c.size() << endl;
    cout << "queue.front()= " << c.front() << endl; 
    cout << "queue.back()= " << c.back() << endl;       
    c.pop();
    cout << "queue.size()= " << c.size() << endl;
    cout << "queue.front()= " << c.front() << endl; 
    cout << "queue.back()= " << c.back() << endl;   


    {
queue<string, list<string>> c;      //以 list 為底層 
    for(long i=0; i< 10; ++i) {
            snprintf(buf, 10, "%d", rand());
            c.push(string(buf));                        
    }
    cout << "queue.size()= " << c.size() << endl;
    cout << "queue.front()= " << c.front() << endl; 
    cout << "queue.back()= " << c.back() << endl;       
    c.pop();
    cout << "queue.size()= " << c.size() << endl;
    cout << "queue.front()= " << c.front() << endl; 
    cout << "queue.back()= " << c.back() << endl;   
    }   

    {
queue<string, vector<string>> c;    //以 vector 為底層 
    for(long i=0; i< 10; ++i) {
            snprintf(buf, 10, "%d", rand());
            c.push(string(buf));                        
    }
    cout << "queue.size()= " << c.size() << endl;
    cout << "queue.front()= " << c.front() << endl; 
    cout << "queue.back()= " << c.back() << endl;       
    //!c.pop();  //[Error] 'class std::vector<std::basic_string<char> >' has no member named 'pop_front'
    cout << "queue.size()= " << c.size() << endl;
    cout << "queue.front()= " << c.front() << endl; 
    cout << "queue.back()= " << c.back() << endl;   
    }   

    {
queue<string, set<string>> c;       //以 set 為底層 
/*!
    for(long i=0; i< 10; ++i) {
            snprintf(buf, 10, "%d", rand());
            c.push(string(buf));                        
    }
    cout << "queue.size()= " << c.size() << endl;
    cout << "queue.front()= " << c.front() << endl; 
    cout << "queue.back()= " << c.back() << endl;       
    c.pop();
    cout << "queue.size()= " << c.size() << endl;
    cout << "queue.front()= " << c.front() << endl; 
    cout << "queue.back()= " << c.back() << endl;
//[Error] 'class std::set<std::basic_string<char> >' has no member named 'push_back'
//[Error] 'class std::set<std::basic_string<char> >' has no member named 'front'
//[Error] 'class std::set<std::basic_string<char> >' has no member named 'pop_front'
*/      
    }

//! queue<string, map<string>> c5;  //以 map 為底層, [Error] template argument 2 is invalid
//! queue<string>::iterator ite1;   //[Error] 'iterator' is not a member of 'std::queue<std::basic_string<char> >'  
}                                                           
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值