So i'm looking for a soultion to print duplicate strings in a vector. If the vector contains {a,b,c,a,d,a,a} i want to print each "a" out of the vector. For the code i'm using i get only 1 "a " out of 2.
Here is an example I had in one of my projects, should help you out:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
string stringToSearch = "This is a string of text to search.";
string stringToFind{};
cout << stringToSearch << endl;
cout << "Enter a string to find" << endl;
getline(cin, stringToFind);
int wordAppearance{};
for (int position = stringToSearch.find(stringToFind, 0); position != string::npos; position = stringToSearch.find(stringToFind, position))
{
cout << "Found " << ++wordAppearance << " instances of " << stringToFind << " at
position " << position << endl;
position++;
}
You are only pushing the duplicate strings to the vector - which doesn't include the first one found. So for a b c a you show a and for a b c a d a a you show aaa.
Why do you want to show the duplicate occurrences?
As an alternative, depending upon what you are trying to achieve, consider using a map to store the number of occurrences of a string and then deal with those strings that have an occurrence > 1.
Sorry for presenting only bits. This is the whole problem.
Loop over the given array of strings to build a result string like this: when a string appears the 2nd, 4th, 6th, etc. time in the array, append the string to the result. Return the empty string if no string appears a 2nd time.
it should be done using vector <string>. And i tough about using map but never really used them.
To store only the 2nd, 4th, 6th, etc appearances of the string, you could count the appearance number in your loop and append the duplicate if it's even. One caution though, be sure to reset your counter when you see a string that isn't a duplicate.
you can do it faster ways (sorting goes through the data more than once), but the easy solution is to sort the input array (or a copy of it if you need it again later) and iterate it. then you have a,a,a,a,b,b,b,b,c,c,c, .... and you can be assured that when you hit b there are no more a's after it, and so on. you just count or print or whatever. Unless your arrays are exceedingly large (many millions) it would still do it in < 1 second.
so i made it. Is it good? "umm no". Does it work? "i think so".
Didn't you overcomplicate? "yes". Should it ever be used "Oh god no".
I'm happy if someone can post something not overcomplicated just so i can learn from it.
Thanks for all the help!
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
usingnamespace std;
string wordAppend(vector<string> strings)
{
string result;
sort(strings.begin(), strings.end());
for (int i = 1; i < strings.size(); i++)
{
if (strings[i - 1] == strings[i])
{
result += strings[i] + ' ';
}
}
return result;
}
int main()
{
std::string result;
vector <string> strings
{ "a", "b", "c", "a", "c", "a", "a" };
//{ "a", "b", "d", "e", "f", "g", "h" };
result = wordAppend(strings);
if (result.size())
{
std::cout << "\n The duplicate strings are:\n " << result << '\n';
}
else
{
std::cerr << "\n There are no duplicates.\n";
}
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
I was playing with the program and came up with this output by changing line 18.
[0] This is a dup
[1] This is a dup
[2] This is a dup
[3] This is a string
[4] This is string 2
[5] This is string 3
[6] This is string 4
The duplicate strings are:
[1] This is a dup
[2] This is a dup
Press Enter to continue:
The for loop for the first part was mostly for checking the sort. Not something I planed to leave.
Loop over the given array of strings to build a result string like this: when a string appears the 2nd, 4th, 6th, etc. time in the array, append the string to the result. Return the empty string if no string appears a 2nd time.