跟Google学写代码--Chromium/base--stl_util源码学习及应用

2024-03-27 12:08

本文主要是介绍跟Google学写代码--Chromium/base--stl_util源码学习及应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Ttile: Chromium/base–stl_util源码学习及应用

Chromium是一个伟大的、庞大的开源工程,很多值得我们学习的地方。

今天与大家分享的就是Chromium下base中的stl_util,是对stl的补充,封装,更有利于我们的使用,完全可以移植到自己的C++工程项目中来。

1 释放STL内存 
Clears internal memory of an STL object.

我们知道,vector的clear()方法式清除了vector中的内容,但是vector object所占的内存不会清除。

因此,std_util中有个这个方法:

template<class T>
void STLClearObject(T* obj) {T tmp;tmp.swap(*obj);// Sometimes "T tmp" allocates objects with memory (arena implementation?).// Hence using additional reserve(0) even if it doesn't always work.obj->reserve(0);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

也就是使用swap,之前写过一篇博客: 
《实战c++中的vector系列–正确释放vector的内存(clear(), swap(), shrink_to_fit())》 
地址:http://blog.csdn.net/wangshubo1989/article/details/50359750

2 释放容器内的指针对象 
这里隐藏了一个巨大的坑儿,就是迭代器的失效问题,之前也有博客里面介绍过:

《实战c++中的vector系列–可怕的迭代器失效(vector重新申请内存)》 
地址:http://blog.csdn.net/wangshubo1989/article/details/50334297

《实战c++中的vector系列–可怕的迭代器失效之二(删除vector中元素)》 
地址:http://blog.csdn.net/wangshubo1989/article/details/50334503

template <class ForwardIterator>
void STLDeleteContainerPointers(ForwardIterator begin, ForwardIterator end) {while (begin != end) {ForwardIterator temp = begin;++begin;delete *temp;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3 正确删除容器内pairs对象

template <class ForwardIterator>
void STLDeleteContainerPairPointers(ForwardIterator begin,ForwardIterator end) {while (begin != end) {ForwardIterator temp = begin;++begin;delete temp->first;delete temp->second;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4 删除容器内pairs对象中的第一个元素

template <class ForwardIterator>
void STLDeleteContainerPairFirstPointers(ForwardIterator begin,ForwardIterator end) {while (begin != end) {ForwardIterator temp = begin;++begin;delete temp->first;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

5 删除容器内pairs对象中的第二个元素

template <class ForwardIterator>
void STLDeleteContainerPairSecondPointers(ForwardIterator begin,ForwardIterator end) {while (begin != end) {ForwardIterator temp = begin;++begin;delete temp->second;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

6 vector转为数组 
一定要注意,vector可能为空的情况下。

template<typename T>
inline T* vector_as_array(std::vector<T>* v) {return v->empty() ? NULL : &*v->begin();
}
  • 1
  • 2
  • 3
  • 4
template<typename T>
inline const T* vector_as_array(const std::vector<T>* v) {return v->empty() ? NULL : &*v->begin();
}
  • 1
  • 2
  • 3
  • 4

7 string转为数组 
还是是要注意string为空。

inline char* string_as_array(std::string* str) {// DO NOT USE const_cast<char*>(str->data())return str->empty() ? NULL : &*str->begin();
}
  • 1
  • 2
  • 3
  • 4

8 查找关联容器中是否有某个特定的key 
没什么好说的,提高效率,函数的参数为const引用类型。

template <typename Collection, typename Key>
bool ContainsKey(const Collection& collection, const Key& key) {return collection.find(key) != collection.end();
}
  • 1
  • 2
  • 3
  • 4

9 判断容器是否有序 
这里用到了adjacent_find,在一个数组中寻找两个相邻的元素;

template <typename Container>
bool STLIsSorted(const Container& cont) {// Note: Use reverse iterator on container to ensure we only require// value_type to implement operator<.return std::adjacent_find(cont.rbegin(), cont.rend(),std::less<typename Container::value_type>())== cont.rend();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

10 获得两个有序容器的不同 
这里用到了DCHECK,是自己定义的宏:

#define DCHECK(condition)                                               \LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON ? !(condition) : false)  \<< "Check failed: " #condition ". "
  • 1
  • 2
  • 3
template <typename ResultType, typename Arg1, typename Arg2>
ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) {DCHECK(STLIsSorted(a1));DCHECK(STLIsSorted(a2));ResultType difference;std::set_difference(a1.begin(), a1.end(),a2.begin(), a2.end(),std::inserter(difference, difference.end()));return difference;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

11 合并两个有序的容器

template <typename ResultType, typename Arg1, typename Arg2>
ResultType STLSetUnion(const Arg1& a1, const Arg2& a2) {DCHECK(STLIsSorted(a1));DCHECK(STLIsSorted(a2));ResultType result;std::set_union(a1.begin(), a1.end(),a2.begin(), a2.end(),std::inserter(result, result.end()));return result;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

12 同时包含在两个容器中的元素 
set_intersection:同时包含第一个和第二个集合中的元素

template <typename ResultType, typename Arg1, typename Arg2>
ResultType STLSetIntersection(const Arg1& a1, const Arg2& a2) {DCHECK(STLIsSorted(a1));DCHECK(STLIsSorted(a2));ResultType result;std::set_intersection(a1.begin(), a1.end(),a2.begin(), a2.end(),std::inserter(result, result.end()));return result;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

13 判断一个容器是否包含另一个容器的所有内容

template <typename Arg1, typename Arg2>
bool STLIncludes(const Arg1& a1, const Arg2& a2) {DCHECK(STLIsSorted(a1));DCHECK(STLIsSorted(a2));return std::includes(a1.begin(), a1.end(),a2.begin(), a2.end());
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

应用: 
对上面介绍的方法进行简单的使用,看看吧:

#include<iostream>
#include<vector>
#include<algorithm>
#include"stl_util.h"int main()
{std::vector<int> numbers{1, 2, 3, 4, 6};std::vector<int> numbers1{ 1, 2, 3 ,4};std::cout << "Test STLIsSorted:{1, 2, 3, 4, 6}" << std::endl;std::cout << std::boolalpha <<STLIsSorted(numbers) << std::endl << std::endl;std::cout << "Test STLSetDifference {1, 2, 3, 4, 6} and { 1, 2, 3 ,4}:" << std::endl;std::vector<int> difference_result;difference_result = STLSetDifference<std::vector<int>, std::vector<int>, std::vector<int>>(numbers, numbers1);for(auto iter: difference_result){std::cout << iter << std::endl << std::endl;}std::cout << "Test STLSetIntersection {1, 2, 3, 4, 6} and { 1, 2, 3 ,4}:" << std::endl;std::vector<int> intersection_result;intersection_result = STLSetIntersection<std::vector<int>, std::vector<int>, std::vector<int>>(numbers, numbers1);for (auto iter : intersection_result){std::cout << iter << " " ;}std::cout << std::endl;std::cout << "Test STLIncludes {1, 2, 3, 4, 6} and { 1, 2, 3 ,4}:" << std::endl;std::cout << std::boolalpha << STLIncludes(numbers, numbers1) << std::endl << std::endl;std::cout << "Test STLClearObject:" << std::endl;STLClearObject(&numbers);std::cout << "vector size:" << numbers.size() << std::endl;std::cout << "vector capacity:" << numbers.capacity() << std::endl << std::endl;system("pause");return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

输出结果:

Test STLIsSorted:{1, 2, 3, 4, 6}
trueTest STLSetDifference {1, 2, 3, 4, 6} and { 1, 2, 3 ,4}:
6Test STLSetIntersection {1, 2, 3, 4, 6} and { 1, 2, 3 ,4}:
1 2 3 4
Test STLIncludes {1, 2, 3, 4, 6} and { 1, 2, 3 ,4}:
trueTest STLClearObject:
vector size:0
vector capacity:0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

这篇关于跟Google学写代码--Chromium/base--stl_util源码学习及应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/852046

相关文章

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

解决升级JDK报错:module java.base does not“opens java.lang.reflect“to unnamed module问题

《解决升级JDK报错:modulejava.basedoesnot“opensjava.lang.reflect“tounnamedmodule问题》SpringBoot启动错误源于Jav... 目录问题描述原因分析解决方案总结问题描述启动sprintboot时报以下错误原因分析编程异js常是由Ja

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案