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. 安装php-ldap模块

    php-ldap模块作用就是实现ldap认证,因此需要安装 1.安装软件包解决依赖 yum install openldapyum install openldap-devel 2.拷贝库文件 cp ...

  2. Android中Service的一个Demo例子

    Android中Service的一个Demo例子  Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding.  本文,主要贴代码,不对Servic ...

  3. Gonet2 游戏server框架解析之Agent(3)

    客户端消息在Agent中的预处理流程. Agent定义好的三种请求: //api.go var RCode = map[int16]string{ 0: "heart_beat_req&qu ...

  4. [转]LNMP环境下的Web常见问题排查(精品)

    来源:http://mp.weixin.qq.com/s? __biz=MjM5NzUwNDA5MA==&mid=200596752&idx=1&sn=37ecae802f32 ...

  5. KendoUi中KendoDropDownList控件的使用——三级级联模块的实现

    1. 应用需求 在权限系统开发中除了以上数据表关系的设计之外.比較麻烦的地方是级联模块在页面的展示,因为设计中最多是控制到三级,因此三级级联模块的展示.编辑等页面操作是须要解决的问题,这里採用Kend ...

  6. JavaScript数组的某些操作(二)

    7.颠倒数组中元素的顺序(注意:不是为数组排序)--reverse方法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona ...

  7. 关于ajax访问express服务器的跨域问题

    在学习es6的时候用promise封装了一个ajax <script type="text/javascript"> function getNews(URL) { l ...

  8. 网页中插入javascript的几种方法

    网页中插入javascript的方法常见的有两种: 一.直接使用html标记 JavaScript 可以出现在 html的任意地方.使用标记<script>…</script> ...

  9. POJ 3038 贪心(multiset)

    题意: 思路: 1. 贪心 我们考虑肯定是走最近的最合适 想象自己是一个黑一日游的司机: 1.如果有乘客要上车,那么就让他上,收钱! 2.如果超载了,把距目的地最远的几个乘客踢下去,退钱. 3.行驶到 ...

  10. Codefroces Educational Round 26 837 D. Round Subset

    D. Round Subset time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...