C ++删除向量中的重复条目 [英] C++ Delete Duplicate Entries in a vector

查看:72
本文介绍了C ++删除向量中的重复条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时间向量,在很多地方都有时间重复。时间向量只是文本文件中的一列数据,同一数据文本文件中还有其他几个向量(速度,位置等)。如何根据时间删除重复的条目,同时保留所有其他条目?说:

I have a vector of times, and there are a number of places where the time is duplicated. The time vector is only one column of data from a text file, there are several other vectors (velocity, position, etc) from the same data text file. How do I go about deleting the duplicate entries based on time, while maintaining all the other entries? Say:

if (time[j] == time[j + 1]
{
    do stuff...
}

我实际上有几个向量,所以我将删除

I actually have several vectors, so I will be deleting the duplicate entries from them as well. I need to preserve the order of data in the other vectors (they are not necessarily consecutive.)

我只想删除连续的重复点,所以我需要保留其他向量中的数据顺序(它们不一定是连续的)。 。

I only want to delete consecutive duplicate points.

谢谢。

推荐答案

如果必须保留订购并重复订购不是连续的:

If ordering must be preserved and duplicates are not consecutive:

{
    auto i = time.begin()
    std::set<time::value_type> exists;
    while(i != time.end()) {
        if (exists.insert(*i).second == false)  //it's a duplicate:
            i = time.erase(i);
        else //else not a duplicate
            ++i;
    }
}

这篇关于C ++删除向量中的重复条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