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 ...
随机推荐
- ActiveMQ 的管理和监控
本章重点 理解 JMX 和 ActiveMQ 使用告警消息来监控 ActiveMQ 管理 ActiveMQ ActiveMQ 的日志配置 额,这本书终于读完了,虽然看到后面都是云里雾里的,但是总算是对 ...
- Tkinter Button按钮组件如何调用一个可以传入参数的函数
这里我们要使用python的lambda函数,lambda是创建一个匿名函数,冒号前十传入参数,后面是一个处理传入参数的单行表达式. 调用lambda函数返回表达式的结果. 首先让我们创建一个函数fu ...
- Autolayout .Compact or .Regular [iPhone/iPad]
- docker的应用
[root@yz6205 ~]# docker imagesINFO[0063] GET /v1.19/images/json REPOSITORY TAG IMAGE ID CREATED VIRT ...
- 【转】Hibernate 原汁原味的四种抓取策略
最近在研究 Hibernate 的性能优化的时候碰到了"抓取策略", 由于以前没有详细的研究过, 所以到处找资料, 但是无论从一些讲 Hibernate 书籍,还是他人 Blog ...
- LeetCode OJ:Maximum Subarray(子数组最大值)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 剑指offer--36.整数中1出现的次数(从1到n整数中1出现的次数)
暴力挨个数 ---------------------------------------------------------------------- 时间限制:1秒 空间限制:32768K 热度指 ...
- 由PostgreSQL的区域与字符集说起(转)
转自:http://blog.chinaunix.net/uid-354915-id-3502551.html 由PostgreSQL的区域与字符集说起 一.PostgreSQL的区域区域属性有以下几 ...
- CodeForces - 963D:Frequency of String (bitset暴力搞)
You are given a string ss. You should answer nn queries. The ii-th query consists of integer kiki an ...
- bzoj 3173 最长上升子序列
Written with StackEdit. Description 给定一个序列,初始为空.现在我们将\(1\)到\(N\)的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字, ...