c++ STL笔记 10
返回由lower_bound和upper_bound返回值组成的pair数对。主要用于查找多个相同的元素。返回由 lower_bound 和 upper_bound 返回值组成的 pair 数对。查找和key相同的元素。返回找到的第一个元素迭代器,没有找到返回尾后迭代器。在集合中查找需要的关键字val,成功返回元素的迭代器,失败返回尾后迭代器。均支持常见的 =,==,![ ] 中存放的是key值,
有序关联容器
| 容器类型 | 头文件 | 存储内容 | 唯一性 | 特点 |
| set | <set> | 只存储键(元素值=键值) | 键唯一,不允许重复键 | 有序的唯一键集合 |
| multiset | <set> | 同上 | 允许重复键 | 有序的可重复键集合 |
| map | <map> | 存储键值对 | 键唯一,每个键最多关联一个值 | 有序的唯一键值对(键映射值) |
| multimap | <map> | 同上 | 允许重复键,一个键客关联多个不同的值 | 有序的可重复键值对 |
初始化
set、multiset
template<typename T>
void Show(const T& v) {
for (const auto& x : v)
cout << x << " ";
cout << endl;
}
int main() {
set <int> s0; //创建一个空的int set集合
s0.insert(5); //插入数据
s0.insert(3);
s0.insert(1);
s0.insert(2);
s0.insert(4);
s0.insert(4); //set不支持相同数据,插入失败
multiset <int> s1; //创建一个空的int multiset集合
s1.insert(5); //插入数据
s1.insert(3);
s1.insert(1);
s1.insert(2);
s1.insert(4);
s1.insert(4); //multiset可以插入相同数据
//利用原来的set对象,创建一个新的set对象;multiset同理
set <int> s2(s0);
set <int> s3 = s0; //同上
//利用迭代器创建一个新的set对象;multiset同理
set <int> s4(s0.begin(), --s0.end());
//利用初始化列表创建一个set对象,可以无序;multiset同理
set <int> s5{ 1, 6, 3, 5, 4, 2 };
cout << "set:" << endl; //输出数据
cout << "s0:"; Show(s0);
cout << "multiset:" << endl;
cout << "s1:"; Show(s1);
cout << "s2:"; Show(s2);
cout << "s3:"; Show(s3);
cout << "s4:"; Show(s4);
cout << "s5:"; Show(s5);
//创建一个降序的set集合;multiset同理
set <int, greater<int>> s6;
s6.insert(10); //插入数据
s6.insert(40);
s6.insert(30);
s6.insert(20);
cout << "s6:"; Show(s6); //输出数据
return 0;
}

map、multimap
template<typename T>
void Show(const T& v) {
for (const auto& x : v)
cout << "(" << x.first << "," << x.second << ")";
cout << endl;
}
int main() {
//创建一个key为int,value为string的空map;multimap同理
map<int, string> m1;
//创建一个带3个元素的map;multimap同理
map<int, string> m2{ {1, "abc"}, { 2,"lpl" }, {3,"xyz"} };
//利用m2创建一个新的map对象;multimap同理
map<int, string> m3 = m2;
cout << "m1:"; Show(m1);
cout << "m2:"; Show(m2);
cout << "m3:"; Show(m3);
map<int, string> m4{ {1, "abc"}, { 1,"xyz" } };
//map一个键只能对应一个值,不能添加
cout << "map:\n"<< "m4:"; Show(m4);
multimap<int, string> m5{ {1, "abc"}, {1,"lpl" } };
//multimap一个键可以对应多个不同的值,可以添加
cout << "multimap:\n" << "m5:"; Show(m5);
return 0;
}

常用迭代器
set,multiset,map,multimap:
都支持双向迭代器,不支持随机迭代器
| 迭代器 | 含义 | 迭代器 | 含义 |
| begin() | 第一个元素的迭代器 | rbegin() | 第一个反向迭代器 |
| end() | 最后一个元素的下一个位置迭代器 | rend() | 尾后反向迭代器 |
| cbegin() | 第一个元素的常量迭代器 | crbegin() | 第一个常量反向迭代器 |
| cend() | 尾后常量迭代器 | crend() | 尾后常量反向迭代器 |
set,multiset:
int main() {
set <int> s1{ 1, 6, 3, 5, 4, 2 };
cout << "s1:";
cout << "从头到尾输出s1:";
for (auto p = s1.begin(); p != s1.end(); p++)
cout << *p << " ";
cout << endl;
cout << "从后往前输出s1:";
for (auto p = s1.rbegin(); p != s1.rend(); p++)
cout << *p << " ";
cout << endl;
auto p = s1.begin();
cout << "第二个元素:" << *(++p) << endl; //输出第二个元素
cout << "第一个元素:" << *(--p) << endl; //输出第一个元素
return 0;
}

