关闭

STL_算法(24)_删除_(3)uinique() unique_copy()

264人阅读 评论(0) 收藏 举报
分类:

remove()

remove_if()

remove_copy()


// 删除连续的重复的算法,只留下唯一的

uinique()

unique_copy()


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()

#include<iostream>
#include<algorithm>
#include<list>
//
//#include<functional>
#include<iterator>

using namespace std;

int main()
{
	int source[] = {1, 4, 4, 6, 1, 2, 2, 3, 1, 6, 6, 6, 5, 7, 5, 4, 4};
	int sourceNum = sizeof(source) / sizeof(source[0]);

	list<int> ilist;
	copy(source, source + sourceNum, back_inserter(ilist));

	for(list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout<<*iter<< ' ';
	cout<<endl;
	
	list<int>::iterator pos;

	// 把连续的数据进行删除
	pos = unique(ilist.begin(), ilist.end());
	
	for(list<int>::iterator iter = ilist.begin(); iter != pos; iter++)
		cout<<*iter<< ' ';
	cout<<endl;

	//
	system("pause");
	return 0;
}


#include<iostream>
#include<algorithm>
#include<list>
//
//#include<functional>
#include<iterator>
bool differeceOne(int elem1, int elem2)
{
	return elem1 + 1 == elem2 || elem1 -1 == elem2;
}

using namespace std;

int main()
{
	int source[] = {1, 4, 4, 6, 1, 2, 2, 3, 1, 6, 6, 6, 5, 7, 5, 4, 4};
	int sourceNum = sizeof(source) / sizeof(source[0]);

	list<int> ilist;
	copy(source, source + sourceNum, back_inserter(ilist));

	for(list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout<<*iter<< ' ';
	cout<<endl;
	
	list<int>::iterator pos;

	// 把连续的数据进行删除
	pos = unique(ilist.begin(), ilist.end());
	
	for(list<int>::iterator iter = ilist.begin(); iter != pos; iter++)
		cout<<*iter<< ' ';
	cout<<endl;

	copy(source, source + sourceNum, ilist.begin());
	for(list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout<<*iter<< ' ';
	cout<<endl;

	//若前面的数字不后面的数字大,把后面的数字删除
	pos = unique(ilist.begin(), ilist.end(), greater<int>());
	for(list<int>::iterator iter = ilist.begin(); iter != pos; iter++)
		cout<<*iter<<' ';
	cout<<endl;

	copy(source, source + sourceNum, ilist.begin());
	for(list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout<<*iter<< ' ';
	cout<<endl;
	//将数据复制到另外的数组里
	unique_copy(ilist.begin(), ilist.end(), ostream_iterator<int>(cout, " "));
	cout<<endl;	

	unique_copy(ilist.begin(), ilist.end(), ostream_iterator<int>(cout, " "), differeceOne);
	//
	system("pause");
	return 0;
}








0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:198370次
    • 积分:4824
    • 等级:
    • 排名:第5966名
    • 原创:227篇
    • 转载:380篇
    • 译文:1篇
    • 评论:94条
    博客专栏
    最新评论