Effective STL (电子书下载) 经验总结程序辅助验证(持续补充)

本文总结了《EffectiveSTL》中关于STL容器使用的最佳实践,重点介绍了如何通过调用empty()方法代替检查size()是否为0来提高效率,以及如何使用reserve()方法来避免vector和string容器不必要的重新分配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

个人对书籍《Effective STL》有过多次阅读,由于在学习工作中遇到过STL的使用问题,所以借此总结自己的使用经验。本文总结的部分准则也会给出相应的程序验证,使大家能够更直观的感受正确合理使用STL,不仅程序看着优美,更能使程序的效率高,易于扩展维护。

电子书下载可以去我的Github网站。

经验准则

容器

第 4 条 : 调用empty而不是检查size()是否为0

因empty对所有的标准容器都是常数时间操作,而对一些list容器size消耗线性时间。

vector和string

第 14 条 : 使用reserve来避免不必要的重新分配

像我们经常使用的顺序容器vector,可以实现容量动态的增长,但为乐程序的效率,我们应尽可能地把重新分配的次数减少到最低限度。
这时我们可以使用reserve。

// Method 1
int N = 521; //预估的容器需要的大小
vector<T> v;
v.reserve(N);

// Method 2
T t;
int N = 521; //预估的容器需要的大小
vector<T> v(N, t);

// Method 3
T t;
int N = 521; //预估的容器需要的大小
vector<T> v;
v.assign(N, t);

size() tells you how many elements are in the container. It does not tell you how much memory the container has allocated for the elements it holds.

capacity() tells you how many elements the container can hold in the memory it has already allocated. This is how many total elements the container can hold in that memory, not how many more elements it can hold. If you’d like to find out how much unoccupied memory a vector or string has, you must subtract size() from capacity(). If size and capacity return the same value, there is no empty space in the container, and the next insertion (via insert or push_back, etc.) will trigger the reallocation steps above.

resize(Container::size_type n) forces the container to change to n the number of elements it holds. After the call to resize, size will return n. If n is smaller than the current size, elements at the end of the container will be destroyed. If n is larger than the current size, new elements will be added to the end of the container, either by copying a default-constructed element or by copying an object you provide via an additional parameter. If n is larger than the current capacity, a reallocation will take place before elements are added.

reserve(Container::size_type n) forces the container to change its capacity to at least n, provided n is no less than the current size. This typically forces a reallocation, because the capacity needs to be increased. (If n is less than the current capacity, vector ignores the call and does nothing. string may reduce its capacity to the maximum of size() and n, but the string’s size definitely remains unchanged. In my experience, using reserve to trim the excess capacity from a string is generally less successful than using “the swap trick,” which is the topic of Item 17.) Note that calling reserve never changes the number of elements in a container (i.e., its size).

摘录来自: Scott Meyers. “Effective STL (Gal Zentner’s Library)。”

#include <iostream>
#include <vector>
#include <ctime>
#include <algorithm>
#include<cmath>
using namespace std;


int main()
{
    clock_t startTime, endTime;
    int N = pow(10, 8);

    startTime = clock();
    vector<int> v1;
    for(int i = 0; i < N; i++)
    {
        v1.push_back(2);
    }
    endTime = clock();
    cout<<"(push_back) The run time is: "<<(double)(endTime - startTime)/CLOCKS_PER_SEC<<"s"<<endl;

    startTime = clock();
    vector<int> v2;
    v2.reserve(N);
    for(int i = 0; i < N; i++)
    {
        v2[i] = 2;
    }
    endTime = clock();
    cout<<"(reserve)   The run time is: "<<(double)(endTime - startTime)/CLOCKS_PER_SEC<<"s"<<endl;

    startTime = clock();
    vector<int> v3(N, 2);
    endTime = clock();
    cout<<"(v(N, t))   The run time is: "<<(double)(endTime - startTime)/CLOCKS_PER_SEC<<"s"<<endl;

    startTime = clock();
    vector<int> v4;
    v4.assign(N, 2);
    endTime = clock();
    cout<<"(assign)    The run time is: "<<(double)(endTime - startTime)/CLOCKS_PER_SEC<<"s"<<endl;

    return 0;
}

程序运行结果如下:

(push_back) The run time is: 4.75988s
(reserve) The run time is: 0.306832s
(v(N, t)) The run time is: 1.54979s
(assign) The run time is: 1.52001s

总结

本文分节详细,但部分书中有的准则并未给出。一是个人能力有限,书中个别总结自己暂未遇到,领悟不深,大家可以去看书。二是本文侧重程序辅助验证,加深理解,因有的准则自己还没能整理出合适的程序, 所以会暂时省略。本文所有代码都可以去我的GitHub下载,都会有一定的注释。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值