STL——set容器

1.set简介

    set是C++标准库中的一种关联容器。所谓关联容器就是通过键(key)来读取和修改元素。与map关联容器不同,它只是单纯键的集合。在set中元素都是唯一的,而且默认情况下会对元素自动进行升序排列,支持集合的交(set_intersection),差(set_difference) 并(set_union),对称差(set_symmetric_difference) 等一些集合上的操作,如果需要集合中的元素允许重复那么可以使用multiset。

2.set实例化

example

#include<iostream>
#include<set>
using namespace std;
//set::begin() return iterator beginning
int main()
{       
        int a[] = { 5,4,3,2,1 };
	int b[] = { 78,8,2,10 };
	set<int> testa(a, a + 5);
	set<int> testb(b, b + 4);
	set<int>::iterator it;
	for (it = testa.begin(); it != testa.end(); it++)
	{
		cout << *it << "\t";
	}
	cout << endl;
	for (it = testb.begin(); it != testb.end(); it++)
	{
		cout << *it << "\t";
	}
	system("pause");
	return 0;
}

output

1       2       3       4       5

2       8       10      78      请按任意键继续. . .

3.成员函数
2.1  set::begin()&&set::end()  用法请看2.set实例化
2.2 set::cbegin()&&set::cend()  (c++11)

    example

#include<iostream>
#include<set>
using namespace std;
//set::cbegin()  return const_iterator to beginning 返回指向容器开始的const_iterator
//set::cend() return const_iterator to end
//set容器中的所有迭代器都是常量迭代器(包括const _iterator和iterator),它们不能用于修改它们指向的内容
int main()
{
	set<int> test = {3,2,1,6,7,0};
	cout << "test contains:";
	for (auto it = test.cbegin(); it != test.cend(); it++)
	{
		cout << "   " << *it ;
	}
        system("pause");
	return 0;
}

    output

    test contains:   0   1   2   3   6   7请按任意键继续. . .

2.3set::crbegin()&&set::crend() (c++11)

    example

#include<iostream>
#include<set>
using namespace std;
//set::crbegin() return const_reverse_iterator to reverse beginning 返回指向容器反向开始的const_reverse_iterator
//set::crend() return const_reverse_iterator to reverse end
int main()
{
	char a[] = { 'i','l','k','y','e' };
	set<char> test(a,a + 5);
	cout << "test contains:";
	for (auto it = test.crbegin(); it != test.crend(); it++)
	{
		cout << "   " << *it ;
	}

	system("pause");
	return 0;
}

    output

    test contains:   y   l   k   i   e请按任意键继续. . .

2.4set::rbegin()&&set::rend()

    example

#include<iostream>
#include<set>
using namespace std;
//set::rbegin() return reverse iterator to reverse beginning 返回指向容器反向开始的reverse_iterator
//set::rend() return reverse iterator to reverse end
int main()
{
	char a[] = { 'i','l','k','y','e' };
	set<char> test(a,a + 5);
	cout << "test contains:";
	for (auto it = test.rbegin(); it != test.rend(); it++)
	{
		cout << "   " << *it ;
	}
	system("pause");
	return 0;
}

    output

    test contains:   y   l   k   i   e请按任意键继续. . .

2.5set::insert()

    example

#include<iostream>
#include<set>
using namespace std;
//set::insert() 
int main()
{
	set<int> test;
	test.insert(5);
	test.insert(1);
	test.insert(3);
	test.insert(6);
	int a[] = { 9,8,6,7 };//元素6重复
	cout << "before insert array a test contains:";
	for (auto it = test.begin(); it != test.end(); it++)
	{
		cout << "   " << *it;
	}
	cout << "\n";
	test.insert(a, a + 4);
	cout << "after insert array a test contains:";
	for (auto it = test.begin(); it != test.end(); it++)
	{
		cout << "   " << *it;
	}
	system("pause");
	return 0;
}

    output

    before insert array a test contains:   1   3   5   6
    after insert array a test contains:   1   3   5   6   7   8   9请按任意键继续. .

2.6set::find()&&set::erase()

    example

#include<iostream>
#include<set>
using namespace std;
int main()
{
	set<int> test;
//	set<int>::iterator it;
	int a[] = { 1,2,3,4 };
	test.insert(a, a + 4);
	int b[] = { 40,30,20,10 };
	test.insert(b, b + 4);
	cout << "test contains:";
	for (auto it = test.begin(); it != test.end(); it++)
	{
		cout << "   " << *it;
	}
	cout << "\n";
	auto it = test.find(2);
	test.erase(it);
	test.erase(test.find(10));
	cout << "after erase test contains:";
	for (auto it = test.begin(); it != test.end(); it++)
	{
		cout << "   " << *it;
	}
	system("pause");
	return 0;
}

    output

    test contains:   1   2   3   4   10   20   30   40
    after erase test contains:   1   3   4   20   30   40请按任意键继续. . .

