跟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

相关文章

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