stl之3 set 与map

来源:互联网 发布:淘宝男装店铺排行潮男 编辑:程序博客网 时间:2024/05/12 02:33
#pragma once#include <iostream>#include <fstream>#include <string>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <iterator>#include <algorithm>namespace myPrint{template<class T>void PRINT_ELEMENT(const T &coll, char *pStr = ""){typename T::const_iterator pos;std::cout << pStr;for (pos = coll.begin(); pos!= coll.end(); ++pos){std::cout <<*pos << " ";}std::cout << std::endl;}}namespace mySet{   template<class T>class CompareMode{public:enum com_mod{ normal, reversed};public:CompareMode(com_mod mod = normal): Mod(mod) { }bool operator()(const T &vLeft, const T &vRight) const{return Mod == normal ? vLeft < vRight : vRight < vLeft;}bool operator==(const CompareMode &rhs){return Mod == rhs.Mod;}private:com_mod Mod;};typedef std::set<int, CompareMode<int> > IntSet; //有两个构造函数void fun(){ CompareMode<int> sort_mod(CompareMode<int>::reversed);IntSet s(sort_mod);for (int i = 1; i < 50; ++i){int x = rand() % 100;s.insert(x);}myPrint::PRINT_ELEMENT(s);}}namespace myMap{struct myString{std::string cmp1;std::string cmp2;std::string cmp3;myString():cmp1(""), cmp2(""), cmp3("") { }myString(const std::string &s1, const std::string &s2, const std::string &s3):cmp1(s1), cmp2(s2), cmp3(s3) { }};class cmpMyString{public:enum com_mod{First, Second, Last};public:cmpMyString(const com_mod mod = First): Mod(mod) { }bool operator() (const myString &vL, const myString &vR){if (Mod == First){if (vL.cmp1 != vR.cmp1) return vL.cmp1 < vR.cmp1;else if (vL.cmp2 != vR.cmp2) return vL.cmp2 < vR.cmp2;return vL.cmp3 < vR.cmp3;}if(Mod == Second){if (vL.cmp2 != vR.cmp2) return vL.cmp2 < vR.cmp2;else if (vL.cmp1 != vR.cmp1) return vL.cmp1 < vR.cmp1;return vL.cmp3 < vR.cmp3;}if(Mod == Last){if (vL.cmp3 != vR.cmp3) return vL.cmp3 < vR.cmp3;else if (vL.cmp1 != vR.cmp1) return vL.cmp1 < vR.cmp1;return vL.cmp2 < vR.cmp2;}}private:com_mod Mod;};typedef std::map<myString, int, cmpMyString> StringIntMap;void print(const std::pair<myString, int> &p){std::cout << p.first.cmp1 << " "      << p.first.cmp2 << " "  << p.first.cmp3 << " "   << p.second << std::endl;}void fun(){//StringIntMap mp((cmpMyString(cmpMyString::Second)));StringIntMap mp(cmpMyString(cmpMyString::Second));//waring warning C4930: “myMap::StringIntMap //mp(myMap::cmpMyString)”: 未调用原型函数(是否是有意用变量定义
mp.insert(std::pair<myString, int>(myString("1", "3", "4"), 1));//error C2228: “.insert”的左边必须有类/结构/联合mp.insert(std::pair<myString, int>(myString("1", "2", "4"), 2));mp.insert(std::pair<myString, int>(myString("2", "3", "4"), 3));mp.insert(std::pair<myString, int>(myString("3", "2", "4"), 4));mp.insert(std::pair<myString, int>(myString("1", "2", "1"), 5));mp.insert(std::pair<myString, int>(myString("1", "3", "5"), 6));for_each(mp.begin(), mp.end(), print); std::cout << std::endl;myString s1("1", "3", "4");myString s2("2", "5", "4");std::swap(s1, s2);std::cout << s1.cmp1 << " "      << s1.cmp2 << " "      << s1.cmp3 << " "   << std::endl;std::cout << s2.cmp1 << " "      << s2.cmp2 << " "      << s2.cmp3 << " "       << std::endl;}
        
        void print1(const std::pair<int, int> &p){std::cout << p.first << " "      << p.second << std::endl;}        typedef std::multimap<int, int> IntIntMap;void fun1(){IntIntMap mp;for(int i = 0; i < 25; ++i){mp.insert(std::pair<int, int>(rand()%25, rand()%50));}for_each(mp.begin(), mp.end(), print1);IntIntMap::iterator it = mp.lower_bound(11);if (it != mp.end()) std::cout << "I find it! " << it->first <<" " << it->second << std::endl;else std::cout << "failed!" << std::endl;IntIntMap::iterator it1 = mp.upper_bound(11);if (it1 != mp.end()) std::cout << "I find it! " << it1->first <<" " << it1->second  << std::endl;else std::cout << "failed!" << std::endl;std::pair<IntIntMap::iterator, IntIntMap::iterator> It = mp.equal_range(10);IntIntMap::iterator x = It.first;std::cout << x->first  <<" " << x->second  << std::endl;x = It.second;std::cout << x->first  <<" " << x->second  << std::endl;for (IntIntMap::iterator x = mp.begin(); x != mp.end(); ){if (x->second == 11){//mp.erase(x);//runtime errormp.erase(x++);}else{++x;}}for_each(mp.begin(), mp.end(), print1);}

}