unique_copy问题

leewon1988 2009-04-02 10:25:13
在线等待
给出unique_copy的两种用法,最好给出代码,尤其是标明各参数代表什么
谢谢
...全文
77 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangshuiling 2009-04-03
  • 打赏
  • 举报
回复

消除一个容器区间内重复元素,这么简单的概念,楼主自己体会就行了
taifeng123 2009-04-03
  • 打赏
  • 举报
回复
// alg_unique_copy.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
#include <ostream>

using namespace std;

// Return whether modulus of elem1 is equal to modulus of elem2
bool mod_equal ( int elem1, int elem2 ) {
if ( elem1 < 0 )
elem1 = - elem1;
if ( elem2 < 0 )
elem2 = - elem2;
return elem1 == elem2;
};

int main() {
vector <int> v1;
vector <int>::iterator v1_Iter1, v1_Iter2,
v1_NewEnd1, v1_NewEnd2;

int i;
for ( i = 0 ; i <= 1 ; i++ ) {
v1.push_back( 5 );
v1.push_back( -5 );
}

int ii;
for ( ii = 0 ; ii <= 2 ; ii++ )
v1.push_back( 4 );
v1.push_back( 7 );

int iii;
for ( iii = 0 ; iii <= 5 ; iii++ )
v1.push_back( 10 );

cout << "Vector v1 is\n ( " ;
for ( v1_Iter1 = v1.begin( ) ; v1_Iter1 != v1.end( ) ; v1_Iter1++ )
cout << *v1_Iter1 << " ";
cout << ")." << endl;

// Copy first half to second, removing consecutive duplicates
v1_NewEnd1 = unique_copy ( v1.begin ( ) , v1.begin ( ) + 8, v1.begin ( ) + 8 );

cout << "Copying the first half of the vector to the second half\n "
<< "while removing adjacent duplicates gives\n ( " ;
for ( v1_Iter1 = v1.begin( ) ; v1_Iter1 != v1_NewEnd1 ; v1_Iter1++ )
cout << *v1_Iter1 << " ";
cout << ")." << endl;

int iv;
for ( iv = 0 ; iv <= 7 ; iv++ )
v1.push_back( 10 );

// Remove consecutive duplicates under the binary prediate mod_equals
v1_NewEnd2 = unique_copy ( v1.begin ( ) , v1.begin ( ) + 14,
v1.begin ( ) + 14 , mod_equal );

cout << "Copying the first half of the vector to the second half\n "
<< " removing adjacent duplicates under mod_equals gives\n ( " ;
for ( v1_Iter2 = v1.begin( ) ; v1_Iter2 != v1_NewEnd2 ; v1_Iter2++ )
cout << *v1_Iter2 << " ";
cout << ")." << endl;
}


Output

Vector v1 is
( 5 -5 5 -5 4 4 4 7 10 10 10 10 10 10 ).
Copying the first half of the vector to the second half
while removing adjacent duplicates gives
( 5 -5 5 -5 4 4 4 7 5 -5 5 -5 4 7 ).
Copying the first half of the vector to the second half
removing adjacent duplicates under mod_equals gives
( 5 -5 5 -5 4 4 4 7 5 -5 5 -5 4 7 5 4 7 5 4 7 ).

leewon1988 2009-04-03
  • 打赏
  • 举报
回复
我要的是unique_copy带有四个参数的那个用法。以及各个参数的含义啊。。。。
lzr4304061988012 2009-04-02
  • 打赏
  • 举报
回复

#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;
void main()
{
vector<int> ivec;
copy(istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(ivec));
vector<int> unique;
unique_copy(ivec.begin(),ivec.end(),back_inserter(unique));
copy(unique.begin(),unique.end(),ostream_iterator<int>(cout," "));
}
/*
1 1 2 2 3 3 4 4
^Z
1 2 3 4 请按任意键继续. . .
*/

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