How to copy certain elements from one vector to another

I have a vector that has multiple party names repeated, here's a sample;

Unaffiliated
Federalist
Democratic-Republican
Democratic-Republican
Republican
Democratic-Republican
Democratic
Democratic
Republican

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.



std::vector<std::string>affiliation;
std::vector<std::string>unique;

for (int i=0; i<affiliation.size(); i++)
{
for (int j=0; j<unique.size(); j++)
{
if (affiliation.at(i) != unique.at(j))


}
}
Just stick them in a set<string>

Like so ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <set>
using namespace 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"     );


   set<string> affiliation;
   for ( string president, party; getline( in, president, ',' ) && getline( in, party ); ) affiliation.insert( party );

   for ( string s : affiliation ) cout << s << '\n';
}

Democratic-Republican
Federalist
Unaffiliated



or, if you want the full list and the unique list ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <set>
using namespace 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
Last edited on
I do not want to use std::sort or std::unique or anything of that kind, just the most basic way to do it.

Normally the most basic and recommended way to do things is to use what the C++ libraries offers.
Yea bu this is an assignments for a class and we were instructed to do so using loops .
Ok, that's different.
Have a lokk at this example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
  vector<string> all_names = { "Anna", "Lisa", "Jenni", "Kati", "Mariya",
                              "Lisa", "Anna", "Kati" };
  vector<string> unique_names;

  for (const string& name : all_names)
  {
    bool found = false;
    if (unique_names.empty())
    {
      unique_names.push_back(name);
    }
    else
    {
      for (size_t i = 0; i < unique_names.size(); i++)
      {
        if (unique_names[i] == name)
          found = true;
      }
      if (!found)
        unique_names.push_back(name);
    }
  }

  // output unique_words
  cout << "Found " << unique_names.size() << " names\n";
  for (const string& name : unique_names)
    cout << name << ' ';
}

Output:

Found 5 names
Anna Lisa Jenni Kati Mariya
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".
Topic archived. No new replies allowed.