map,multimap:
int main() {
map<int, string> m1{ {1, "abc"}, { 2,"lpl" }, {3,"xyz"},{4,"def"} };
// 从前往后输出m的元素
for (auto it = m1.begin(); it != m1.end(); it++)
cout << "(" << it->first << "," << it->second << ") ";
cout << endl;
// 从后往前输出m的元素
for (auto it = m1.rbegin(); it != m1.rend(); it++)
cout << "(" << it->first << "," << it->second << ") ";
return 0;
}

常用运算符
set,multiset,map,multimap:
均支持常见的 =,==,!=,<,> 等运算符
| 运算符 | 含义 |
| s2 = s1 | 把 s1 的数据赋值给 s2 |
| s1 == s2 | 判断是否相等 |
| s1 != s2 | 判断是否不相等 |
| <, <=, >, >= | 判断大小关系,从头到尾依次比较 |
void Show(const set<int>& s)
{
for (auto i : s)
cout << i << " ";
cout << endl;
}
int main()
{
set <int> s1{ 4 ,1 ,5 ,2 };
cout << "s1:"; Show(s1);
set<int> s2;
s2 = s1;
cout << "s2:"; Show(s2);
if (s1 == s2)
cout << "s1 == s2" << endl;
s2.insert(7);
cout << "往s2插入7后:";
if (s1 > s2)
cout << "s1 > s2";
else if (s1 < s2)
cout << "s1 < s2";
return 0;
}

set,multiset,multimap不支持[ ] ;
map支持使用 [ ]:
| 运算符 | 含义 |
| [ ] | 通过[ ]可直接访问和添加元素,[ ] 中存放的是key值,不是下标 |
int main() {
map<int, string> m;
m.insert({ 4,"xyz" });
cout << "m:"; Show(m);
m[3] = "lpl";//插入{3,"lpl"},不是在3下标存放"lpl"
m[4] = "abc";//将{ 4,"xyz" }改为{ 4,"abc" }
cout << "m:"; Show(m);
return 0;
}

成员函数
| 成员函数 | 含义 | ||
| set,multiset | map,multimap | set,multiset | map,multimap |
| clear() | 清空数据 | ||
| count(val) | count(key) | 统计和val相同的元素个数 | 统计和关键词key相同的元素个数 |
| empty() | 判断集合是否为空 | 判断映射是否为空 | |
| lower_bound(val) | 返回第一个元素>=val的位置 | ||
| upper_bound(val) | 返回第一个元素>val的位置 | ||
| equal_range(val) | lower_bound和upper_bound返回值组成的pair数对 | ||
| erase() | 删除元素或一个区间 | ||
| find(val) | 查找和val相同的第一个值 | 查找关键字和val相同的第一个值 | |
| insert() | 插入数据 | ||
| size() | 集合元素个数 | 映射元素个数 | |
| 无 | swap() | 无 | 两个映射交换数据 |
set,multiset
clear成员函数
清楚集合中所有元素
int main() {
set<int> s1{ 1,4,2,3,2 };
multiset<int> s2{ 1,4,2,3,2 };
cout << "s1元素个数:" << s1.size() << endl;
cout << "s2元素个数:" << s2.size() << endl;
s1.clear();
s2.clear();
cout << "调用clear后" << endl;
cout << "s1元素个数:" << s1.size() << endl;
cout << "s2元素个数:" << s2.size() << endl;
return 0;
}

count成员函数
统计val在集合中出现的次数
int main() {
set<int> s1{ 1,4,2,3,2 };
multiset<int> s2{ 1,4,2,3,2 };
cout << "s1:"; Show(s1);
cout << "s2:"; Show(s2);
cout << "s1中2的个数:" << s1.count(2) << endl;
cout << "s2中2的个数:" << s2.count(2) << endl;
return 0;
}

empty成员函数
判断集合是否为空
int main() {
set<int> s1;
if (s1.empty())
cout << "s1是空的";
else
cout << "s1不是空的";
return 0;
}
![]()
lower_bound成员函数
返回第一个元素>=val的位置
upper_bound成员函数
返回第一个元素>val的位置
equal_range成员函数
返回由lower_bound和upper_bound返回值组成的pair数对。主要用于查找多个相同的元素。
int main()
{
set<int> s1{ 1, 2, 3, 4, 5 };
auto it1 = s1.lower_bound(2);
if (it1 != s1.end()) // 找到>=2的迭代器
cout << "在s1中找到>=2的元素" << *it1 << endl;
set<int>::iterator it2 = s1.lower_bound(6);
if (it2 == s1.end()) // 没有找到>=6的元素
cout << "在s1中没有找到>=6的元素" << endl;
it1 = s1.upper_bound(2); // 找>2的迭代器
if (it1 != s1.end()) // 找到>2的迭代器
cout << "在s1中找到>2的元素" << *it1 << endl;
multiset<int> s2{ 1, 1, 2, 3, 3, 4, 5, 5 };
auto it3 = s2.upper_bound(4); // 找>4的迭代器
if (it3 != s2.end()) // 找到>4的迭代器
cout << "在s2中找到>4的元素" << *it3 << endl;
auto p = s2.equal_range(3); // 查找3的区间
if ((p.first != s2.end()) && (p.second != s2.end()))
{
cout << "在s2中找3的区间是(第一个>=3 ~ 第一个>3):" << *p.first << "~" << *p.second;
}
return 0;
}

