Problem
/*
// Given two lists of strings build a new list that has all strings that appear in both the original lists. if the same string appears more than once output it as same times as it appears in both lists
//
// Example:
// "Dog", "bird", "elephant", "dog", "dog", "cat"
// "Cat", "dog", "dog", "cat", "cat", "fish"
// Result (order doesn' t matter)
// "Dog", "dog", "cat"
*/
Solution
[Cpp]
# Include <iostream>
# Include <list>
# Include <iterator>
# Include <algorithm>
# Include <string>
Using namespace std;
Void find_comm_strings (list <string> & output, list <string> & listA, list <string> & listB)
{
ListA. sort ();
ListB. sort ();
List <string >:: const_iterator citA = listA. begin ();
List <string >:: const_iterator citB = listB. begin ();
While (citA! = ListA. end () & citB! = ListB. end ()){
Int eq = (* citA). compare (* citB );
If (eq = 0 ){
Output. push_back (* citA );
CitA ++;
CitB ++;
}
Else if (eq> 0 ){
CitB ++;
}
Else {
CitA ++;
}
}
}
Int main (int argc, char * argv [])
{
List <string> listA;
List <string> listB;
List <string> output;
Cout <"list A:" <endl;
ListA. push_back ("dog ");
ListA. push_back ("bird ");
ListA. push_back ("elephant ");
ListA. push_back ("dog ");
ListA. push_back ("dog ");
ListA. push_back ("cat ");
Copy (listA. begin (), listA. end (), ostream_iterator <string> (cout ,","));
Cout <endl;
Cout <"list B:" <endl;
ListB. push_back ("cat ");
ListB. push_back ("dog ");
ListB. push_back ("dog ");
ListB. push_back ("cat ");
ListB. push_back ("cat ");
ListB. push_back ("fish ");
Copy (listB. begin (), listB. end (), ostream_iterator <string> (cout ,","));
Cout <endl;
Find_comm_strings (output, listA, listB );
Cout <"common strings" <endl;
Copy (output. begin (), output. end (), ostream_iterator <string> (cout ,","));
Cout <endl;
Return 0;
}
# Include <iostream>
# Include <list>
# Include <iterator>
# Include <algorithm>
# Include <string>
Using namespace std;
Void find_comm_strings (list <string> & output, list <string> & listA, list <string> & listB)
{
ListA. sort ();
ListB. sort ();
List <string >:: const_iterator citA = listA. begin ();
List <string >:: const_iterator citB = listB. begin ();
While (citA! = ListA. end () & citB! = ListB. end ()){
Int eq = (* citA). compare (* citB );
If (eq = 0 ){
Output. push_back (* citA );
CitA ++;
CitB ++;
}
Else if (eq> 0 ){
CitB ++;
}
Else {
CitA ++;
}
}
}
Int main (int argc, char * argv [])
{
List <string> listA;
List <string> listB;
List <string> output;
Cout <"list A:" <endl;
ListA. push_back ("dog ");
ListA. push_back ("bird ");
ListA. push_back ("elephant ");
ListA. push_back ("dog ");
ListA. push_back ("dog ");
ListA. push_back ("cat ");
Copy (listA. begin (), listA. end (), ostream_iterator <string> (cout ,","));
Cout <endl;
Cout <"list B:" <endl;
ListB. push_back ("cat ");
ListB. push_back ("dog ");
ListB. push_back ("dog ");
ListB. push_back ("cat ");
ListB. push_back ("cat ");
ListB. push_back ("fish ");
Copy (listB. begin (), listB. end (), ostream_iterator <string> (cout ,","));
Cout <endl;
Find_comm_strings (output, listA, listB );
Cout <"common strings" <endl;
Copy (output. begin (), output. end (), ostream_iterator <string> (cout ,","));
Cout <endl;
Return 0;
}
Output
[Cpp]
List:
Dog, bird, elephant, dog, dog, cat,
List B:
Cat, dog, dog, cat, cat, fish,
Common strings
Cat, dog, dog,
Press any key to continue...
List:
Dog, bird, elephant, dog, dog, cat,
List B:
Cat, dog, dog, cat, cat, fish,
Common strings
Cat, dog, dog,
Press any key to continue...