区间版本insert的优势(序列容器)

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/timer.hpp>

#define NUMS (10000000)
int
main()
{
    std::vector<int> v1, v2;
    for(int i=0; i<NUMS; i++){
        v1.push_back(1);
    }

    boost::timer t;
    double elapse;
    std::cout<<"max time elapsed can be tested = "<<t.elapsed_max()/3600<<"h"<<std::endl;
    std::cout<<"min time elapsed can be tested = "<<t.elapsed_min()<<"s"<<std::endl;

    v2.insert(v2.begin(), v1.begin(), v1.end());
    elapse = t.elapsed();
    std::cout<<"区间insert插入"<<NUMS<<"个数的时间 = "<<elapse<<std::endl;

    std::vector<int>().swap(v2);
    std::vector<int>::iterator tmp(v2.begin());
    t.restart();
    for(int i=0; i<NUMS; i++){
        tmp = v2.insert(tmp, v1[i]);
        ++tmp;
    }
    elapse = t.elapsed();
    std::cout<<"循环insert插入"<<NUMS<<"个数的时间 = "<<elapse<<std::endl;
    return 0;
}

相关文章
相关标签/搜索