#include <iostream> #include <unordered_set> int main() { std::unordered_set<int> X { 2020, 2019, 2018 }; X.insert(19); X.insert(99); X.insert(89); X.insert(59); X.insert(79); for (auto Y: X) std::cout << Y << ' '; return 0; }
root@ubuntu:~/c++# ./set 79 59 89 99 2020 2019 19 2018
#include <iostream> #include <unordered_set> int main() { std::unordered_set<int> X { 2020, 2019, 2018 }; for (auto Y: X) std::cout << Y << ' '; return 0; }
root@ubuntu:~/c++# g++ -std=c++11 set.cpp -o set root@ubuntu:~/c++# ./set 2018 2019 2020
#include <iostream> #include <set> int main() { std::set<int> X { 2020, 2019, 2018 }; for (auto Y: X) std::cout << Y << ' '; return 0; }
有序输出
root@ubuntu:~/c++# g++ -std=c++11 set.cpp -o set root@ubuntu:~/c++# ./set 2018 2019 2020
#include <iostream> #include <set> int main() { std::set<int> X { 2020, 2019, 2018 }; X.insert(2019); for (auto Y: X) std::cout << Y << ' '; return 0; }
没有重复数据
root@ubuntu:~/c++# g++ -std=c++11 set.cpp -o set root@ubuntu:~/c++# ./set 2018 2019 2020
#include <iostream> #include <set> int main() { std::set<int> X { 2020, 2019, 2018 }; X.insert(19); X.insert(99); X.insert(89); X.insert(59); X.insert(79); for (auto Y: X) std::cout << Y << ' '; return 0; }
root@ubuntu:~/c++# g++ -std=c++11 set.cpp -o set root@ubuntu:~/c++# ./set 19 59 79 89 99 2018 2019 2020
~