I have to create another vector in which I only copy the unique party names.
I want to do so by using a loop that goes through the first vector with all the party names, and another loop that goes through the second vector of unique names and only adds a value if it does not exist.
I do not want to use std::sort or std::unique or anything of that kind, just the most basic way to do it.
I am not sure if my loops are correct or how to copy elements of one vector into another.
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <set>
usingnamespace std;
int main()
{
// ifstream in( "data.txt" );
istringstream in( "George Washington,Unaffiliated\n""John Adams,Federalist\n""Thomas Jefferson,Democratic-Republican\n""James Madison,Democratic-Republican\n""James Monroe,Democratic-Republican\n" );
vector<string> affiliation;
for ( string president, party; getline( in, president, ',' ) && getline( in, party ); ) affiliation.push_back( party );
cout << "All party names:\n";
for ( string s : affiliation ) cout << s << '\n';
set<string> uniq( affiliation.begin(), affiliation.end() );
cout << "\nUnique party names:\n";
for ( string s : uniq ) cout << s << '\n';
}
All party names:
Unaffiliated
Federalist
Democratic-Republican
Democratic-Republican
Democratic-Republican
Unique party names:
Democratic-Republican
Federalist
Unaffiliated
In other words the goal is not "use of C++", but "to learn to think".
Ignore the C++ syntax for now and list the logical steps that the program has to take. The algorithm. Once you have that, then it is time to figure a syntax for the steps.
Your initial code fragment looks like that you have lost in the details of syntax without "big picture".