Set_Map/Windows_Set_Map/Main.cpp

370 lines
8.6 KiB
C++
Raw Normal View History

#include <iostream>
#include <set>
#include <map>
#include "Set.hpp"
#include "Map.hpp"
using namespace std;
using namespace Lenyiin;
// <20><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>: vector/list/string/deque/array/forward_list
// <20><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>: set/multiset/map/multimap/unordered_set/unordered_multiset/unordered_map/unordered_multimap
void test_set()
{
set<int> s1; // setĬ<74><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>С<EFBFBD><D0A1><EFBFBD><EFBFBD> set<65>ײ<EFBFBD><D7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
s1.insert(10);
s1.insert(20);
s1.insert(5);
s1.insert(2);
s1.insert(1);
s1.insert(8);
s1.insert(14);
s1.insert(44);
s1.insert(23);
s1.insert(0);
s1.insert(2);
s1.insert(1);
s1.insert(8);
s1.insert(14);
// <20><><EFBFBD><EFBFBD> + ȥ<><C8A5>
set<int>::iterator it = s1.begin();
while (it != s1.end())
{
//*it = 100; // set<65>е<EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>
cout << *it << " ";
it++;
}
cout << endl;
// <20><>Χfor
for (auto e : s1)
{
cout << e << " ";
}
cout << endl;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <09>ײ<EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EEBFBD>
set<int> s2(s1);
for (auto e : s2)
{
cout << e << " ";
}
cout << endl;
// ɾ<><C9BE>
// find()<29>dz<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>set/multiset/map/multimap
// set<int>::iterator pos = s2.find(5);
auto pos = s2.find(5);
// find()<29>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// s.find() O(logN) s.begin() O(1) s.end() O(1)
// find(s.begin(), s.end(), 5) O(N) Ч<>ʻ<EFBFBD><CABB>Ͳ<EFBFBD><CDB2><EFBFBD>
// auto pos = find(s2.begin(), s2.end(), 5);
if (pos != s2.end())
{
s2.erase(pos);
}
for (auto e : s2)
{
cout << e << " ";
}
cout << endl;
// <20><><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ɾ
s2.erase(8);
s2.erase(80); // <20>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><E1B1A8>
for (auto e : s2)
{
cout << e << " ";
}
cout << endl;
}
void test_map1()
{
map<int, int> m1;
m1.insert(pair<int, int>(1, 1)); // pair<69><72>һ<EFBFBD><D2BB>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><E0A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>pair<69><72><EFBFBD><EFBFBD>
m1.insert(pair<int, int>(3, 3));
m1.insert(pair<int, int>(2, 2));
m1.insert(pair<int, int>(5, 5));
m1.insert(pair<int, int>(6, 6));
m1.insert(pair<int, int>(4, 4)); // pair<69><72><EFBFBD><EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD>뵽map<61><70>
// <20>ճ<EFBFBD><D5B3><EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD>make_pair()<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>pair<69><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>
m1.insert(make_pair(7, 7)); // <20><><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><E5B9B9>һ<EFBFBD><D2BB>pair<69><72><EFBFBD><EFBFBD>
map<int, int>::iterator it = m1.begin();
while (it != m1.end())
{
// cout << *it << " "; // C++<2B><>֧<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
cout << (*it).first << " " << (*it).second << endl; // operator*()<29><><EFBFBD>ص<EFBFBD><D8B5>ǽڵ<C7BD><DAB5><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
cout << it->first << " " << it->second << endl; // operator->()<29><><EFBFBD>ص<EFBFBD><D8B5>ǽڵ<C7BD><DAB5><EFBFBD>ָ<EFBFBD><D6B8> Ҳ<><D2B2><EFBFBD><EFBFBD>pair<k, v><3E><>ָ<EFBFBD><D6B8>
it++;
}
cout << endl;
// <20><>Χfor
for (auto& e : m1)
{
cout << e.first << " " << e.second << endl;
}
cout << endl;
}
void test_map2()
{
std::map<std::string, std::string> dirt;
dirt.insert(pair<std::string, std::string>("sort", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
dirt.insert(make_pair("string", "<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>"));
dirt.insert(make_pair("left", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>")); // <20><><EFBFBD><EFBFBD>ASCII<49><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
std::map<std::string, std::string>::iterator it = dirt.begin();
while (it != dirt.end())
{
cout << it->first << " " << it->second << endl;
it++;
}
cout << endl;
}
void test_map3()
{
// ͳ<>Ƶ<EFBFBD><C6B5>ʳ<EFBFBD><CAB3>ֵĴ<D6B5><C4B4><EFBFBD>
string strArr[] = { "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ӣ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ӣ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ӣ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ӣ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ӣ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>" };
map<string, int> countMap;
// <20><>һ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
for (auto& str : strArr)
{
map<string, int>::iterator pos = countMap.find(str);
if (pos != countMap.end())
{
pos->second++;
}
else
{
countMap.insert(make_pair(str, 1));
}
}
for (auto& e : countMap)
{
cout << e.first << " " << e.second << endl;
}
cout << endl;
// <20>ڶ<EFBFBD><DAB6>ַ<EFBFBD><D6B7><EFBFBD>
for (auto& str : strArr)
{
pair<map<string, int>::iterator, bool> ret = countMap.insert(make_pair(str, 1));
if (ret.second == false) // <20><><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>˵<EFBFBD><CBB5><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD>
{
ret.first->second++; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
}
}
for (auto& e : countMap)
{
cout << e.first << " " << e.second << endl;
}
cout << endl;
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
for (auto& str : strArr)
{
// 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˮ<EFBFBD><CBAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>map<61>У<EFBFBD><D0A3><EFBFBD>[]<5D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>pair<str, 0>, <20><><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3><EFBFBD><EFBFBD><EFBFBD>󣨴<EFBFBD><F3A3A8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ý<EFBFBD><C3BD><EFBFBD>++
// 2<><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˮ<EFBFBD><CBAE><EFBFBD><EFBFBD>map<61>У<EFBFBD><D0A3><EFBFBD>[]<5D><EFBFBD><E1B7B5>ˮ<EFBFBD><CBAE><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>ӳ<EFBFBD><D3B3><EFBFBD><EFBFBD><EFBFBD>󣨴<EFBFBD><F3A3A8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>++
countMap[str]++;
}
countMap["<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"]; // <20><><EFBFBD><EFBFBD> һ<><EFBFBD><E3B2BB>ô<EFBFBD><C3B4>
countMap["<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"] = 100; // <20>޸<EFBFBD>
cout << countMap["<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"] << endl; // <20><><EFBFBD><EFBFBD>
countMap["<EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD>"] = 5; // <20><><EFBFBD><EFBFBD> + <20>޸<EFBFBD>
// һ<><D2BB>ʹ<EFBFBD><CAB9>operator[]ȥ
// 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD> + <20>޸<EFBFBD>
// 2<><32><EFBFBD>޸<EFBFBD>
// һ<><EFBFBD><E3B2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD><D2A3><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>key<65><79><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD>
for (auto& e : countMap)
{
cout << e.first << " " << e.second << endl;
}
cout << endl;
}
void test_multi()
{
// multiset<65><74>set<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>
multiset<int> s1; // ʹ<>ú<EFBFBD>setһ<74><D2BB><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>multiset<65><74><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>Ԫ<EFBFBD><D4AA>
s1.insert(10);
s1.insert(20);
s1.insert(5);
s1.insert(2);
s1.insert(1);
s1.insert(8);
s1.insert(8);
s1.insert(14);
s1.insert(44);
s1.insert(23);
s1.insert(0);
s1.insert(2);
s1.insert(1);
s1.insert(8);
s1.insert(8);
s1.insert(14);
for (auto e : s1)
{
cout << e << " ";
}
cout << endl;
auto pos = s1.find(8); // <20><><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>Ԫ<EFBFBD>صĵ<D8B5>һ<EFBFBD><D2BB>Ԫ<EFBFBD>ص<EFBFBD>ַ
cout << *pos << endl;
pos++;
cout << *pos << endl;
pos++;
cout << *pos << endl;
pos++;
cout << *pos << endl;
pos++;
cout << *pos << endl;
// multimap<61><70>map<61><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
multimap<string, int> mm;
mm.insert(make_pair("ƻ<EFBFBD><EFBFBD>", 1));
mm.insert(make_pair("ƻ<EFBFBD><EFBFBD>", 1));
mm.insert(make_pair("ƻ<EFBFBD><EFBFBD>", 3));
mm.insert(make_pair("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 2));
mm.insert(make_pair("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 1));
for (auto& e : mm)
{
cout << e.first << " " << e.second << endl;
}
cout << endl;
}
// <20>Լ<EFBFBD>ʵ<EFBFBD>ֵ<EFBFBD> Set Map <20><><EFBFBD><EFBFBD>
void test_Set()
{
Set<int> s;
s.Insert(3);
s.Insert(4);
s.Insert(1);
s.Insert(2);
s.Insert(2);
s.Insert(5);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Set<int>::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
++it;
}
cout << endl;
// ɾ<><C9BE>
s.Erase(4);
// for
for (const auto& k : s)
{
cout << k << " ";
}
cout << endl;
cout << "IsRBTree? " << (s.IsRBTree() ? "true" : "false") << endl;
}
void test_Map1()
{
// int a[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
Map<int, int> m;
for (const auto& e : a)
{
m.Insert(make_pair(e, e));
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Map<int, int>::iterator it = m.begin();
while (it != m.end())
{
cout << it->first << ":" << it->second << endl;
++it;
}
cout << endl;
// ɾ<><C9BE>
m.Erase(4);
// <20><>Χfor<6F><72><EFBFBD><EFBFBD> for_each <20>ײ<EFBFBD>Ҳ<EFBFBD>ǵ<EFBFBD><C7B5>õĵ<C3B5><C4B5><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵı<D6B5><C4B1><EFBFBD>
for (const auto& kv : m)
{
cout << kv.first << ":" << kv.second << endl;
}
cout << endl;
cout << "IsRBTree? " << (m.IsRBTree() ? "true" : "false") << endl;
}
void test_Map2()
{
// ͳ<>Ƶ<EFBFBD><C6B5>ʳ<EFBFBD><CAB3>ֵĴ<D6B5><C4B4><EFBFBD>
string strArr[] = { "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ӣ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ӣ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ӣ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ӣ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ӣ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ƻ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD>" };
Map<string, int> countMap;
for (auto& str : strArr)
{
countMap[str]++;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Map<string, int>::iterator it = countMap.begin();
while (it != countMap.end())
{
cout << it->first << ":" << it->second << endl;
++it;
}
cout << endl;
// ɾ<><C9BE>
countMap.Erase("ƻ<EFBFBD><EFBFBD>");
for (const auto& kv : countMap)
{
cout << kv.first << ":" << kv.second << endl;
}
cout << endl;
cout << "IsRBTree? " << (countMap.IsRBTree() ? "true" : "false") << endl;
}
int main()
{
// test_set();
// test_map1();
// test_map2();
// test_map3();
// test_multi();
test_Set();
test_Map1();
test_Map2();
return 0;
}