Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

std - C++ Delete Duplicate Entries in a vector

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.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, a cute way you could do it is to sort the vector using std::sort, and then you can use std::unique to remove consecutive duplicates from the vectors whole range (.begin() to .end()).

You can probably find a more efficient way, but this is an interesting one at least and will only require 2 lines of code :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...