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的一些技巧函数使用的更多相关文章

  1. c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例

    c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...

  2. 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数

    ---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...

  3. C++STL中的unique函数解析

    一.总述 unique函数属于STL中比较常用函数,它的功能是元素去重.即”删除”序列中所有相邻的重复元素(只保留一个).此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素给占领了(详细情 ...

  4. STL之vector常用函数笔记

    STL之vector常用函数笔记 学会一些常用的vector就足够去刷acm的题了 ps:for(auto x:b) cout<<x<<" ";是基于范围的 ...

  5. stl::map之const函数访问

    如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...

  6. STL中的find_if函数

      上一篇文章也讲过,find()函数只能处理简单类型的内容,也就是缺省类型,如果你想用一个自定义类型的数据作为查找依据则会出错!这里将讲述另外一个函数find_if()的用法 这是find()的一个 ...

  7. STL容器的reserve()函数和resize()函数解析

    以vector为例,我们都知道可以用reserve()和resize()函数来为容器预留空间或者调整它的大小. 不过从它俩的名字上可以看出区别: reserve():serve是"保留&qu ...

  8. 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 ...

  9. STL数组处理常用函数

    reverse(a,a+n)反转 sort(a,a+n,cmp)排序 unique(a,a+n,cmp)对于有序集合进行去重,返回新数组最后一个元素的指针 next_permutatoin(a,a+n ...

随机推荐

  1. SVN与Git优缺点比较

    1.SVN优缺点优点: 1. 管理方便,逻辑明确,符合一般人思维习惯. 2. 易于管理,集中式服务器更能保证安全性. 3. 代码一致性非常高. 4. 适合开发人数不多的项目开发. 缺点: 1. 服务器 ...

  2. LeetCode OJ:Majority Element(主要元素)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. 【SQL查询】模糊查询_like

    [格式]:SELECT 字段 FROM 表 WHERE 某字段 Like 条件 [说明]: 1. %表示任意0个或多个字符,可匹配任意类型和长度的字符. 2. _ 表示任意单个字符.匹配单个任意字符. ...

  4. CBP是什么?

    coded_block_pattern  简称CBP,用来反映该宏块编码中残差情况的语法元素.CBP共有6位,其中前面2位代表UV分量,描述如下表所示:后面4位是Y分量,分别代表宏块内的4个8x8子宏 ...

  5. Android 画廊效果之ViewPager显示多个图片

    首先来看下面的效果: 从上面的图片可以看到,当添加多张图片的时候,能够在下方形成一个画廊的效果,我们左右拉动图片来看我们添加进去的图片,效果是不是好了很多呢?下面来看看怎么实现吧! 上面的效果类似An ...

  6. SVG 总结

    //文件名:11.svg<?xml version="1.0" encoding="UTF-8" ?> <!--XML NameSpace:名 ...

  7. AlexNet神经网络结构

    Alexnet是2014年Imagenet竞赛的冠军模型,准确率达到了57.1%, top-5识别率达到80.2%. AlexNet包含5个卷积层和3个全连接层,模型示意图: 精简版结构: conv1 ...

  8. [转载] ffmpeg摄像头视频采集-采集步骤概述并采集一帧视频

    近期由于工作任务,需要开发一个跨平台视频聊天系统,其中就用到了ffmpeg进行采集与编码,网上找了一大堆的资料,虽然都有一些有用的东西,但实在太碎片化了,这几天一直在整理和实验这些资料,边整理,边做一 ...

  9. 第一章 Oracle10g数据库新特性

    1.1 Oracle10g数据库概述 1.1.1 网格数据库 Oracle10g数据库是一种为网格计算而设计的数据库,是第一个用完整集成的软件基础架构来实现网络计算的数据库系统,其中10g的g表示gr ...

  10. SGU 502 Digits Permutation

    这个题目 正解应该是 dp 吧 对18个数字进行2进制枚举放不放,,,可以这么理解 以当前状态 stu,他对应的余数是 h 进入下一个状态: 进行记忆画搜索就行了 1 #include<iost ...