Reputation: 43
in my small project i want to make small program where I have to store unlimited no of unique string, but user can input same unique string multiple times. But in my array I want only unique id to be saved only once. In simple word I dont want duplicate data in my array. I want to do this in C++, But somehow I cant get the logic ? Can someone please help me out here?
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
string str[100],ch;
int i,j,n;
j=0;n=0;
//str[0]= "a";
do {
getline(cin,ch);
for (i=0;i <j; i++){
if (ch=str[i]){
cout << "duplicate" ;
}
str[i] =ch;
j++;
}
n++;
} while (n =100);
getchar();
}
I am noob at C++, so please help me out here
Upvotes: 1
Views: 4963
Reputation: 36082
haven't compiled this but something like this should work, that said you should use a set
or similar more c++sh ways of solving this if you want a more efficient solution but from the sound of it you seem to need more basic advice.
int main()
{
const int maxstrings = 100; // never use magic numbers, instead declare them as a constant
string str[maxstrings],ch; // should have other variable names that are more descriptive
int i,n = 0; // didn't see the need for j here, n contains number of unique strings
do
{
getline(cin,ch);
// you may want to check what is in ch first, to see if user doesn't want to enter 100 strings
bool duplicate = false;
for (i=0; !duplicate && i<n; ++i) // check among number of stored strings (n)
{
if (ch==str[i]) // use two '=' for equality i.e '=='
{
cout << "duplicate:" << ch << endl; // show the duplicate, user friendlier
duplicate = true;
}
}
// did we find a duplicate? no, then add to array
if ( !duplicate )
{
str[n++]=ch;
}
}
while ( n < maxstrings );
getchar();
}
Upvotes: 2
Reputation: 101456
If you want to maintain a list of unique strings
, then the easiest thing to do is use the right tool for the job; namely, a set<string>
rather than an array of string
.
Edit:
If you don't need your collection of strings to be sorted (as set
does), and you have it available to you, it would be more appropriate to use unordered_set
rather than set
. set
will just do needless sorting every time you add a string.
Edit2:
A set
is an associative array, which means there can only be one element with a given key. In the case of set<string>
, the key is the string
you insert. If you insert the same key multiple times, there will still only be one instance of it in the set
.
Here's an example program that illustrates this. If you run this you'll find the output is just one "foo", even though "foo" was inserted 3 times:
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
set<string> my_strings;
my_strings.insert("foo");
my_strings.insert("foo");
my_strings.insert("foo");
copy( my_strings.begin(), my_strings.end(), ostream_iterator<string>(cout, "\n"));
}
Upvotes: 7
Reputation: 851
You should use a vector-like to keep a list of strings. For example you can use a set (http://www.cplusplus.com/reference/stl/set/).
Besides that, if you need to check if the string already exist on the set<> object, then you need to check for it using find() method: http://www.cplusplus.com/reference/stl/set/find/
I think that's all you need.
FYI: the line: if (ch=str[i]){ is totally wrong! you are not comparing ! you are assigning, remember to use '==' and not '='.
Upvotes: 0