I am trying to read from and excel file and I have a list of names but I want only to print out the names that are unique. ie if sam appears twice I want to discard this name entirely.
string name;
vector<string> hostnameHolder;
int main(){
while (getline(src, name, ','))
{
nameHolder.push_back(name);
sort(nameHolder.begin(), nameHolder.end());
int vSize = nameHolder.size();
name = nameHolder[0];
for (int i = 1; i < vSize; i++)
{
if (name != nameHolder[i])
{
cout << name << ", "
}
}
this is a snippet of my code so far but it is not working how I would like.
A better and an elegant approach is to use std::map. Every time you add a new string, map increments its count index. This is an example for using maps.
You might also look into unordered_set, which would be quicker to load names into and doesn't care what order they are spit back out in.
Both types have built-in find functions as well.
[EDIT:]
Hmm, you're actually looking for truly unique names in the file, so if sam is mentioned twice you don't want his name even once. This is still easily doable with set, do a kind of boolean subtraction on it.
1 2 3 4 5 6 7 8 9 10 11 12
while(processing names)
{
pair testval = add name to main set
if(testval means that name is already in main set)
{
add name to a subtraction set
}
}
for(All names in subtraction set)
{
remove that name from the main set.
}
I have used maps to get unique names and dates of birth. However, now when I apply the other conditions and try to print out the remaining, it shows all unique names and unique DOBs. It doesnt not print out a particular DOB that is blonde for example.