Comparing lists of strings

I need to count the number of strings that occur in the first list, but not the second list.

This is what i have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 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
			else if (bIt != aIt) {
				++result;
				++bIt;
			}
//else they are the same then do not add to result and iterate both
			else { ++aIt; ++bIt; }
		}
		return result;
	}

and i do not know why it doesn't work
Last edited on
You can use set_difference - http://www.cplusplus.com/reference/algorithm/set_difference/ . Here's an example that will alter the lists (sort them). If you don't like that, you can pass copies.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>
#include <list>

#include <iostream>     // std::cout
#include <algorithm>    // std::set_difference, std::sort
#include <vector>       // std::vector

using std::list;
using std::vector;
using std::string;

int numberOfStringsInAButNotInB(list<string> & A, list<string> & B)
{
	A.sort();
	B.sort();

	std::vector<string> difference(A.size());

	auto it = std::set_difference(A.begin(), A.end(), B.begin(), B.end(), difference.begin());
	difference.resize(it - difference.begin());
	
	return difference.size();
}


int main()
{
	list<string> a{ "cat", "aa", "bb", "cc" };
	list<string> b{ "dog", "bb", "aa"};

	std::cout << numberOfStringsInAButNotInB(a, b);
}


As an aside, only use lists when you know you need them. For a great many common situations, a vector performs better.
Last edited on
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.
Actually, while i get most of it and it works, What in the heck is auto?
asked and answered http://www.cplusplus.com/forum/general/103388/
Last edited on
Repeater can you show me how that would work with 2 files ?
Read in all the words from one file into one list.
Then read in all the words from the other file into the other list.

http://www.cplusplus.com/doc/tutorial/files/

Then do as above.
From file. Disallowing (or assuming no) repeats.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <sstream>
#include <string>
#include <set>
#include <iterator>
using namespace 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 );
}

Topic archived. No new replies allowed.