删除向量中的重复元素 [英] Delete duplicate elements in vector
问题描述
vector< string> str;
我的向量用字符串a,b,c,b,c,d填充。 ,a。
即
str [0] = a; str [1] = b; str [2] = c; .....等等。
现在我想删除上面向量中的重复元素,使得向量只包含不同的元素 ;a,b,c;和d。
我该怎么办?
请帮助
谢谢!
vector< string> ; str;
我的向量用字符串a,b,c,b,c,d填充。 ,a。
即
str [0] = a; str [1] = b; str [2] = c; .....等等。
现在我想删除上面向量中的重复元素,使得向量只包含不同的元素 ;a,b,c;和d。
我该怎么办?
请帮助
谢谢!!
我建议使用第二个向量和for循环迭代元素( vector iterator )。然后,您可以将当前向量与第二个向量的所有元素进行比较。这是否有意义,或者我是否需要了解更多细节?
vector< string> str;
我的向量用字符串a,b,c,b,c,d填充。 ,a。
即
str [0] = a; str [1] = b; str [2] = c; .....等等。
现在我想删除上面向量中的重复元素,使得向量只包含不同的元素 ;a,b,c;和d。
我该怎么办?
请帮助
谢谢!!
你真的需要一个向量吗?
如果你必须消除重复项,那么使用一个集合是最有用的,因为每个元素都被添加只有一次。
/ Sid
嗨西卡里,
能不能给我更多细节关于你所说的,因为我无法完全理解你的意思。
我有一大堆代码,我一直在使用向量,所以现在我不会能够从向量切换到集合。
使用向量实现此目的的可能方法是什么?
谢谢!!
Hi,
vector<string> str;
I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a".
i.e
str[0] = a; str[1] = b; str[2] = c;.....and so on.
Now I would like to delete the duplicate elements in the above vector such that the vector only contains distinct elemets "a", "b", "c" and "d".
How can I do that??
Please help
Thanks!!
Hi,
vector<string> str;
I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a".
i.e
str[0] = a; str[1] = b; str[2] = c;.....and so on.
Now I would like to delete the duplicate elements in the above vector such that the vector only contains distinct elemets "a", "b", "c" and "d".
How can I do that??
Please help
Thanks!!I would recommend using a second vector and a for loop to iterate through the elements (vector iterator). Then you can compare the current vector against all the elements of the second vector. Does that make sense, or do I need to get into a bit more detail?
Hi,
vector<string> str;
I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a".
i.e
str[0] = a; str[1] = b; str[2] = c;.....and so on.
Now I would like to delete the duplicate elements in the above vector such that the vector only contains distinct elemets "a", "b", "c" and "d".
How can I do that??
Please help
Thanks!!Do you really need a vector ??
If you have to eliminate duplicates then using a set is most useful as every element is added only once.
/ Sid
Hi Sicarie,
Can you please give me more details on what you have said, as I am not able to exactly grasp what you meant.
I have a large set of code where I have been using vectors, so now I will not be able to switch from vectors to sets.
What is the possible way to achieve this using vectors??
Thanks!!
这篇关于删除向量中的重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!