使用通用函数复制而不重复
Copy without duplicates using generic function
我正在尝试制作一个通用函数,它将元素从一个块复制到另一个块而没有重复项。函数接受三个 pointers/iterators 并且必须适用于所有类型的迭代器。函数应该 return 一个 pointer/iterator 正好指向目标块后面的一个位置。
p1和p2来自同一类型。但是,p3 不必与 p1 和 p2 的类型相同。
#include <iostream>
#include <algorithm>
#include <vector>
template<typename iter_type1, typename iter_type2>
auto CopyWithoutDuplicate(iter_type1 p1, iter_type1 p2, iter_type2 p3){
int n=std::distance(p1,p2);
std::unique_copy(p1,p2,p3);
p3+=n;
return p3;
}
int main()
{
std::string s="abc defabcd ghidef",str;
std::vector<int>a{1,1,2,2,3,4,3,5},b;
auto it1=CopyWithoutDuplicate(s.begin(),s.end(),str.begin());
while(it1!=str.begin())
{
std::cout<<*it1;
it1--;
}
std::cout<<endl;
auto it2=CopyWithoutDuplicate(a.begin(),a.end(),b.begin());
while(it2!=b.begin())
{
std::cout<<*it2<<" ";
it2--;
}
return 0;
}
Correct output would be:
abc defghi
1 2 3 4 5
我尝试为此使用 std::unique_copy
,但我不知道我的代码哪里出错了。这不会在屏幕上打印任何内容。
CopyWithoutDuplicate
可以简化。
auto CopyWithoutDuplicate(iter_type1 p1, iter_type1 p2, iter_type2 p3){
return std::unique_copy(p1,p2,p3);
}
工作示例:
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <unordered_set>
template<typename iter_type1, typename iter_type2>
iter_type2 CopyWithoutDuplicate(iter_type1 p1, iter_type1 p2, iter_type2 p3){
std::unordered_set<typename std::iterator_traits<iter_type1>::value_type> m;
for (; p1 != p2; ++p1) {
if (m.count(*p1) != 0)
continue;
*p3++ = *p1;
m.insert(*p1);
}
return p3;
}
int main()
{
std::string s="abc defabcd ghidef",str;
std::vector<int>a{1,1,2,2,3,4,3,5},b;
CopyWithoutDuplicate(s.begin(),s.end(),std::back_inserter(str));
for(char c : str)
std::cout<<c;
std::cout<<std::endl;
CopyWithoutDuplicate(a.begin(),a.end(),std::back_inserter(b));
for(int n : b)
std::cout<<n<<" ";
return 0;
}
输出
abc defghi
1 2 3 4 5
我正在尝试制作一个通用函数,它将元素从一个块复制到另一个块而没有重复项。函数接受三个 pointers/iterators 并且必须适用于所有类型的迭代器。函数应该 return 一个 pointer/iterator 正好指向目标块后面的一个位置。
p1和p2来自同一类型。但是,p3 不必与 p1 和 p2 的类型相同。
#include <iostream>
#include <algorithm>
#include <vector>
template<typename iter_type1, typename iter_type2>
auto CopyWithoutDuplicate(iter_type1 p1, iter_type1 p2, iter_type2 p3){
int n=std::distance(p1,p2);
std::unique_copy(p1,p2,p3);
p3+=n;
return p3;
}
int main()
{
std::string s="abc defabcd ghidef",str;
std::vector<int>a{1,1,2,2,3,4,3,5},b;
auto it1=CopyWithoutDuplicate(s.begin(),s.end(),str.begin());
while(it1!=str.begin())
{
std::cout<<*it1;
it1--;
}
std::cout<<endl;
auto it2=CopyWithoutDuplicate(a.begin(),a.end(),b.begin());
while(it2!=b.begin())
{
std::cout<<*it2<<" ";
it2--;
}
return 0;
}
Correct output would be:
abc defghi
1 2 3 4 5
我尝试为此使用 std::unique_copy
,但我不知道我的代码哪里出错了。这不会在屏幕上打印任何内容。
CopyWithoutDuplicate
可以简化。
auto CopyWithoutDuplicate(iter_type1 p1, iter_type1 p2, iter_type2 p3){
return std::unique_copy(p1,p2,p3);
}
工作示例:
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <unordered_set>
template<typename iter_type1, typename iter_type2>
iter_type2 CopyWithoutDuplicate(iter_type1 p1, iter_type1 p2, iter_type2 p3){
std::unordered_set<typename std::iterator_traits<iter_type1>::value_type> m;
for (; p1 != p2; ++p1) {
if (m.count(*p1) != 0)
continue;
*p3++ = *p1;
m.insert(*p1);
}
return p3;
}
int main()
{
std::string s="abc defabcd ghidef",str;
std::vector<int>a{1,1,2,2,3,4,3,5},b;
CopyWithoutDuplicate(s.begin(),s.end(),std::back_inserter(str));
for(char c : str)
std::cout<<c;
std::cout<<std::endl;
CopyWithoutDuplicate(a.begin(),a.end(),std::back_inserter(b));
for(int n : b)
std::cout<<n<<" ";
return 0;
}
输出
abc defghi
1 2 3 4 5