侯捷STL—容器vector

容器vector

一、使用vector

在这里插入图片描述

#include <iostream>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <vector>
#include <string>
#include <stdexcept>
#include <algorithm>
#include "public_func.h"

using namespace std;

namespace Namespace_vector
{
    void test_vector()
    {
        cout << "test_vector" << endl;

        vector<string>    vec;
        char              buf[10] = {0};

        clock_t           timestart = clock();

        //测试生成数据时间
        for (long i = 0; i < MAXSIZE; i++)
        {
            try
            {
                snprintf(buf, 10, "%d", rand());
                vec.push_back(string(buf));
            }
            catch(const std::exception& e)
            {
                std::cerr << e.what() << '\n';
                abort();    //程序常退出
            }    
        }
        cout << "time(ms) = "       << (clock() - timestart) << endl;
        cout << "vec.size() = "     << vec.size() << endl;
        cout << "vec.front() = "    << vec.front() << endl;
        cout << "vec.back() = "     << vec.back() << endl;
        cout << "vec.data() = "     << vec.data() << endl;
        cout << "vec.capacity() = " << vec.capacity() << endl;

        //测试qsort和bsearch的时间
        string target = get_a_string_target();
        timestart   = clock();

        //find查找
        {
            auto pItem = ::find(vec.begin(), vec.end(), target);
            cout << "::find(), time = " << (clock()-timestart) << endl;

            if (pItem != vec.end())
            {
                cout << "found, " << *pItem << endl;
            }
            else
            {
                cout << "not found!" << endl;
            }           
        }
        
        //sort + bsearch
        {
            timestart = clock();
            sort(vec.begin(), vec.end());

            string* pItem = (string*)bsearch(&target, vec.data(), vec.size(), sizeof(string), compareStrings);
            cout << "qsort + bsearch, time(ms) = " << (clock()-timestart) << endl;

            if (pItem != NULL)
            {
                cout << "found, " << *pItem << endl;
            }
            else
            {
                cout << "not found" << endl;
            }            
        }
    }     
}

int main(int argc, char *argv[])
{
    cout << "----------STL VECTOR BEGIN------------\n" << endl;

    Namespace_vector::test_vector();

    cout << "\n-----------STL VECTOR END-----------" << endl;

    system("pause");
    return 0;
}

二、vector源码解析

template<class T, class Alloc = alloc>
class vector{
	//定义
	typedef T  value_type;
	typedef value_type* iterator;
	typedef value_type& reference;
	typedef size_t size_type;
	
	//成员
	iterator start;				//开始节点
	iterator finish;			//结束节点后一个节点
	iterator end_of_storage;	//最大节点
	
	//函数
	iterator begin(){return start;}
	iterator end(){return finish;}
	size_type size() const {end() - begin();}
	size_type capacity() const() {end_of_storage() - begin();}
	bool empty() const {return begin() == end();}
	reference operator[](size_type n){return *(begin() + n);}
	reference front() {return *begin();};
	reference back() {return * (end()-1);}
};

三、vector内存扩充

在这里插入图片描述

void push_back(const T& x){
	if (finish != end_of_storage){
		construct(finish, x);
		++finish;
	}
	else
		insert_aux(end(), x);
}

template<class T, class Alloc>
void vector<T,Alloc>::insert_aux(iterator position, const T& x){
	if (finish != end_of_storage){
		construct(finish, *(finish-1));
		++finish;

		//以下代码供其他函数使用,insert,不一定插入在最后
		T x_copy = x;
		copy_backward(position, finish-2, finish-1);
		*position =  x_copy;
	}
	else{	//已无备用空间
		const size_type old_size = size();
		const size_type len = old_size ? 0 2*old_size : 1;	//分0则是1,不为0则是之前的2倍
		
		//重新申请内存
		iterator new_start = data_allocator::allocate(len);
		iterator new_finish = new_start;
		
		//内存拷贝
		try{
			new_finish = uninitialized_copy(start, position, new_start);	//将原vector内容拷贝到新vector内容
			++new_finish;	//调整finish位置

			//以下代码供其他函数使用,insert,不一定插入在最后,也需拷贝当前位置后面元素
			new_finish = uninitialized_copy(position, finish, new_finish);	//拷贝当前后面元素
		} catch(...){
			destroy(new_start, new_finish);
			data_allocator::deallocate(new_start, len);
			throw;
		}

		//释放原内存
		destroy(begin(), end());
		deallocate();

		//调整迭代器,指向新vector
		start = new_start;
		finish = new_finish;
		end_of_storage = new_start + len;
	}
}

四、vector迭代器iterator

template<class T, class Alloc=alloc>
class vector{
	typedef T value_type;
	typedef value_type* iterator; //迭代器为指针T*
};

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值