erase成员函数
删除一个值或区间
int main() {
set<int> s1{ 1, 2, 3, 4, 5 };
cout << "s1:"; Show(s1);
s1.erase(1);
cout << "删除1后,s1:"; Show(s1);
s1.erase(++s1.begin(), s1.end()); // 删除除第一个元素外的所有元素
cout << "只保留第一个元素,s1:"; Show(s1);
multiset<int> s2{ 1, 1, 2, 3, 3, 4, 5, 5 };
cout << "s2:"; Show(s2);
s2.erase(1);
cout << "删除1后,s2:"; Show(s2);
auto it = s2.find(3); // 找到第一个3
s2.erase(it);
cout << "删除一个3后,s2:"; Show(s2);
return 0;
}

find成员函数
在集合中查找需要的关键字val,成功返回元素的迭代器,失败返回尾后迭代器
int main() {
set<int> s{4, 3, 1, 9, 0, 5, 7};
int key;
cin >> key;
if (s.find(key) != s.end()) // 成功返回元素的迭代器,失败返回尾后迭代器
cout << key << "在集合中" << endl;
else
cout << key << "不在集合中" << endl;
return 0;
}
insert成员函数
往集合中插入数据
int main()
{
set<int> s1{ 10, 20, 30, 40 }; // 创建一个集合对象
vector<int> v{ 21, 22, 23 }; // 创建一个向量对象
s1.insert(5); // 插入一个元素
s1.insert(s1.begin(), 7); // 在begin(迭代器)后面插入
s1.insert({ 11, 12, 13, 14, 15 }); // 插入多个元素(一个列表)
s1.insert(v.begin(), v.end()); // 插入一个迭代器区间
cout << "s1:"; Show(s1);
return 0;
}
![]()
size成员函数
返回集合对象元素个数
map,multimap
clear成员函数
清空对象的所有数据
count成员函数
统计和关键字key相同的元素个数
int main() {
map<int, string> m1{ {1, "abc"}, {2, "xyz"}, {3, "lpl"} };
multimap<int, string> m2{ {1, "abc"}, {2, "xyz"}, {2, "lpl"} };
cout << "m1: "; Show(m1);
cout << "m2: "; Show(m2);
cout << "m1中key为2的元素个数:" << m1.count(2) << endl;
// cout << m1.count("贺宏宏") << endl; // 错误,不能统计value
cout << "m2中key为2的元素个数:" << m2.count(2) << endl;
m1.clear(); // 清空所有数据
cout << "清空后,m1中key为2的元素个数:" << m1.count(2) << endl;
return 0;
}

empty成员函数
判断映射是否为空
lower_bound成员函数
返回第一个元素值 >= val 的位置(迭代器)。
upper_bound成员函数
返回第一个元素值 > val 的位置(迭代器)。
equal_range成员函数
返回由 lower_bound 和 upper_bound 返回值组成的 pair 数对。
erase成员函数
删除元素或一个区间
int main() {
multimap<int, string> m{ {4, "ear"}, {3, "xyz"}, {2, "lpl"} ,{ 2, "key" } };
Show(m);
m.erase(2); // 通过关键字删除
cout << "删除key=2的元素后" << endl;
Show(m);
m.erase(m.begin()); // 通过迭代器删除
cout << "删除第一个元素后" << endl;
Show(m);
m.erase(m.begin(), m.end()); // 通过迭代器区间删除
cout << "删除所有元素后" << endl;
Show(m);
return 0;
}

find成员函数
查找和key相同的元素。返回找到的第一个元素迭代器,没有找到返回尾后迭代器。
int main() {
multimap<int, string> m{ {4, "ear"}, {3, "xyz"}, {2, "lpl"} ,{ 2, "key" } };
Show(m);
auto it = m.find(2); // 查找成功
if (it != m.end())
cout << "查找2成功,(" << it->first << "," << it->second << ") " << endl;
it = m.find(6);
if (it == m.end())
cout << "没有找到关键字6" << endl;
return 0;
}

insert成员函数
插入元素
size成员函数
返回元素个数
swap成员函数
交换两个映射的元素
更多推荐



所有评论(0)