简体   繁体   English

Unique_copy不返回预期输出

[英]Unique_copy not returning expected output

I have this simple code, i know i must be doing some silly mistake : 我有这个简单的代码,我知道我必须做一些愚蠢的错误:

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<iterator>
using namespace std;

int main()
{
vector<string> coll{"one", "two", "two", "three", "four", "one"};
vector<string> col2(coll.size(), "");
unique_copy(coll.cbegin(), coll.cend(), col2.begin());
for(const auto& ele : col2)
    cout<<" - "<<ele;
cout<<endl;
return 0;
}

Output - - one - two - three - four - one - 输出 - - one - two - three - four - one -

I was expecting :- - one - two - three - four - - 我期待着: - - one - two - three - four - -

What am i missing? 我错过了什么?

EDIT: As pointed out in aswer - Damn, i was missing the definition of unique_copy itself. 编辑:正如aswer-Damn所指出的,我错过了unique_copy本身的定义。

Is there any function to do what i expectecd (delete unique elements irrespective of adjacency), other than inserting them in ordered set or sorting before unique copy as i want to keep the ordering. 是否有任何功能可以做我expectecd(删除独立元素而不管相邻性),除了将它们插入ordered set或排序之前,我想保留排序。

std::unique_copy : std :: unique_copy

Copies the elements from the range [first, last) , to another range beginning at d_first in such a way that there are no consecutive equal elements. 将范围[first, last)的元素复制到从d_first开始的另一个范围,使得没有连续的相等元素。

one is not consecutive in your case. 在你的情况下, one不是连续的。

Try: 尝试:

vector<string> coll{"one", "one", "two", "two", "three", "four"};

you will find the output as 你会发现输出为

- one - two - three - four -  - 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM