如何唯一化存储在矢量中的对象中存储的数据? [英] How to unique my data that are stored in an object which are stored in a vector?
问题描述
我想创建一种方法来消除文本文件中的重复项.
I want to create a method to eliminate duplicates from a text file.
为什么我会被否决?就像我没有在问之前没有在网上搜索一样.
Why am i getting downvoted ? It's not like I didn't search through the web before asking.
例如,文本文件中的数据:
For example, the data in the text file:
Fruits:Edible:Inedible
Apple:5:10
Apple:1:2
Pear:5:1
Orange:20:1
Pear:5:1
Apple:5:10
Orange:1:20
Orange:20:1
根据此示例,我有一类苹果,橙子,梨.使用该类,我使用set方法创建了3种不同的对象向量来将它们存储在其中.
I have a class of apple, orange, pear according to this example. Using the class, I have created 3 different object vector to store them in, using set methods.
例如,如果检测到Apple
:
Apple.setedible(Edible);
Apple.setinedible(Inedible);
我目前可以将它们很好地存储到它们的对象向量中,这将导致以下结果:
I can currently store them nicely into their object vectors which will result in this:
In Apple vector:
5:10
1:2
5:10
In Orange Vector:
20:1
1:20
20:1
In Pear Vector:
5:1
5:1
我想消除基于edible
和inedible
的重复项,而且我不知道如何消除它们,这会导致以下结果:
I want to eliminate the duplicates base on edible
and inedible
and I have no idea on how am I going to eliminate them which will result me in:
In Apple vector:
5:10
1:2
In Orange Vector:
20:1
1:20
In Pear Vector:
5:1
请告知.
推荐答案
您想要什么作为5:10
或1:2
的类型?是一个字符串还是几个整数?我想这是我的示例字符串.
What do you want as type for 5:10
or 1:2
? Is it a string or a couple of integers ? I suppose it's a string for my example.
您应该使用std::map< std::string , std::set<std::string> >
来存储数据.然后,您可以像下面的示例一样添加它,每个水果的每个字符串都是唯一的:
You should use a std::map< std::string , std::set<std::string> >
to store your data. Then you can add it like this example with each string unique for each fruit :
std::map< std::string , std::set<std::string> > data;
data["Apple"].insert("5:10");
data["Apple"].insert("1:2");
data["Apple"].insert("5:10"); // Nothing is inserted here, already exists
这篇关于如何唯一化存储在矢量中的对象中存储的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!