您的位置:首页 > 其它

STL学习之set与multiset操作练习

2017-12-06 10:23 246 查看
// STL_set_操作.cpp: 定义控制台应用程序的入口点。
//
/*	一个集合容器
包含元素唯一
不可以直接存取元素
采用红黑树变体 在插入和删除上比vector快	wangsl */
#include "stdafx.h"

using namespace std;

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include "set"
#include <algorithm>
#include<functional>

/* 元素集合,自动排序,不能按照[]方式插入元素 wangsl */
void testSet()
{
set<int> set1;
for (int i = 0; i<5 ; i++)
{
int tmp = rand();
set1.insert(tmp);
}
set1.insert(100); set1.insert(100); set1.insert(100);
for (set<int>::iterator it = set1.begin();it!=set1.end();it++)
{
cout << *it << " ";
}
/* 删除集合 wangsl */
while (!set1.empty())
4000

{
set<int>::iterator it = set1.begin();
cout << *it << " ";
set1.erase(set1.begin());
}
}

/* 对于复杂的数据类型 wangsl */
void set_compare()
{
set<int> set1;
set<int, less<int>> set2;	//按从小到大排序
set<int, greater<int>> set3;	//按从大到小排序 用greater需包含functional头文件
//相当于预定义好的仿函数
for (int i = 0; i < 5; i++)
{
int tmp = rand();
set3.insert(tmp);
}
for (set<int, greater<int>>::iterator it = set3.begin();it!=set3.end();it++)
{
cout << *it << " ";
}
}

class Student
{
public:
Student(char *name, int age)
{
strcpy_s(this->name , name);
this->age = age;
}
public:
char name[64];
int age;
};
/* 仿函数 wangsl */
struct stuFunctor
{
bool operator()(const Student &left, const Student &right)
{
if (left.age < right.age)	//如果左边小返回真 从小到大按照年龄排序
{
return true;			//可以使set自动按从小到大按年龄排序,可参见运行结果
}
else
return false;
}
};
void set_class()
{
set<Student , stuFunctor> set1;
Student s1("s1", 32);
Student s2("s2", 22);
Student s3("s3", 54);
Student s4("s4", 13);
Student s5("s5", 32);	//通过仿函数 set按照age唯一,所以有两个age一样的时候,插入不成功

set1.insert(s1);
set1.insert(s2);
set1.insert(s3);
set1.insert(s4);
set1.insert(s5);

for (set<Student,stuFunctor>::iterator it = set1.begin(); it!=set1.end(); it++)
{
cout << it->age << "\t" <<it->name << endl;
}

}

void set_erro_log()
{

Student s1("s1", 32);
Student s2("s2", 22);
Student s3("s3", 54);
Student s4("s4", 13);
Student s5("s5", 32);	//通过仿函数 set按照age唯一,所以有两个age一样的时候,插入不成功
set<Student, stuFunctor> set1;
/* pair为对组,可以将两个元素视为一个单元 wangsl */
pair<set<Student, stuFunctor>::iterator, bool> pair1 = set1.insert(s1);
if (pair1.second == true)
{
cout<< "插入s1成功!" << endl;
}
else
cout<< "插入s1失败!" << endl;
set1.insert(s2);
pair<set<Student, stuFunctor>::iterator, bool> pair2 = set1.insert(s5);
if (pair2.second == true)
{
cout << "插入s5成功!" << endl;
}
else
cout << "插入s5失败!" << endl;
//set1.insert(s5);

for (set<Student, stuFunctor>::iterator it = set1.begin(); it != set1.end(); it++)
{
cout << it->age << "\t" << it->name << endl;
}
}

/* 要会使用返回值为pair的参数 wangsl */
void set_find_pair()
{
set<int> set1;
for (int i = 0; i<10 ; i++)
{
set1.insert(i + 1);
}

for (set<int>::iterator it = set1.begin(); it!=set1.end();it++)
{
cout<< *it << endl;
}
cout << endl;

set<int>::iterator it1 = set1.find(5);
cout<< "it1: " <<  *it1 << endl;

int num1 = set1.count(5);
cout<< "num1:" << num1 << endl;

set<int>::iterator it2 = set1.lower_bound(5);	/* 小于5的元素迭代器位置 wangsl */
cout<< "it2: " << *it2 << endl;

set<int>::iterator it3 = set1.upper_bound(5);	/* 大于5的迭代器位置 wangsl */
cout << "it3: " << *it3 << endl;

// 	using _Pairib = pair<iterator, bool>;
// 	using _Pairii = pair<iterator, iterator>;
// 	using _Paircc = pair<const_iterator, const_iterator>;
set1.erase(5);
pair<set<int>::iterator,set<int>::iterator> mypair =   set1.equal_range(5);
set<int>::iterator it4 = mypair.first;	/* 大于等于5的位置 wangsl */
set<int>::iterator it5 = mypair.second;	/* 大于5的位置 wangsl */
cout << "it4: " << *it4 << endl; cout << "it5: " << *it5 << endl;
}

//////////////////////////////////////////////////////////////////////////
/* multiset操练 wangsl */
void test_multiset()
{
/* multiset与set的区别是:
set支持唯一键值,每个元素值只能出现一次
multiset中同一值可以出现多次	wangsl */
multiset<int> set1;
int tmp = 0;
//输入
printf("请输入multiset的值:");
scanf_s("%d", &tmp);
while (tmp != 0)
{
set1.insert(tmp);
printf("请输入multiset的值:");
scanf_s("%d", &tmp);
}
//遍历
cout<< "遍历multiset" << " ";
for (multiset<int>::iterator it = set1.begin(); it!=set1.end(); it++)
{
cout << *it << " ";
}
cout<< "删除multiset" << " ";
while (!set1.empty())
{
multiset<int>::iterator it = set1.begin();
cout<< *it << " ";
//删除
set1.erase(it);
}
}

int main()
{
testSet();
cout << endl;
set_compare();
cout << endl;
set_class();
cout << endl;
set_erro_log();
cout << endl;
set_find_pair();
cout << endl;
test_multiset();
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: