文章目录
- 1.set的原型
- 2.set的成员函数
-
- 3.map的原型
- 4.map的成员函数
-
- 5.OJ练习
-
- 1.前K个高频单词
- 2.两个数组的交集
- 3.随即链表的复制
1.set的原型
template <
class T,
class Compare = less<T>,
class Alloc = allocator<T>
>
class set;
2.set的成员函数
1.构造函数
explicit set(const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
template <class InputIterator>
set(InputIterator first, InputIterator last, const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
set(const set& x);
2.代码演示
void test_set1()
{
set<int> s;
s.insert(3);
s.insert(1);
s.insert(4);
s.insert(2);
s.insert(1);
s.insert(2);
set<int>::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
}
void test_set2()
{
set<int> s;
s.insert(3);
s.insert(1);
s.insert(4);
s.insert(2);
s.insert(1);
s.insert(2);
s.erase(30);
auto pos = s.find(30);
if (pos != s.end())
{
s.erase(pos);
}
int x;
while (cin >> x)
{
if (s.count(x))
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
}
set<int> s1;
set<int>::iterator itlow, itup;
for (int i = 1; i < 10; i++)
s1.insert(i * 10);
itlow = s1.lower_bound(25);
itup = s1.upper_bound(60);
s1.erase(itlow, itup);
cout << "s1 contains:";
for (auto it = s1.begin(); it != s1.end(); ++it)
cout << ' ' << *it;
cout << '\n';
}
void test_set3()
{
multiset<int> ms;
ms.insert(3);
ms.insert(1);
ms.insert(4);
ms.insert(2);
ms.insert(1);
ms.insert(1);
ms.insert(1);
ms.insert(2);
multiset<int>::iterator mit = ms.begin();
while (mit != ms.end())
{
cout << *mit << " ";
++mit;
}
cout << endl;
auto pos = ms.find(1);
while (pos != ms.end() && *pos == 1)
{
cout << *pos << " ";
++pos;
}
cout << endl;
cout << "1的个数" << ms.count(1) << endl;
ms.erase(1);
cout << "1的个数" << ms.count(1) << endl;
cout << "2的个数" << ms.count(2) << endl;
auto pos = ms.find(3);
if (pos != ms.end())
{
ms.erase(pos);
}
++pos;
if (pos != ms.end())
{
ms.erase(pos);
}
}
3.map的原型
template <
class Key,
class T,
class Compare = less<Key>,
class Alloc = allocator<pair<const Key,T> >
>
class map;
4.map的成员函数
1.构造函数
explicit map(const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
template <class InputIterator>
map(InputIterator first, InputIterator last, const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
map(const map& x);
2.代码演示
void test_map1()
{
map<string, string> m;
pair<string, string> p("Kevin", "凯文");
m.insert(p);
m.insert(pair<string, string>("Kevin", "凯文"));
m.insert(make_pair("Eddie", "彭于晏"));
m.insert(make_pair("Tom", "汤姆"));
m.insert(make_pair("Jerry", "杰瑞"));
auto it = m.begin();
while (it != m.end())
{
cout << it->first << "-" << it->second << endl;
++it;
}
cout << endl;
for (const auto& e : m)
{
cout << e.first << "-" << e.second << endl;
}
cout << endl;
}
void test_map2()
{
map<string, string> m;
m.insert(make_pair("Eddie", "彭于晏"));
m.insert(make_pair("Tom", "汤姆"));
m.insert(make_pair("Jerry", "杰瑞"));
m["abc"];
m["ABC"] = "牛顿";
m["Eddie"] = "埃迪";
cout << m["string"] << endl;
}
void test_map3()
{
string s[] = { "陀螺", "陀螺", "洋娃娃", "陀螺", "洋娃娃", "洋娃娃", "陀螺",
"洋娃娃", "悠悠球", "洋娃娃", "悠悠球", "乐高" };
map<string, int> count;
for (auto& key : s)
{
count[key]++;
}
for (auto& toy : count)
{
cout << toy.first << ":" << toy.second << endl;
}
}
5.OJ练习
1.前K个高频单词
前K个高频单词

2.两个数组的交集
两个数组的交集

3.随即链表的复制
随机链表的复制
