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 ...
随机推荐
- python基础----多态与多态性、super函数用法、继承原理
一.多态与多态性 ㈠多态: 多态指的是一类事物有多种形态, ...
- phonegap(cordova)从手机app跳转到web页面在跳转回APP本地页面思路
项目中需要用到 WAP支付宝支付. 但是 使用PHONEGAP开发 跳转到支付宝支付,然后跳转回来 就回不到APP的本地页面, 就是使用WAP的第三方登录也是一样的.很难从WAP页面在跳转到 app本 ...
- Linux常用网络工具:批量主机服务扫描之nmap
Linux下有很多强大网络扫描工具,网络扫描工具可以分为:主机扫描.主机服务扫描.路由扫描等. 之前已经写过常用的主机扫描和路由扫描工具,nmap支持批量主机扫描和主机服务扫描. nmap的安装直接使 ...
- 新生代Eden与两个Survivor区的解释
文章出处:http://ifeve.com/jvm-yong-generation/ 聊聊JVM的年轻代 1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分 ...
- SpringBoot (六) :如何优雅的使用 mybatis
原文出处: 纯洁的微笑 这两天启动了一个新项目因为项目组成员一直都使用的是mybatis,虽然个人比较喜欢jpa这种极简的模式,但是为了项目保持统一性技术选型还是定了 mybatis.到网上找了一下关 ...
- Bootstrap 按钮下拉菜单
向下拉 <div class="dropdown"> <button class="btn btn-default" data-toggle= ...
- (转) 使用vivado创建工程 3
Create a Hello World application In this experiment we will use Xilinx SDK to create a simple Hello ...
- 前端PHP入门-007-流程控制
在之前我们已经使用过if判断语句 基本语法,不能有半点马乎,完全是语法规范规定的,不这么写就错! 简单看看 <?php //定义是否打赏的变量 $dashang = true; if($dash ...
- AngularJs附件上传下载
首先:angular-file-upload 是一款轻量级的 AngularJS 文件上传工具,为不支持浏览器的 FileAPI polyfill 设计,使用 HTML5 直接进行文件上传. 第一步: ...
- struts2验证规则validation配置文件命名方式总结
1.Action级别校验命名格式: ActionClassName-validation.xml 2.Action中某个方法的校验命名格式: ActionClassName-ActionAliasNa ...