stl之3 set 与map
2013-09-08 16:25
204 查看
#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 error
mp.erase(x++);
}else
{
++x;
}}
for_each(mp.begin(), mp.end(), print1);
}
}
相关文章推荐
- STL基本容器: string、vector、list、deque、set、map
- STL容器 set、 multiset、map、multimap
- STL之map、set数据结构基础
- vector,list,deque,set,map of STL
- vector,list,deque,set,map of STL
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
- [置顶] STL——set && map的使用
- STL中list,vector,deque,map,set区别、联系和使用场景
- STL下的map和set
- STL中关于map和set的四个问题?
- STL中map、set的数据结构及底层实现
- STL中map、set的数据结构及底层实现
- POJ 3297 Open Source STL(map+set+pair)
- STL中关于map和set的四个问题
- 温故篇之STL_map,set的一些应用
- C++之STL set和map解析(4)---《C++ STL》
- C++ STL容器的学习使用(vector、queue、list、set、map)
- STL容器 map 与 set 的用法
- C++ STL关联容器 set和map学习笔记
- STL库的map和set的使用