RuntimeStringCmp.cpp
#includeusing namespace std;// function object to compare strings// - allows you to set the comparison criterion at runtime// - allows you to compare case insensitiveclass RuntimeStringCmp {public: // constants for the comparison criterion enum cmp_mode { normal, nocase };private: // actual comparison mode const cmp_mode mode; // auxiliary function to compare case insensitive static bool nocase_compare(char c1, char c2) { return toupper(c1) < toupper(c2); }public: // constructor: initializes the comparison criterion RuntimeStringCmp(cmp_mode m = normal) : mode(m) { } // the comparison bool operator() (const string& s1, const string& s2) const { if (mode == normal) { return s1
MapAdvanceTest.h
#ifndef _Stl_Container_Map_Advance_Test_H_#define _Stl_Container_Map_Advance_Test_H_#include "../../TestBase.h"#include
MapAdvanceTest.cpp
#include
cout << "#############################################" << endl;
}void MapAdvanceTest::runtimeMapCompare(){ // create a container with the default comparison criterion StringStringMap coll1; fillAndPrint(coll1); // create an object for case-insensitive comparisons RuntimeStringCmp ignorecase(RuntimeStringCmp::nocase); // create a container with the case-insensitive comparisons criterion StringStringMap coll2(ignorecase); fillAndPrint(coll2);}void MapAdvanceTest::run(){ printStart("runtimeMapCompare()"); runtimeMapCompare(); printEnd("runtimeMapCompare()");}
运行结果:
---------------- runtimeMapCompare(): Run Start ----------------
Bestatter undertakerDeutschland GermanyHaken snagHund dogUnternehmen enterprisearbeiten workdeutsch Germangehen walkunternehmen undertake#############################################
arbeiten workBestatter undertakerdeutsch GermanDeutschland Germanygehen walkHaken snagHund dogUnternehmen undertake#############################################
---------------- runtimeMapCompare(): Run End ----------------