C++实践积累
C++ STL
vector
如何彻底清空一个vector?
实践证明,vector.clear()并不能把vector容量清空,只会让vector.size()变为零,依然很占内存。那如何让vector的capacity变为0呢?
vector<int> nums(500);
nums.clear();
cout<<"size after clear:"<<nums.size()<<endl;
cout<<"capacity after clear:"<<nums.capacity()<<endl;
nums = vector<int>();
cout<<"capacity after from new vector:"<<nums.capacity()<<endl;
输出结果:

string
substr
s.substr(start, nums); //从下标为start的元素开始,取nums个数。注意:nums表示所取字符个数,而不结尾字符的下标。
s.substr(start); //从下标为start的元素开始,取到s的结尾
列如,
string str="We think in generalities, but we live in details.";
string str2 = str.substr (3,5); // "think"
string str3 = str.substr (3); //think in generalities, but we live in details.
size_t pos = str.find("live"); // position of "live" in str
string str3 = str.substr (pos); // get from "live" to the end
set
set 用来为C++中的集合容器,但是是底层是用红黑树实现的,其迭代器不能加减常数。但是如何还想用set迭代器加减常数,可以使用vector模拟一个set。每次修改vector之后,需要使用del_dup函数除去重复元素。
void del_dup(vector<int> &vec) {
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
}
虽说set迭代器不能加减常数,但提供了.begin(),.end(),it++等操作,所以遍历集合set可以有以下两种方式:
方法一:auto for
for (auto i : myset) {
cout<<i<<" ";
}
方法二:迭代器
for (auto it = myset.begin(); it != myset.end(); it++) {
cout<<*it<<" ";
}
C++11新用法
哈希表
C++11中引用哈希表来加快查找内容的速度,但是名字不叫hash_map,而叫unordered_set与unordered_map,使用时,
#include <unordered_set>
#include <unordered_map>
unordered_set
unordered_set可以在常数时间查找表中的内容,当我们需要确定曾经是否访问过某个内容,使用unordered_set可以实现O(1)时间查找。比如图的遍历中,确定是否曾经访问过某个结点,可以一边遍历,一边构建hash表,一边查边,确保没有重复访问结点,具体实现参考2.4 图的层序遍历。
unodered_map
unodered_map当我们需要建立某键值对{key, val}之间的关系,并且随后需要根据key快速查找value时,可以使用unordered_map。
创建方法
unordered_map<int, string> hash;
初始化
unordered_map<int, string> hash = {{1, "milk"},{2, "flour"}};
插入
hash.insert({3, "apple"});
查找
auto got = hash.find(1);
if (got != hash.end()) { //使用前最好先确定确实找到了键值对
cout<<got->first<<endl;
cout<<got->second<<endl;
}
输出
1
milk
当我们需要深度拷贝图,或者带随机指针的链表时,可以使用该方法。
遍历容器不同方法
C++引入了一种更简单的for语句, 这种语句可以更简洁遍历初始值列表、数组、容器、string等类型的对象,这些对象的公共特点是都有返回迭代器的begin与end成员。
主要形式,如下:
for (auto &element : vector) {
statement;
}
其中
- auto用来确定容器中元素的类型,不仅可以用在for中,也可以用在其它位置用来确定定义的变量类型,如:auto it = vector.begin() 这样写可以比较简洁
- 如果需要改写容器中元素内容比较在element前加引用型&
以下代码为遍历一个容器或者数组或者string的所有方式:
int array[] = {2, 4, 6, 8};
vector<int> vec(array, array + 4);
string str = "I am good student";
cout<<endl<<"method1: iterator"<<endl;
for (vector<int>::iterator it = vec.begin(); it < vec.end(); ++it) {
cout<<*it<<" ";
}
cout<<endl<<"method2: index"<<endl;
for (vector<int>::size_type id = 0; id < vec.size(); ++id) {
cout<<vec[id]<<" ";
}
cout<<endl<<"method3: auto type"<<endl;
//sometimes, you need iterator traverse while simple code
for (auto it = vec.begin(); it < vec.end(); ++it) {
cout<<*it<<" ";
}
cout<<endl<<"method4: auto for"<<endl;
//use & for writing element to vector
for (auto item : vec) {
cout<<item<<" ";
}
cout<<endl<<"method5: auto for to traverse array"<<endl;
for (auto item : array) {
cout<<item<<" ";
}
cout<<endl;
for (auto item : {1, 3, 5, 7}) {
cout<<item<<" ";
}
cout<<endl<<"method6: auto for to traverse string"<<endl;
for (auto c : str) {
cout<<c;
}
C++与python语言切换时的一些定式思维
二维数组的访问
python中二维数组中可以使用[row, col]的方式访问二维数组

