qfedu-cpp-level/day10/stl_set_demo/d1.cpp

33 lines
670 B
C++
Raw Normal View History

2023-08-14 17:20:39 +08:00
#include <iostream>
#include <set>
using namespace std;
template <typename T>
void print(const set<T> &s)
{
for (typename set<T>::const_iterator it = s.begin(); it != s.end(); ++it)
cout << *it << ' ';
cout << endl;
}
template <typename T>
void print(const multiset<T> &s)
{
for (typename multiset<T>::const_iterator it = s.begin(); it != s.end(); ++it)
cout << *it << ' ';
cout << endl;
}
int main()
{
int m[] = {1, 2, 3, 2, 3, 4};
set<int> s(m, m + 6);
print(s); // set 会自动去重,输出 1 2 3 4
multiset<int> ms(m, m + 6);
print(ms); // multiset 不会自动去重,输出 1 2 2 3 3 4
return 0;
}