更新时间:2015-10-06 20:14:43浏览次数:1+次
其结构示意图如下:
1、multiset元素插入
#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <string>
using namespace std;
int main()
{
multiset<string> str;
str.insert("abc");
str.insert("123");
str.insert("111");
str.insert("aaa");
str.insert("123");
//中序遍历集合中所有的元素
for(multiset<string>::iterator iter = str.begin(); iter!=str.end(); iter++)
cout << *iter << " ";
cout << endl;
return 0;
}
运行结果:#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <string>
using namespace std;
int main()
{
multiset<string> str;
str.insert("abc");
str.insert("123");
str.insert("111");
str.insert("aaa");
str.insert("123");
//中序遍历集合中所有的元素
for(multiset<string>::iterator iter = str.begin(); iter!=str.end(); iter++)
cout << *iter << " ";
cout << endl;
cout << endl;
int n = str.erase("123");
cout << "删除元素的个数: " << n << endl;
cout << "输出删除后剩余元素" << endl;
for(multiset<string>::iterator iter = str.begin(); iter!=str.end(); iter++)
cout << *iter << " ";
cout << endl;
cout << endl;
str.clear();
cout << "输出删除后剩余元素" << endl;
for(multiset<string>::iterator iter = str.begin(); iter!=str.end(); iter++)
cout << *iter << " ";
cout << endl;
return 0;
}
运行结果:#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <string>
using namespace std;
int main()
{
multiset<string> str;
str.insert("abc");
str.insert("123");
str.insert("111");
str.insert("aaa");
str.insert("123");
//中序遍历集合中所有的元素
for(multiset<string>::iterator iter =str.begin(); iter!= str.end(); iter++)
cout << *iter << " ";
cout << endl;
cout << endl;
multiset<string>::iterator iter;
iter = str.find("123");
if(iter != str.end()) //找到
cout << *iter << endl;
else //没有找到
cout << "没有找到" << endl;
iter = str.find("bbb");
if(iter != str.end())
cout << *iter << endl;
else
cout << "没有找到" << endl;
return 0;
}
运行结果:#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <string>
using namespace std;
struct myComp
{
bool operator()(string a, string b)
{
return a > b;
}
};
int main()
{
multiset<string, myComp> str;
str.insert("abc");
str.insert("123");
str.insert("111");
str.insert("aaa");
str.insert("123");
//中序遍历集合中所有的元素
for(multiset<string, myComp>::iterator iter = str.begin(); iter!=str.end(); iter++)
cout << *iter <<"";
cout << endl;
return 0;
}
运行结果:#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <string>
using namespace std;
struct Info
{
string name;
float score;
bool operator < (Info a) const
{
return a.score < score;
}
};
int main()
{
multiset<Info> str;
//插入元素
Info info;
info.name = "Jack";
info.score = 60;
str.insert(info);
info.name = "Bomi";
info.score = 80;
str.insert(info);
info.name = "Peti";
info.score = 80;
str.insert(info);
info.name = "Kity";
info.score = 70;
str.insert(info);
for(multiset<Info>::iterator iter = str.begin(); iter !=str.end(); iter++)
cout << (*iter).name<< ""<< (*iter).score << endl;
return 0;
}
运行结果: