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() ...
随机推荐
- babel的插件
比如想编译es6的箭头函数,需要使用babel-plugin-transform-es2015-arrow-functions这个插件 此外babel提供了 prests(预设) 相当于是插件的集合 ...
- Mybatis mapper.xml文件头文件备份
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- Cocos2d-x游戏的一般验证分析
Coco2d-x引擎是相对于Unity3D的又一实力派引擎.尽管随着3D游戏的热门,很多其它的厂商偏向于Unity3D.可是Coco2d-x的普及量也不容小觑,特别是一些比較大的手游公司.比方触控科技 ...
- PIM-DM协议内核触发机制及协议执行机制记录
PIM-DM和PIM-SM是组播路由ASM(随意信源组播)中的两种不同模式.相对PIM-SM的组播注冊机制.PIM-DM的扩散机制显得更加粗犷. 一.PIM-DM无需向内核注冊pimreg虚接口. 可 ...
- ADB高级应用
ADB高级应用 一.利用无线来查看adb shell > adb tcpip 5555 连接: > adb connect IP:5555 见后文<调试注意事项> 二.模拟按键 ...
- Android自己定义动态布局 — 多图片上传
Android自己定义动态布局 - 多图片上传 本文介绍Android中动态布局加入图片,多图片上传. 项目中效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5 ...
- Java解析注解
package com.itbuluoge.anno; import java.lang.reflect.Method; import java.util.ArrayList; import java ...
- Scott Hanselman的问题-3
.Net程序员面试 中级篇 (回答Scott Hanselman的问题) 继<.Net 程序员面试 C# 语言篇 (回答Scott Hanselman的问题)>跟<.Net程序员 ...
- POJ 1904 思路题
思路: 思路题 题目诡异地给了一组可行匹配 肯定有用啊-. 就把那组可行的解 女向男连一条有向边 如果男喜欢女 男向女连一条有向边 跑一边Tarjan就行了 (这个时候 环里的都能选 "增广 ...
- Hive里的分区、分桶、视图和索引再谈
福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 Java全栈大联盟 ...