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)的更多相关文章

  1. cb45a_c++_STL_算法_删除_(3)_unique(唯一的意思)删除连续性的重复的数据

    cb45a_c++_STL_算法_删除_(3)_unique(唯一的意思)删除连续性的重复的数据unique(b,e),删除连续性的,删除重复的数据,比如如果有两个连续的5,5,则留下一个.uniqu ...

  2. cb44a_c++_STL_算法_删除_(2)remove_copy_remove_copy_if

    cb44a_c++_STL_算法_删除_(2)remove_copy_remove_copy_if remove_copy()//在复制过程中删除一些数据remove_copy_if() 删除性算法: ...

  3. cb43a_c++_STL_算法_删除_(1)remove_remove_if

    cb43a_c++_STL_算法_删除_(1)remove_remove_ifremove()remove_if() 注意:1.并不是真正的删除,而是把后面的元素向前移动,覆盖被删除元素,元素个数并没 ...

  4. STL_算法_中使用的函数对象

    写在前面: STL算法中的 函数对象的功能: (1).都是提供一种比较的函数,比较相邻的左右两个值的 相等/大小 等的关系, (2).返回值都是bool :该返回值 貌似是指明 遍历元素是否还要继续往 ...

  5. STL_容器_删除

    1.erase()函数 用于删除由迭代器指定的元素,或者一个区间. 2.clear()函数 用于删除容器中所有元素 3.remove()函数 用于删除指定的元素,无需知道元素在容器的哪个位置,会删除掉 ...

  6. STL_算法_查找算法(lower_bound、upper_bound、equal_range)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n)))    已序区间查找算法 lower_bound()        //找第一个符合的 ...

  7. STL_算法_查找算法(find、find_if)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...

  8. STL_算法_逆转(reverse,reverse_copy)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) //全部容器适用 reverse(b,e)        //逆转区间数据 reverse_copy(b,e,b2) /** ...

  9. STL_算法_依据第n个元素排序(nth_element)

    C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) //全部容器适用 nth_element(b,n,e) nth_element(b,n,e,p) 对照:partition() ...

随机推荐

  1. androidclient和站点数据交互的实现(基于Http协议获取数据方法)

    androidclient一般不直接訪问站点数据库,而是像浏览器一样发送get或者post请求.然后站点返回client能理解的数据格式,client解析这些数据.显示在界面上.经常使用的数据格式是x ...

  2. java对象和json数据转换实现方式1-使用json-lib实现

    測试代码: package com.yanek.util.json; import java.util.ArrayList; import java.util.List; import net.sf. ...

  3. HDU1788 Chinese remainder theorem again【中国剩余定理】

    题目链接: pid=1788">http://acm.hdu.edu.cn/showproblem.php?pid=1788 题目大意: 题眼下边的描写叙述是多余的... 一个正整N除 ...

  4. mongo数据库--非关系型数据库

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGFuZ2xpdXFpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  5. 操作系统 linux 内核的三种进程调度方法

    1.SCHED_OTHER 分时调度策略: 2.SCHED_FIFO 实时调度策略.先到先服务: 3,SCHED_RR 实时调度策略,时间片轮转 . 实时进程将得到优先调用,实时进程依据实时优先级决定 ...

  6. Resize图片

    在网站上传图片的时候,提示图片太大了. 有5种方式来调整图片大小 http://www.wikihow.com/Resize-a-JPEG picresize.com 这个网站比较靠谱:使用Windo ...

  7. ASPNET 页面编码

    转自:http://www.cnblogs.com/libingql/archive/2009/04/11/1433771.html 设置ASPNET页面编码格式 1.Web.Config设置 < ...

  8. Js将类数组转化为数组

    说起伪数组,大家可能会想到arguments, 这个我们函数参数的一个类数组,是类数组的代表. 1.拥有length属性,可以使用下标来访问元素,这两点和数组相同. 2.不能使用数组的方法,他们不能使 ...

  9. Kinect 开发 —— 面部识别

    EmguCV库也能用来进行面部识别(face identify).实际的面部识别,就是将一张图像上的人物的脸部识别出来,这是个很复杂的过程,具体过程我们这里不讨论.对一幅影像进行处理来找到包含脸部的那 ...

  10. Spring boot 解析jsp支持jsp热部署

    解析jsp并且支持jsp热部署 参考地址:https://www.wanpishe.top/detail?blogId=39875536-7d45-48ec-a7b5-f36b85c3a235