How to unique my data that are stored in an object which are stored in a vector?

北城余情 提交于 2019-12-13 07:45:09

问题


I want to create a method to eliminate duplicates from a text file.

Edit: 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

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.

For example if Apple is detected:

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

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

Please advise.


回答1:


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.

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


来源:https://stackoverflow.com/questions/30412626/how-to-unique-my-data-that-are-stored-in-an-object-which-are-stored-in-a-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!