[OCLUG-devel] Matching Strings

Christopher Smith x at xman.org
Thu Aug 4 14:30:35 PDT 2005


Hmm... I don't have time to look at your code right now, but the
algorithm is pretty straight forward for doing this right. In C++ it'd
be something like (filling out the necessary types and various bits of
prologue and epilogue code is left as an excercise to the reader):

//keep going until we've seen all the data
while (my_ifstream) {
        //read a new line
	getline(buffer, my_ifstream);
        //get whatever section is relevant as a key
	string key = buffer.substr(0, n);
        //check to see if we've seen this key before
	my_map_iterator = my_map.find(key);
	if (my_map_iterator != my_map.end()) {
                //check if we've printed out the previously
		//scanned line before
		if (my_map_iterator->second) {
			//print out the previous line
                	cout << *(my_map_iterator->second) << endl;
			//set last_time to null and wipe
			//out the string
			auto_ptr<string> temp(my_map_iterator->second);
			my_map_iterator->second = 0;
                }
		//print out this line
		cout << buffer << endl;
	} else {
		//add this new key to our map
		my_map_iterator.insert(make_pair(key, new string(buffer)));
        }
}

--Chris


More information about the OCLUG-devel mailing list