STL_算法_删除(unique、unique_copy)
C++ Primer 学习中。
。
。
简单记录下我的学习过程 (代码为主)
全部容器适用
unique(b,e)
unique(b,e,p)
unique_copy(b1,e1,b2)
unique_copy(b1,e1,b2,p)
注意:
1、没有unique_if()
2、没有unique_copy_if()
/**------http://blog.csdn.net/u010579068------**/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std; /*****************************************
//全部容器适用
unique(b,e)
unique(b,e,p)
unique_copy(b1,e1,b2)
unique_copy(b1,e1,b2,p)
注意:
1、没有unique_if()
2、没有unique_copy_if()
*****************************************/
/**----------------------------------------------------------------------------------
使用方法:删除连续的、反复的元素
----------------------------------------------------------------------------------**/
/*************************************************************************************
std::unique 全部排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator>
ForwardIterator unique ( ForwardIterator first, ForwardIterator last ); template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique ( ForwardIterator first, ForwardIterator last,
BinaryPredicate pred );
//eg:
template <class ForwardIterator>
ForwardIterator unique ( ForwardIterator first, ForwardIterator last )
{
ForwardIterator result=first;
while (++first != last)
{
if (!(*result == *first)) // or: if (!pred(*result,*first)) for the pred version
*(++result)=*first;
}
return ++result;
}
*************************************************************************************/ /*************************************************************************************
std::unique_copy 全部排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class InputIterator, class OutputIterator>
OutputIterator unique_copy ( InputIterator first, InputIterator last,
OutputIterator result ); template <class InputIterator, class OutputIterator, class BinaryPredicate>
OutputIterator unique_copy ( InputIterator first, InputIterator last,
OutputIterator result, BinaryPredicate pred );
//eg:
template <class InputIterator, class OutputIterator>
OutputIterator unique_copy ( InputIterator first, InputIterator last,
OutputIterator result )
{
typename std::iterator_traits<InputIterator>::value_type value = *first;
*result=*first;
while (++first != last)
{
if (!(value == *first)) // or: if (!pred(value,*first)) for the pred version
*(++result) = value = *first;
}
return ++result;
}
*************************************************************************************/ bool myfunction (int i, int j)
{
return (i==j);
}
template<typename T>
void Print(T& V)
{
typename T::iterator iter=V.begin();
while(iter != V.end())
{
cout<<*iter++<<" ";
}
cout<<endl;
}
int main()
{
int myints[] = {10,20,20,20,30,30,20,20,10}; // 10 20 20 20 30 30 20 20 10
vector<int> myvector (myints,myints+9);
vector<int>::iterator it,pend; // using default comparison:
it = unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ?
// ^ myvector.resize( it - myvector.begin() ); // 10 20 30 20 10 // using predicate comparison:
unique (myvector.begin(), myvector.end(), myfunction); // (no changes) // print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl; vector<int> vec;
copy(myints,myints+9,back_inserter(vec));
Print(vec);
pend =unique(vec.begin(),vec.end(),greater<int>());//取单调不递减数列
cout << "vec contains:";
for (it=vec.begin(); it!=pend; ++it)
cout << " " << *it;
cout << endl << endl;
/**--------------------------------------------------------------------------------**/ // int myints[] = {10,20,20,20,30,30,20,20,10};
deque<int> mydeque (9); // 0 0 0 0 0 0 0 0 0
deque<int>::iterator id; // using default comparison:
id=unique_copy (myints,myints+9,mydeque.begin()); // 10 20 30 20 10 0 0 0 0
// ^ sort (mydeque.begin(),id); // 10 10 20 20 30 0 0 0 0
// ^ // using predicate comparison:
id=unique_copy (mydeque.begin(), id, mydeque.begin(), myfunction);
// 10 20 30 20 30 0 0 0 0
// ^ mydeque.resize( id - mydeque.begin() ); // 10 20 30 // print out content:
cout << "mydeque contains:";
for (id=mydeque.begin(); id!=mydeque.end(); ++id)
cout << " " << *id; cout << endl; return 0;
}
STL_算法_删除(unique、unique_copy)的更多相关文章
- cb45a_c++_STL_算法_删除_(3)_unique(唯一的意思)删除连续性的重复的数据
cb45a_c++_STL_算法_删除_(3)_unique(唯一的意思)删除连续性的重复的数据unique(b,e),删除连续性的,删除重复的数据,比如如果有两个连续的5,5,则留下一个.uniqu ...
- cb44a_c++_STL_算法_删除_(2)remove_copy_remove_copy_if
cb44a_c++_STL_算法_删除_(2)remove_copy_remove_copy_if remove_copy()//在复制过程中删除一些数据remove_copy_if() 删除性算法: ...
- cb43a_c++_STL_算法_删除_(1)remove_remove_if
cb43a_c++_STL_算法_删除_(1)remove_remove_ifremove()remove_if() 注意:1.并不是真正的删除,而是把后面的元素向前移动,覆盖被删除元素,元素个数并没 ...
- STL_算法_中使用的函数对象
写在前面: STL算法中的 函数对象的功能: (1).都是提供一种比较的函数,比较相邻的左右两个值的 相等/大小 等的关系, (2).返回值都是bool :该返回值 貌似是指明 遍历元素是否还要继续往 ...
- STL_容器_删除
1.erase()函数 用于删除由迭代器指定的元素,或者一个区间. 2.clear()函数 用于删除容器中所有元素 3.remove()函数 用于删除指定的元素,无需知道元素在容器的哪个位置,会删除掉 ...
- STL_算法_查找算法(lower_bound、upper_bound、equal_range)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n))) 已序区间查找算法 lower_bound() //找第一个符合的 ...
- STL_算法_查找算法(find、find_if)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...
- STL_算法_逆转(reverse,reverse_copy)
C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) //全部容器适用 reverse(b,e) //逆转区间数据 reverse_copy(b,e,b2) /** ...
- STL_算法_依据第n个元素排序(nth_element)
C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) //全部容器适用 nth_element(b,n,e) nth_element(b,n,e,p) 对照:partition() ...
随机推荐
- Vuejs2.0构建一个彩票查询WebAPP(2)
一,Vuex的使用 import Vue from 'vue' import Vuex from 'vuex' import MsgModules from './MsgModules' Vue.us ...
- nuxt 关闭ESlint 语法检测
学习nuxt中在自己编写search组件的时候出现了各种类似于Expected indentation of 0 spaces but found 4的问题 上网搜是因为ESlint语法检测产生的问题 ...
- 昼猫笔记--什么是DOM
昼猫笔记--给你带来不一样的笔记 不止是笔记 更多的是思考 Hello,大家好,昼猫,今天来加深下DOM 什么DOM呢?它的全称叫 Document Object Model 通过全称可以知道它是 文 ...
- 学习《深度学习与计算机视觉算法原理框架应用》《大数据架构详解从数据获取到深度学习》PDF代码
<深度学习与计算机视觉 算法原理.框架应用>全书共13章,分为2篇,第1篇基础知识,第2篇实例精讲.用通俗易懂的文字表达公式背后的原理,实例部分提供了一些工具,很实用. <大数据架构 ...
- 今日SGU 5.18
SGU 125 题意:给你一个数组b[i][j],表示i,j的四周有多少个数字大于它的,问你能不能构造出一个a矩形 收获:dfs + 剪枝 一行一行的dfs,然后第一行去枚举0-9,下一行判断当前选 ...
- CSUOJ 1525 Algebraic Teamwork
Problem A Algebraic Teamwork The great pioneers of group theory and linear algebra want to cooperate ...
- 剑指offer_面试题6_重建二叉树(分解步骤,逐个击破)
题目:输入某二叉树的前序遍历和中序遍历的结果.请重建出该二叉树.如果输入的前序遍历和中序遍历的结果中都不含反复的数字. 比如:输入前序遍历 {1,2,4,7,3,5,6,8} 和中序遍历序列 {4,7 ...
- 机器学习完整过程案例分布解析,python代码解析
所谓学习问题,是指观察由n个样本组成的集合,并依据这些数据来预測未知数据的性质. 学习任务(一个二分类问题): 区分一个普通的互联网检索Query是否具有某个垂直领域的意图.如果如今有一个O2O领域的 ...
- hdu5288 OO’s Sequence 二分 多校联合第一场
OO's Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- git 版本管理工具说明
$ git init (初始化本地仓库,会生成.git 文件夹 .git 文件夹里存储了所有的版本信息.标记等内容) $ git add . ...