STL的一些技巧函数使用
1.emplace() 函数和 emplace_back() 函数
C++11的STL中新增加了emplace() 函数和 emplace_back() 函数,用来实现insert() 函数和 push_back() 函数的功能。
如果容器中的元素是对象:
emplace() 函数的功能是可以直接将参数传给对象的构造函数,在容器中构造出一个对象,从而实现 0 拷贝。(底层机制是调用placement new即布局new运算符来实现的)。
假设类MyClass 有构造函数 MyClass(T1 val1,T2 val2);
那么container.emplace(container.end() ,val1, val2) 可以直接在容器的末尾构造出一个对象,如果用container.insert(container.end() ,MyClass(val1,val2)) 会先构造一个对象,然后拷贝到容器的末尾。
同样的,emplace_back() 和 push_back() 的差别类似。
如果是一个已有的对象 MyClass obj;container.emplace(..., obj) 和 container.emplace_back(obj)的效率差不多。
2.stable_sort()方法 与 sort()方法;partition()方法和 stable_partition()方法
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- bool IsOdd(int i)
- {
- return (i% == );
- }
- int main()
- {
- vector<int> v;
- for(int i = ; i < ; i++)
- v.push_back(i);
- cout<<"The original elements in the vector are: "<<endl;
- vector<int>::iterator it, bound;
- for(it = v.begin(); it != v.end(); it++)
- cout<<*it<<" ";
- cout<<endl;
- cout<<"First use the function partition() to separate all elements into 2 groups without ordering: "<<endl;
- //use partition to separate the vector into 2 parts...
- bound = partition(v.begin(), v.end(), IsOdd);
- cout << "All odd elements in the vector are:" <<endl;
- for(it = v.begin(); it != bound; it++)
- cout<<*it<<" ";
- cout<<endl;
- cout<< "All even elements in the vector are:" <<endl;
- for(it = bound; it != v.end(); it++)
- cout<<*it<<" ";
- cout<<endl;
- v.clear();
- for(int i = ; i < ; i++)
- v.push_back(i);
- cout<<"Secondly use the function stable_partition() to separate all elements into 2 groups with ordering: "<<endl;
- //use stable_partition to separate the vector into 2 parts...
- bound = stable_partition(v.begin(), v.end(), IsOdd);
- cout << "All odd elements in the vector are:" <<endl;
- for(it = v.begin(); it != bound; it++)
- cout<<*it<<" ";
- cout<<endl;
- cout<< "All even elements in the vector are:" <<endl;
- for(it = bound; it != v.end(); it++)
- cout<<*it<<" ";
- cout<<endl;
- return ;
- }
- The original elements in the vector are:
- First use the function partition() to separate all elements into groups without ordering:
- All odd elements in the vector are:
- All even elements in the vector are:
- Secondly use the function stable_partition() to separate all elements into groups with ordering:
- All odd elements in the vector are:
- All even elements in the vector are:
3. unique()&& unique_copy函数
类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素。
该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值范围得结束。
- 1 // sort words alphabetically so we can find the duplicates
- 2 sort(words.begin(), words.end());
- 3 /* eliminate duplicate words:
- 4 * unique reorders words so that each word appears once in the
- 5 * front portion of words and returns an iterator one past the
- 6 unique range;
- 7 * erase uses a vector operation to remove the nonunique elements
- 8 */
- 9 vector<string>::iterator end_unique = unique(words.begin(), words.end());
- 10 words.erase(end_unique, words.end());
- 在STL中unique函数是一个去重函数, unique的功能是去除相邻的重复元素(只保留一个),其实它并不真正把重复的元素删除,是把重复的元素移到后面去了,然后依然保存到了原数组中,然后 返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
- 算法标准库定义了一个名为unique_copy的函数,其操作类似于unique。唯一的区别在于:前者接受第三个迭代器实参,用于指定复制不重复元素的目标序列。unique_copy根据字面意思就是去除重复元素再执行copy运算。
- 《Effective STL》里这些话可能有用处:
- ● 如果你需要在vector、string、deque或数组上进行完全排序,你可以使用sort或stable_sort。
● 如果你有一个vector、string、deque或数组,你只需要排序前n个元素,应该用partial_sort。
● 如果你有一个vector、string、deque或数组,你需要鉴别出第n个元素或你需要鉴别出最前的n个元素,而不用知道它们的顺序,nth_element是你应该注意和调用的。
● 如果你需要把标准序列容器的元素或数组分隔为满足和不满足某个标准,你大概就要找partition或stable_partition。
● 如果你的数据是在list中,你可以直接使用partition和stable_partition,你可以使用list的sort来代替sort和stable_sort。
如果你需要partial_sort或nth_element提供的效果,你就必须间接完成这个任务,但正如我在上面勾画的,会有很多选择。
4.STL remove和erase
移除容器里面的元素不应该使用remove算法,而是容器自己的方法erase()。
- erase使用:
- #include <iostream>
- #include <vector>
- using namespace std;
- int main()
- {
- vector<int> arr;
- arr.push_back();
- arr.push_back();
- arr.push_back();
- arr.push_back();
- for(vector<int>::iterator it=arr.begin(); it!=arr.end(); )
- {
- if(* it == )
- {
- it = arr.erase(it);
- }
- else
- {
- ++it;
- }
- }
- //注意上面不能写成
- /*
- for(vector<int>::iterator it=arr.begin(); it!=arr.end(); it ++)
- {
- if(* it == 8)
- {
- arr.erase(it); //在erase后,it失效,并不是指向vector的下一个元素,it成了一个“野指针”。
- }
- }
- */
- }
条款32:如果你真的想删除东西的话就在类似remove的算法后接上erase
把remove的返回值作为erase区间形式第一个实参传递很常见,这是个惯用法。事实上,remove和erase是亲密联盟,这两个整合到list成员函数remove中。这是STL中唯一名叫remove又能从容器中除去元素的函数:
- list<int> li; // 建立一个list
- // 放一些值进去
- li.remove(); // 除去所有等于99的元素:
- // 真的删除元素,
- // 所以它的大小可能改变了
STL的一些技巧函数使用的更多相关文章
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...
- 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数
---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...
- C++STL中的unique函数解析
一.总述 unique函数属于STL中比较常用函数,它的功能是元素去重.即”删除”序列中所有相邻的重复元素(只保留一个).此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素给占领了(详细情 ...
- STL之vector常用函数笔记
STL之vector常用函数笔记 学会一些常用的vector就足够去刷acm的题了 ps:for(auto x:b) cout<<x<<" ";是基于范围的 ...
- stl::map之const函数访问
如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...
- STL中的find_if函数
上一篇文章也讲过,find()函数只能处理简单类型的内容,也就是缺省类型,如果你想用一个自定义类型的数据作为查找依据则会出错!这里将讲述另外一个函数find_if()的用法 这是find()的一个 ...
- STL容器的reserve()函数和resize()函数解析
以vector为例,我们都知道可以用reserve()和resize()函数来为容器预留空间或者调整它的大小. 不过从它俩的名字上可以看出区别: reserve():serve是"保留&qu ...
- Codeforces 1131 F. Asya And Kittens-双向链表(模拟或者STL list)+并查集(或者STL list的splice()函数)-对不起,我太菜了。。。 (Codeforces Round #541 (Div. 2))
F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- STL数组处理常用函数
reverse(a,a+n)反转 sort(a,a+n,cmp)排序 unique(a,a+n,cmp)对于有序集合进行去重,返回新数组最后一个元素的指针 next_permutatoin(a,a+n ...
随机推荐
- spring3: AOP 之 通知顺序
如果我们有多个通知想要在同一连接点执行,那执行顺序如何确定呢?Spring AOP使用AspectJ的优先级规则来确定通知执行顺序.总共有两种情况:同一切面中通知执行顺序.不同切面中的通知执行顺序. ...
- vue-cli favicon 怎么引入
把你的 favicon 图标放到 static 目录下,然后修改 link 标签指向 static 目录下的 favicon 图标文件即可
- Intent Flag启动模式P203
Activity启动模式:点此查看 Intent intent = new Intent(); /** * Intent.FLAG_ACTIVITY_NEW_TASK * 使用一个新的Task来启动一 ...
- linux命令三
作业一:1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) [root@bogon test]# cat /etc/passwd /etc/group > /1. ...
- LeetCode OJ:4Sum(4数字之和)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- Android Broadcast 和 BroadcastReceiver的权限机制
在Android应用开发中,有时会遇到以下两种情况, 1. 一些敏感的广播并不想让第三方的应用收到 : 2. 要限制自己的Receiver接收某广播来源,避免被恶意的同样的ACTION的广播所干扰. ...
- VSCode打开文件总是会覆盖上次打开的标签
在使用VSCode的时候,打开一个文件之后,如果没有修改的话,那么再打开下一个文件的时候,他总会替换上次打开的标签,那么怎么样才能每次都在新的标签打开文件呢? 实际上,这种情况的出现是因为我们点击文件 ...
- E: Could not get lock /var/lib/dpkg/lock解决
ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决 通过终端安装或卸载程序sudo apt-get install/autoremove xxx时出 ...
- Linux paste 命令
Linux paste命令用于合并文件的列. paste指令会把每个文件以列对列的方式,一列列地加以合并. 语法 paste [-s][-d <间隔字符>][--help][--versi ...
- 【前端】XHTML入门笔记
教程/XHTML 模块/XHTML 标准属性/XHTML 事件属性 XHTML 指可扩展超文本标签语言(EXtensible HyperText Markup Language). XHTML 元素必 ...