How is the unique data stored in an object stored in a vector?
I want to create a method to remove duplicates from a text file.
Edit: Why am I getting downvoted? It doesn't look like I haven't searched the internet before asking.
For example, data in a 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 an apple, orange, pear class according to this example. Using the class, I created 3 different object vectors to store them using set methods.
For example, if found Apple
:
Apple.setedible(Edible);
Apple.setinedible(Inedible);
I can currently store them well in my object vectors, resulting 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 base of duplicates on edible
and inedible
, and I don't know how I will eliminate them, which will lead me to:
In Apple vector:
5:10
1:2
In Orange Vector:
20:1
1:20
In Pear Vector:
5:1
Please advise.
source to share
What do you want as a type for 5:10
or 1:2
? Is this a string or a pair of integers? I suppose this is the line for my example.
You must use std::map< std::string , std::set<std::string> >
to store your data. Then you can add it like in this example, with each line unique to 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
source to share