set和multiset插入,删除,查找都是O(log n)复杂度的。 set不允许重复 multiset则允许。
#include "iostream"
#include "map"
#include "set"
using namespace std;
int main() {
multiset<int>st;
int a[10] = { 1,14,12,13,7,13,21,19,8,8 };
for (int i = 0; i < 10; i++) {
st.insert(a[i]);
}
multiset<int>::iterator i;
for (i = st.begin(); i != st.end(); i++) {
cout << *i <<" ";
}
cout << endl;
i = st.find(22);
if (i == st.end()) {
cout << "Not Found" << endl;
}
st.insert(22);
i = st.find(22);
if (i == st.end()) {
cout << "Not Found" << endl;
}
else {
cout << "Found" << endl;
}
i = st.lower_bound(13); // 返回最靠后的迭代器it [begin(),it)中的元素
cout << *i << endl;
i = st.upper_bound(8); //返回最靠前的迭代器it [it,end())中的元素都在8后面
cout << *i << endl;
st.erase(st.find(13));
for (i = st.begin(); i != st.end(); i++) {
cout << *i << " ";
}
cout << endl;
return 0;
}
/* 在vs2015中跑会出错 用在线编译器没问题 不知道为啥- -是支持的C++标准不一样么?*/
#include <iostream>
#include <functional>
#include <set>
using namespace std;
struct Rule1 {
bool operator()(const int & a, const int & b) {
return (a % 10) < (b % 10);
}//返回值为true则说明a必须排在b前面
};
int main() {
multiset<int, greater<int> > st; //排序规则为从大到小
int a[10] = { 1,14,12,13,7,13,21,19,8,8 };
for (int i = 0; i < 10; ++i)
st.insert(a[i]);
multiset<int, greater<int> >::iterator i;
for (i = st.begin(); i != st.end(); ++i)
cout << *i << ",";
cout << endl;
multiset<int, Rule1 > st2;
//st2的元素排序规则为:个位数小的排前面
for (int i = 0; i < 10; ++i)
st2.insert(a[i]);
multiset<int, Rule1>::iterator p;
for (p = st2.begin(); p != st2.end(); ++p)
cout << *p << ",";
cout << endl;
p = st2.find(133); /* 能找到 因为不能确定是133排在13前面还是13排在133前面 所以即找到 输出13*/
cout << *p << endl;
return 0;
}
/* 在vs2015中跑会出错 用在线编译器没问题 不知道为啥- -是支持的C++标准不一样么?*/
#include <iostream>
#include <functional>
#include <set>
using namespace std;
struct Student {
char name[20];
int id;
int score;
};
Student students[] = { { "Jack",112,78 },{ "Mary",102,85 },{ "Ala",333,92 },{ "Zero",101,70 },{ "Cindy",102,78 } };
struct Rule {
bool operator() (const Student& s1, const Student& s2) {
if (s1.score != s2.score)
return s1.score > s2.score;
else
return (strcmp(s1.name, s2.name) < 0);
}
};
int main() {
multiset<Student, Rule>st;
for (int i = 0; i < 5; i++) {
st.insert(students[i]);
}
multiset<Student, Rule>::iterator p;
for (p = st.begin(); p != st.end(); p++) {
cout << p->score << " " << p->name << " " << p->id << endl;
}
Student s = { "Mary",1000,85 };
p = st.find(s);
if (p != st.end()) {
cout << p->score << " " << p->name << " " << p->id << endl;
}
return 0;
}
/* 在vs2015中跑会出错 用在线编译器没问题 不知道为啥- -是支持的C++标准不一样么? */
#include <iostream>
#include <functional>
#include <set>
using namespace std;
int main() {
set<int>st;
int a[10] = { 1,2,3,8,7,7,5,6,8,12 };
for (int i = 0; i < 10; i++) {
st.insert(a[i]);
}
cout << st.size() << endl;
set<int>::iterator i;
for (i = st.begin(); i != st.end(); i++) {
cout << *i << " ";
}
cout << endl;
pair<set<int>::iterator, bool>result = st.insert(2);
if (!result.second) {
cout << *result.first << " " << "already exist" << endl;
}
else {
cout << *result.first << " " << "inserted" << endl;
}
return 0;
}