// pre: A and B are sorted.
// post: The number of strings in A that do not occur in B
// has been returned.
int unmatched(list<string> & A, list<string> & B)
{
//declaring iterators starting at the beinging of a list
std::list<string>::iterator aIt = A.begin();
std::list<string>::iterator bIt = B.begin();
int result = 0;
{
//go until you reach the end
while (aIt != A.end() && bIt != A.end());
{
// if A is not equal to B add to result and iterate A
if (aIt!=bIt)
{
++result; ++aIt;
}
//If B is not equal to A add to result and iterate B
elseif (bIt != aIt) {
++result;
++bIt;
}
//else they are the same then do not add to result and iterate both
else { ++aIt; ++bIt; }
}
return result;
}
I was having trouble understanding the inputs for that STL function. Thank you very much i was missing the output location when i tried it and thats why i couldnt get that to work.
#include <iostream>
#include <sstream>
#include <string>
#include <set>
#include <iterator>
usingnamespace std;
int numAnotB( istream &strmA, istream &strmB ) // better with filenames, but here streams for testing
{
using it = istream_iterator<string>;
set<string> S( it{ strmB }, {} ); // reads DISTINCT elements of B into a set
int BSize = S.size(); // stores number of (distinct) elements in B
copy( it{ strmA }, {}, inserter( S, S.end() ) ); // attempt to put elements of A into the set (only distinct ones go in)
return S.size() - BSize; // number of DISTINCT elements that could be added
}
int main()
{
stringstream A( "cat aa bb cc" ); // just to simulate files for now
stringstream B( "dog bb aa" ); //
cout << numAnotB( A, B );
}