但是C++中逗号表达式表示顺序执行所有表达式,并且将最后一个表达式的值返回。C++ A[i, j]其实表示的是A[j],只是一个指针而已,这会带来很大的误解。
C++实践积累的更多相关文章
- JS实战 · 实践积累点滴杂烩
onmouseover : 鼠标进入 onmouseout : 鼠标离开 onfocus:得到焦点 表单提交执行JS代码,有两种常用方式. 一:在局部(比如按钮定义处)用onclick=" ...
- awk实践积累
#cat iii |awk '{max=$2;min=$2;for (i=2;i<=NF;i++) if ($i>max) max=$i fi} {for (i=2;i<=NF;i+ ...
- eclipse常用快捷键实践积累
1. [Ctrl + D]:删除一整行 2. 给函数添加注释 [选中函数名]-[Alt + Shift + J].如果需要自定义注释内容可通过[项目]-[属性]-[Java代码样式]-[代码模板]-[ ...
- 深度解析SDN——利益、战略、技术、实践(实战派专家力作,业内众多专家推荐)
深度解析SDN——利益.战略.技术.实践(实战派专家力作,业内众多专家推荐) 张卫峰 编 ISBN 978-7-121-21821-7 2013年11月出版 定价:59.00元 232页 16开 ...
- T型知识实践结构的力量(转载)
最近在做的一些新的事情,这其中获得的一些新的思考. T型的知识积累,深度的挖掘可以通过"举一反三"的应用在广度上,广度可以通过"交叉验证"加强我们的认识,可以说 ...
- 干货 |《从Lucene到Elasticsearch全文检索实战》拆解实践
1.题记 2018年3月初,萌生了一个想法:对Elasticsearch相关的技术书籍做拆解阅读,该想法源自非计算机领域红火已久的[樊登读书会].得到的每天听本书.XX拆书帮等. 目前市面上Elast ...
- 【转载】 Spark性能优化指南——基础篇
转自:http://tech.meituan.com/spark-tuning-basic.html?from=timeline 前言 开发调优 调优概述 原则一:避免创建重复的RDD 原则二:尽可能 ...
- [转载]AxureRP学习成长之路
[编者按]本文作者@朱军华Ronzhu , 本文借用官网的描述定义,介绍了在学习AxureRP过程当中所要经历的各个阶段,也结合了作者自身学习AxureRP使用的经验,讲一下在各个阶段中的一些学习方法 ...
- 【转】Spark性能优化指南——基础篇
http://mp.weixin.qq.com/s?__biz=MjM5NDMwNjMzNA==&mid=2651805828&idx=1&sn=2f413828d1fdc6a ...
随机推荐
- windows提权基础大全
Not many people talk about serious Windows privilege escalation which is a shame. I think the reason ...
- 【hdu6051】If the starlight never fade
Portal --> hdu6051 Solution 神仙题qwq好吧我个人感觉是神仙题 这题其实有一个比较野路子的做法..就是..打表观察..反正场上ckw大佬就是这样把这题A穿的 ...
- maven 启动 tomcat 及 跳过 test 安装
1.先在pom文件中配置 tomcat插件 <!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload< ...
- [linux]linuxI/O测试的方法之dd
参考http://www.thomas-krenn.com/en/wiki/Linux_I/O_Performance_Tests_using_dd Measuring Write Performan ...
- python函数的输入参数
http://note.youdao.com/noteshare?id=c2a0a39ee3cae09a62dcbc9f96d04b56
- STL源码分析-function
http://note.youdao.com/noteshare?id=269e16541b43095eaf97636d4e046b1d
- 「Python」10个python项目
1. Pillow. Pillow是由Alex Clark以及其他贡献者实现的“友好版”的PIL.PIL即Python Imaging Library,作者是Fredrik Lundh及其他开发者.A ...
- 前端PHP入门-034-Session技术-掌握级别
而Session是通过将数据保存在服务器端来实现保持连接的.我们通过一个例子来了解session的机制. 我们去饮料店买饮料,下单以后服务员会给我们一个号码牌,然后你走到一旁,服务员并不认识你是谁,如 ...
- OpenCV---图像梯度
图像梯度 推文:[OpenCV入门教程之十二]OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑 图像梯度可以把图像看成二维离散函数,图像梯度其实就是这个 ...
- [LeetCode] Matrix 值修改系列,例题 Surrounded Regions,Set Matrix Zeroes
引言 Matrix内部的值修改严格来讲放在一个系列里不大合适,因为对于不同的问题,所用的算法和技巧可能完全不同,权且这样归类,以后需要时再拆分吧. 例题 1 Given a 2D board cont ...