2.7set::clear()

    example

#include<iostream>
#include<set>
using namespace std;
//set::clear() clear all elements in set
int main()
{
	set<int> test;
	int a[] = { 1,2,3,4 };
	test.insert(a, a + 4);
	test.clear();
	int b[] = { 40,30,20,10 };
	test.insert(b, b + 4);
	cout << "test contains:";
	for (auto it = test.begin(); it != test.end(); it++)
	{
		cout << "   " << *it;
	}
	cout << "\n";
	system("pause");
	return 0;
}

    output

    test contains:   10   20   30   40

    请按任意键继续. . .

2.8set::count()

    example

#include<iostream>
#include<set>
using namespace std;
//set::count(type value) 
//1 if the container contains an element equivalent to val, or zero otherwise.
int main()
{
	set<int> test;
	int a[] = { 1,2,3,4 };
	test.insert(a, a + 4);
	cout << "test contains:";
	for (auto it = test.begin(); it != test.end(); it++)
	{
		cout << "   " << *it;
	}
	cout << "\n";
	for (auto i = 0; i <= 5; i++)
	{
		if (test.count(i))
			cout << i << "is an element in container" << endl;
		else
			cout << i << "is not in container" << endl;
	}
	system("pause");
	return 0;
}

    output

    test contains:   1   2   3   4
    0is not in container
    1is an element in container
    2is an element in container
    3is an element in container
    4is an element in container
    5is not in container

    请按任意键继续. . .

2.9set::empty()&&set::equal_range&&set::upper_bound&&set::lower_bound

    example

#include<iostream>
#include<set>
using namespace std;
int main()
{
	set<int> test;
	for (auto i = 0; i <= 10; i++)
		test.insert(i * 10);
	cout << "test contains:";
	for (auto it = test.begin(); it != test.end(); it++)
	{
		cout << "   " << *it;
	}
	auto low = test.lower_bound(20);
	auto high = test.upper_bound(60);
	test.erase(low, high);
	cout << "\nnow test contains:";
	for (auto it = test.begin(); it != test.end(); it++)
	{
		cout << "   " << *it;
	}
	auto ret = test.equal_range(70);
	cout << "\nthe lower bound points to:" << *ret.first;
	cout << "\nthe upper bound points to:" << *ret.second;
	system("pause");
	return 0;
}

    output

    test contains:   0   10   20   30   40   50   60   70   80   90   100
    now test contains:   0   10   70   80   90   100
    the lower bound points to:70

    the upper bound points to:80请按任意键继续. . .

2.10set::emplace()&&set::emplace_hint()(c++11)

    example

#include<iostream>
#include<set>
using namespace std;
//set::emplace(type value) return 0 if value already in container,else return 1
//set::emplace_hint()
//If the function successfully inserts the element (because no equivalent element existed already in the set),
//the function returns an iterator to the newly inserted element.
//Otherwise, it returns an iterator to the equivalent element within the container.
int main()
{
	set<string> test_s;
	test_s.emplace("hello");
	test_s.emplace("world");
	auto ret = test_s.emplace("hello");
	cout << ret.second<< endl;// 0
	if (!ret.second)
	{
		cout << "hello already inserted in container " << endl;
	}
	set<string> test_n;
	auto it = test_n.cbegin();
	test_n.emplace_hint(it,"abc");
	it = test_n.emplace_hint(test_n.cend(), "much");
	it = test_n.emplace_hint(it,"so");
	it = test_n.emplace_hint(it, "c ++");
	for (auto it = test_n.cbegin(); it != test_n.cend(); it++)
	{
		cout << "   " << *it;
	}
	system("pause");
	return 0;
}

    output

    0
    hello already inserted in container

    abc   c ++   much   so请按任意键继续. . .

2.11set::max_size()

    example

#include<iostream>
#include<set>
using namespace std;
int main()
{
	int i;
	set<int> test;
	cout << test.max_size();
	set<string> test_a;
	cout <<" " << test_a.max_size();
	if (test.max_size()>1000)
	{
		for (i = 0; i<1000; i++) test.insert(i);
		cout << "The set contains 1000 elements.\n";
	}
	else
		cout << "The set could not hold 1000 elements.\n";
	system("pause");
	return 0;
}

    output

    214748364 97612893The set contains 1000 elements.
请按任意键继续. . .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值