赞
踩
C+帮助文档:链接: Set的文档介绍
#include <iostream> #include <string> #include <iomanip> #include <map> template<typename Map> void print_map(Map& m) { std::cout << '{'; for(auto& p: m) std::cout << p.first << ':' << p.second << ' '; std::cout << "}\n"; } struct Point { double x, y; }; struct PointCmp { bool operator()(const Point& lhs, const Point& rhs) const { return lhs.x < rhs.x; // NB 。有意忽略 y } }; int main() { // (1) 默认构造函数 std::map<std::string, int> map1; map1["something"] = 69; map1["anything"] = 199; map1["that thing"] = 50; std::cout << "map1 = "; print_map(map1); // (2) 范围构造函数 std::map<std::string, int> iter(map1.find("anything"), map1.end()); std::cout << "\niter = "; print_map(iter); std::cout << "map1 = "; print_map(map1); // (3) 复制构造函数 std::map<std::string, int> copied(map1); std::cout << "\ncopied = "; print_map(copied); std::cout << "map1 = "; print_map(map1); // (4) 移动构造函数 std::map<std::string, int> moved(std::move(map1)); std::cout << "\nmoved = "; print_map(moved); std::cout << "map1 = "; print_map(map1); // (5) initializer_list 构造函数 const std::map<std::string, int> init { {"this", 100}, {"can", 100}, {"be", 100}, {"const", 100}, }; std::cout << "\ninit = "; print_map(init); // 定制关键类选项 1 : // 使用比较 struct std::map<Point, double, PointCmp> mag = { { {5, -12}, 13 }, { {3, 4}, 5 }, { {-8, -15}, 17 } }; for(auto p : mag) std::cout << "The magnitude of (" << p.first.x << ", " << p.first.y << ") is " << p.second << '\n'; // 定制关键类选项 2 : // 使用比较 lambda // 此 lambda 按照其模比较点,注意其中模取自局部变量 mag auto cmpLambda = [&mag](const Point &lhs, const Point &rhs) { return mag[lhs] < mag[rhs]; }; // 你亦可使用不依赖局部变量的 lambda ,像这样: // auto cmpLambda = [](const Point &lhs, const Point &rhs) { return lhs.y < rhs.y; }; std::map<Point, double, decltype(cmpLambda)> magy(cmpLambda); // 各种插入元素的方式: magy.insert(std::pair<Point, double>({5, -12}, 13)); magy.insert({ {3, 4}, 5}); magy.insert({Point{-8.0, -15.0}, 17}); std::cout << '\n'; for(auto p : magy) std::cout << "The magnitude of (" << p.first.x << ", " << p.first.y << ") is " << p.second << '\n'; }
输出:
map1 = {anything:199 something:69 that thing:50 } iter = {anything:199 something:69 that thing:50 } map1 = {anything:199 something:69 that thing:50 } copied = {anything:199 something:69 that thing:50 } map1 = {anything:199 something:69 that thing:50 } moved = {anything:199 something:69 that thing:50 } map1 = {} init = {be:100 can:100 const:100 this:100 } The magnitude of (-8, -15) is 17 The magnitude of (3, 4) is 5 The magnitude of (5, -12) is 13 The magnitude of (3, 4) is 5 The magnitude of (5, -12) is 13 The magnitude of (-8, -15) is 17
需要注意的是,set容器的特点是,插入元素会自动进行排序,且不允许重复的元素插入。
元素不支持修改 底层是一个const类型的迭代器;
有两种查找的方式,一种是set库里给的find函数,时间复杂度是logN,另一种是用暴力查找的算法 复杂度是O(N);
帮助文档:
lower_bound确定的范围是大于key的,相当于数学里的闭区间[ 。
例如:
// set::lower_bound/upper_bound #include <iostream> #include <set> int main () { std::set<int> myset; std::set<int>::iterator itlow,itup; for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90 itlow=myset.lower_bound (30); // ^ itup=myset.upper_bound (60); // ^ myset.erase(itlow,itup); // 10 20 70 80 90 std::cout << "myset contains:"; for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
运行结果:
myset contains: 10 20 70 80 90
确定范围比key大的,不包括key,相当于数学里的开区间(key
例如:
// set::lower_bound/upper_bound #include <iostream> #include <set> int main () { std::set<int> myset; std::set<int>::iterator itlow,itup; for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90 itlow=myset.lower_bound (30); // ^ itup=myset.upper_bound (60); // ^ myset.erase(itlow,itup); // 10 20 70 80 90 std::cout << "myset contains:"; for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
运行结果:
myset contains: 10 20 70 80 90
删除有两种方式,一种是查找删除,一种是直接删除。
查找删除:
查找删除,如果查找的数不存在set中就会报错!
直接删除:
直接删除,有就删,没有就不删,不会报错!
帮助文档:
创作不易,一起努力!转载请注明出处!谢谢支持!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。