0

I am trying to print a database using OOP in c++. But in my file.csv there are a lot of similar elements, so I'm trying to print each name only once. I know this is vague, but for you experts, this is an easy one. If possible please use basic coding.

wychmaster
  • 712
  • 2
  • 8
  • 23
  • Maybe you should use a hash_map not a vector to store those strings, or if you must, you can use a hash_map for checking work before you put that string into you Vector. – Hu Xixi Jun 05 '20 at 02:44
  • Use `std::unordered_set` instead of `std::vector`. – Rotem Jun 05 '20 at 02:45

2 Answers2

2

If you are not limited to vector, use unordered_set to populate the data. This is implemented using a hash table. A sample program looks like below.

#include <unordered_set>
#include <string>
#include <iostream>
using namespace std;
int main() {
   unordered_set <string> data; 
   data.insert("code1");
   data.insert("code2");
   //duplicate
   data.insert("code2");

   cout << "\nAll elements : "; 
   unordered_set<string> :: iterator itr; 
   for (itr = data.begin(); itr != data.end(); itr++) 
       cout << (*itr) << endl;
   return 0;
}
djacob
  • 110
  • 1
  • 9
0

You can insert some sql statement in your c++ code to filter out similar elements in your database before you store/use it in your program. Take a look at this page How can i insert values into database(mySql) using cpp program? and this page https://www.w3schools.com/sql/sql_where.asp

Without database, this may help you Filter out duplicate values in array in C++