c++ sets

Sets是存储以下特定顺序独特元素的容器。

在一组,一个元素的值也将其标识(该值是本身的密钥类型的T,),并且每个值必须是唯一的。在一组中的元素的值,不能在容器一旦被改性(元素总是常数),但它们可以被插入或从容器中取出。
在内部,在一组中的元素通常排序以下通过其内部的比较对象表示(类型的比较)特定的严格弱排序标准。
组容器通常比unordered_set集装箱其密钥来访问单个元素慢,但他们允许基于他们的订单子集上的直接迭代。
套通常被实现为二进制搜索树。
ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/974d0224-4e5d-4a6a-8efc-5fc3254563bf.htm
http://www.cplusplus.com/reference/set/set/

begin	返回的地址集的第一个元素的迭代
clear	擦除的一组中的所有元素
count	返回一组,其关键的参数指定的关键字相匹配的元素数
empty	/返回set是否为空
end	返回解决了一套成功的最后一个元素的位置的迭代器
equal_range返回一对迭代的分别与第一元件中的一组与键比指定的键,并在该组用钥匙比该关键等于或大于第一元件更大

erase	删除元素或一个范围从指定的位置中的一组的元素或删除匹配指定键的元素
find	返回迭代寻址一组具有键相当于指定的键的元素的位置
get_allocator返回分配器对象的副本,用于构造该组
insert	插入一个元素或一定范围内的元素为一组
key_comp检索用于在一组命令键的比较对象的副本
lower_bound返回一个迭代的第一元件中的一组与键比指定的键等于或大于
max_size返回该组的最大长度
rbegin	返回一个迭代解决的第一个元素的集合逆转
rend	返回在颠倒一套解决成功的最后一个元素的位置的迭代器
size	返回集合中的元素的数量
swap	交换两个set元素
upper_bound返回一个迭代器的第一个元素一组用钥匙比指定的键更大
value_comp检索使用的比较对象的副本一组命令元素值



#include <set>
#include <iostream>
#include <xfunctional>


//构造函数
void setConstructor(void);

//返回的地址集的第一个元素的迭代
void set_begin(void);

//擦除的一组中的所有元素
void set_clear(void);

//返回一组,其关键的参数指定的关键字相匹配的元素数
void set_count(void);

//返回set是否为空
void set_empty(void);

//返回解决了一套成功的最后一个元素的位置的迭代器
void set_end(void);

//返回一对迭代的分别与第一元件中的一组与键比指定的键,并在该组用钥匙比该关键等于或大于第一元件更大
void set_equal_range(void);

//删除元素或一个范围从指定的位置中的一组的元素或删除匹配指定键的元素
void set_erase(void);

//返回迭代寻址一组具有键相当于指定的键的元素的位置
void set_find(void);

//返回分配器对象的副本,用于构造该组
void set_get_allocator(void);

//插入一个元素或一定范围内的元素为一组
void set_insert(void);

//检索用于在一组命令键的比较对象的副本
void set_key_comp(void);

//返回一个迭代的第一元件中的一组与键比指定的键等于或大于
void set_lower_bound(void);

//返回该组的最大长度
void set_max_size(void);

//返回一个迭代解决的第一个元素的集合逆转
void set_rbegin(void);

//返回在颠倒一套解决成功的最后一个元素的位置的迭代器
void set_rend(void);

//返回集合中的元素的数量
void set_size(void);

//交换两个set元素
void set_swap(void);

//返回一个迭代器的第一个元素一组用钥匙比指定的键更大
void set_upper_bound(void);

//检索使用的比较对象的副本一组命令元素值
void set_value_comp(void);


int main()
{
	//setConstructor();
	//set_begin();
	//set_clear();
	//set_count();
	//set_empty();
	//set_end();
	//set_equal_range();
	//set_erase();
	//set_find();
	//set_get_allocator();
	//set_insert();
	//set_key_comp();
	//set_lower_bound();
	//set_max_size();
	//set_rbegin();
	//set_rend();
	//set_size();
	//set_swap();
	//set_upper_bound();
	set_value_comp();
	return 0;
}

//构造函数
void setConstructor(void)
{
	using namespace std;
	set <int>::iterator s1_Iter, s2_Iter, s3_Iter, s4_Iter, s5_Iter, s6_Iter;

	// Create an empty set s0 of key type integer
	set <int> s0;

	// Create an empty set s1 with the key comparison
	// function of less than, then insert 4 elements
	set <int, less<int> > s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	// Create an empty set s2 with the key comparison
	// function of geater than, then insert 2 elements
	set <int, greater<int> > s2;
	s2.insert(10);
	s2.insert(20);

	// Create a set s3 with the 
	// allocator of set s1
	set <int>::allocator_type s1_Alloc;
	s1_Alloc = s1.get_allocator();
	set <int> s3(less<int>(), s1_Alloc);
	s3.insert(30);

	// Create a copy, set s4, of set s1
	set <int> s4(s1);

	// Create a set s5 by copying the range s1[_First, _Last)
	set <int>::const_iterator s1_bcIter, s1_ecIter;
	s1_bcIter = s1.begin();
	s1_ecIter = s1.begin();
	s1_ecIter++;
	s1_ecIter++;
	set <int> s5(s1_bcIter, s1_ecIter);

	// Create a set s6 by copying the range s4[_First, _Last)
	// and with the allocator of set s2
	set <int>::allocator_type s2_Alloc;
	s2_Alloc = s2.get_allocator();
	set <int> s6(s4.begin(), ++s4.begin(), less<int>(), s2_Alloc);

	cout << "s1 =";
	for (s1_Iter = s1.begin(); s1_Iter != s1.end(); s1_Iter++)
		cout << " " << *s1_Iter;
	cout << endl;

	cout << "s2 = " << *s2.begin() << " " << *++s2.begin() << endl;

	cout << "s3 =";
	for (s3_Iter = s3.begin(); s3_Iter != s3.end(); s3_Iter++)
		cout << " " << *s3_Iter;
	cout << endl;

	cout << "s4 =";
	for (s4_Iter = s4.begin(); s4_Iter != s4.end(); s4_Iter++)
		cout << " " << *s4_Iter;
	cout << endl;

	cout << "s5 =";
	for (s5_Iter = s5.begin(); s5_Iter != s5.end(); s5_Iter++)
		cout << " " << *s5_Iter;
	cout << endl;

	cout << "s6 =";
	for (s6_Iter = s6.begin(); s6_Iter != s6.end(); s6_Iter++)
		cout << " " << *s6_Iter;
	cout << endl;

	return;
	/*
	s1 = 10 20 30 40
	s2 = 20 10
	s3 = 30
	s4 = 10 20 30 40
	s5 = 10 20
	s6 = 10
	请按任意键继续. . .
	*/
}

//返回的地址集的第一个元素的迭代
void set_begin(void)
{
	using namespace std;
	set <int> s1;
	set <int>::iterator s1_Iter;
	set <int>::const_iterator s1_cIter;

	s1.insert(1);
	s1.insert(2);
	s1.insert(3);

	s1_Iter = s1.begin();
	cout << "The first element of s1 is " << *s1_Iter << endl;

	s1_Iter = s1.begin();
	s1.erase(s1_Iter);

	// The following 2 lines would err because the iterator is const
	// s1_cIter = s1.begin( );
	// s1.erase( s1_cIter );

	s1_cIter = s1.begin();
	cout << "The first element of s1 is now " << *s1_cIter << endl;

	return;
	/*
	The first element of s1 is 1
	The first element of s1 is now 2
	请按任意键继续. . .
	*/
}

//擦除的一组中的所有元素
void set_clear(void)
{
	using namespace std;
	set <int> s1;

	s1.insert(1);
	s1.insert(2);

	cout << "The size of the set is initially " << s1.size()
		<< "." << endl;

	s1.clear();
	cout << "The size of the set after clearing is "
		<< s1.size() << "." << endl;

	return;
	/*
	The size of the set is initially 2.
	The size of the set after clearing is 0.
	请按任意键继续. . .
	*/
}

//返回一组,其关键的参数指定的关键字相匹配的元素数
void set_count(void)
{
	using namespace std;
	set<int> s1;
	set<int>::size_type i;

	s1.insert(1);
	s1.insert(1);

	// Keys must be unique in set, so duplicates are ignored
	i = s1.count(1);
	cout << "The number of elements in s1 with a sort key of 1 is: "
		<< i << "." << endl;

	i = s1.count(2);
	cout << "The number of elements in s1 with a sort key of 2 is: "
		<< i << "." << endl;

	return;
	/*
	The number of elements in s1 with a sort key of 1 is: 1.
	The number of elements in s1 with a sort key of 2 is: 0.
	请按任意键继续. . .
	*/
}

//返回set是否为空
void set_empty(void)
{
	using namespace std;
	set <int> s1, s2;
	s1.insert(1);

	if (s1.empty())
		cout << "The set s1 is empty." << endl;
	else
		cout << "The set s1 is not empty." << endl;

	if (s2.empty())
		cout << "The set s2 is empty." << endl;
	else
		cout << "The set s2 is not empty." << endl;

	return;
	/*
	The set s1 is not empty.
	The set s2 is empty.
	请按任意键继续. . .
	*/
}

//返回解决了一套成功的最后一个元素的位置的迭代器
void set_end(void)
{
	using namespace std;
	set <int> s1;
	set <int> ::iterator s1_Iter;
	set <int> ::const_iterator s1_cIter;

	s1.insert(1);
	s1.insert(2);
	s1.insert(3);

	s1_Iter = s1.end();
	s1_Iter--;
	cout << "The last element of s1 is " << *s1_Iter << endl;

	s1.erase(s1_Iter);

	// The following 3 lines would err because the iterator is const
	// s1_cIter = s1.end( );
	// s1_cIter--;
	// s1.erase( s1_cIter );

	s1_cIter = s1.end();
	s1_cIter--;
	cout << "The last element of s1 is now " << *s1_cIter << endl;

	return;
	/*
	The last element of s1 is 3
	The last element of s1 is now 2
	请按任意键继续. . .
	*/
}

//返回一对迭代的分别与第一元件中的一组与键比指定的键,并在该组用钥匙比该关键等于或大于第一元件更大
void set_equal_range(void)
{
	using namespace std;
	typedef set<int, less< int > > IntSet;
	IntSet s1;
	set <int, less< int > > ::const_iterator s1_RcIter;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);

	pair <IntSet::const_iterator, IntSet::const_iterator> p1, p2;
	p1 = s1.equal_range(20);

	cout << "The upper bound of the element with "
		<< "a key of 20 in the set s1 is: "
		<< *(p1.second) << "." << endl;

	cout << "The lower bound of the element with "
		<< "a key of 20 in the set s1 is: "
		<< *(p1.first) << "." << endl;

	// Compare the upper_bound called directly 
	s1_RcIter = s1.upper_bound(20);
	cout << "A direct call of upper_bound( 20 ) gives "
		<< *s1_RcIter << "," << endl
		<< "matching the 2nd element of the pair"
		<< " returned by equal_range( 20 )." << endl;

	p2 = s1.equal_range(40);

	// If no match is found for the key,
	// both elements of the pair return end( )
	if ((p2.first == s1.end()) && (p2.second == s1.end()))
		cout << "The set s1 doesn't have an element "
		<< "with a key less than 40." << endl;
	else
		cout << "The element of set s1 with a key >= 40 is: "
		<< *(p1.first) << "." << endl;

	return;
	/*
	The upper bound of the element with a key of 20 in the set s1 is: 30.
	The lower bound of the element with a key of 20 in the set s1 is: 20.
	A direct call of upper_bound( 20 ) gives 30,
	matching the 2nd element of the pair returned by equal_range( 20 ).
	The set s1 doesn't have an element with a key less than 40.
	请按任意键继续. . .
	*/
}

//删除元素或一个范围从指定的位置中的一组的元素或删除匹配指定键的元素
void set_erase(void)
{
	using namespace std;
	set<int> s1, s2, s3;
	set<int>::iterator pIter, Iter1, Iter2;
	int i;
	set<int>::size_type n;

	for (i = 1; i < 5; i++)
	{
		s1.insert(i);
		s2.insert(i * i);
		s3.insert(i - 1);
	}

	// The 1st member function removes an element at a given position
	Iter1 = ++s1.begin();
	s1.erase(Iter1);

	cout << "After the 2nd element is deleted, the set s1 is:";
	for (pIter = s1.begin(); pIter != s1.end(); pIter++)
		cout << " " << *pIter;
	cout << "." << endl;

	// The 2nd member function removes elements
	// in the range [_First, _Last)
	Iter1 = ++s2.begin();
	Iter2 = --s2.end();
	s2.erase(Iter1, Iter2);

	cout << "After the middle two elements are deleted, "
		<< "the set s2 is:";
	for (pIter = s2.begin(); pIter != s2.end(); pIter++)
		cout << " " << *pIter;
	cout << "." << endl;

	// The 3rd member function removes elements with a given _Key
	n = s3.erase(2);

	cout << "After the element with a key of 2 is deleted, "
		<< "the set s3 is:";
	for (pIter = s3.begin(); pIter != s3.end(); pIter++)
		cout << " " << *pIter;
	cout << "." << endl;

	// The 3rd member function returns the number of elements removed
	cout << "The number of elements removed from s3 is: "
		<< n << "." << endl;

	// The dereferenced iterator can also be used to specify a key
	Iter1 = ++s3.begin();
	s3.erase(Iter1);

	cout << "After another element (unique for set) with a key"
		<< endl;
	cout << "equal to that of the 2nd element is deleted, "
		<< "the set s3 is:";
	for (pIter = s3.begin(); pIter != s3.end(); pIter++)
		cout << " " << *pIter;
	cout << "." << endl;

	return;
	/*
	After the 2nd element is deleted, the set s1 is: 1 3 4.
	After the middle two elements are deleted, the set s2 is: 1 16.
	After the element with a key of 2 is deleted, the set s3 is: 0 1 3.
	The number of elements removed from s3 is: 1.
	After another element (unique for set) with a key
	equal to that of the 2nd element is deleted, the set s3 is: 0 3.
	请按任意键继续. . .
	*/
}

//返回迭代寻址一组具有键相当于指定的键的元素的位置
void set_find(void)
{
	using namespace std;
	set <int> s1;
	set <int> ::const_iterator s1_AcIter, s1_RcIter;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);

	s1_RcIter = s1.find(20);
	cout << "The element of set s1 with a key of 20 is: "
		<< *s1_RcIter << "." << endl;

	s1_RcIter = s1.find(40);

	// If no match is found for the key, end( ) is returned
	if (s1_RcIter == s1.end())
		cout << "The set s1 doesn't have an element "
		<< "with a key of 40." << endl;
	else
		cout << "The element of set s1 with a key of 40 is: "
		<< *s1_RcIter << "." << endl;

	// The element at a specific location in the set can be found 
	// by using a dereferenced iterator addressing the location
	s1_AcIter = s1.end();
	s1_AcIter--;
	s1_RcIter = s1.find(*s1_AcIter);
	cout << "The element of s1 with a key matching "
		<< "that of the last element is: "
		<< *s1_RcIter << "." << endl;

	return;
	/*
	The element of set s1 with a key of 20 is: 20.
	The set s1 doesn't have an element with a key of 40.
	The element of s1 with a key matching that of the last element is: 30.
	请按任意键继续. . .
	*/
}

//返回分配器对象的副本,用于构造该组
void set_get_allocator(void)
{
	using namespace std;
	set <int>::allocator_type s1_Alloc;
	set <int>::allocator_type s2_Alloc;
	set <double>::allocator_type s3_Alloc;
	set <int>::allocator_type s4_Alloc;

	/*
	// The following lines declare objects
	// that use the default allocator.
	set <int> s1;
	set <int, allocator<int> > s2;
	set <double, allocator<double> > s3;
	
	s1_Alloc = s1.get_allocator();
	s2_Alloc = s2.get_allocator();
	s3_Alloc = s3.get_allocator();
	
	cout << "The number of integers that can be allocated"
		<< endl << "before free memory is exhausted: "
		<< s2.max_size() << "." << endl;

	cout << "\nThe number of doubles that can be allocated"
		<< endl << "before free memory is exhausted: "
		<< s3.max_size() << "." << endl;

	// The following line creates a set s4
	// with the allocator of multiset s1.
	set <int> s4(less<int>(), s1_Alloc);

	s4_Alloc = s4.get_allocator();

	// Two allocators are interchangeable if
	// storage allocated from each can be
	// deallocated by the other
	if (s1_Alloc == s4_Alloc)
	{
		cout << "\nThe allocators are interchangeable."
			<< endl;
	}
	else
	{
		cout << "\nThe allocators are not interchangeable."
			<< endl;
	}
	*/

	return;
	/*
	编译报错。MSDN
	The number of integers that can be allocated
	before free memory is exhausted: 1073741823.

	The number of doubles that can be allocated
	before free memory is exhausted: 536870911.

	The allocators are interchangeable.

	*/
}

//插入一个元素或一定范围内的元素为一组
void set_insert(void)
{
	using namespace std;
	set <int>::iterator s1_pIter, s2_pIter;

	set <int, less<int> > s1, s2;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	cout << "The original s1 =";
	for (s1_pIter = s1.begin(); s1_pIter != s1.end(); s1_pIter++)
		cout << " " << *s1_pIter;
	cout << "." << endl;

	pair< set<int>::iterator, bool > pr;
	pr = s1.insert(10);

	if (pr.second == true)
	{
		cout << "The element 10 was inserted in s1 successfully."
			<< endl;
	}
	else
	{
		cout << "The element 10 already exists in s1 and"
			<< " *( pr.first ) = " << *(pr.first) << "." << endl;
	}

	s1.insert(--s1.end(), 50);

	cout << "After the insertions, s1 =";
	for (s1_pIter = s1.begin(); s1_pIter != s1.end(); s1_pIter++)
		cout << " " << *s1_pIter;
	cout << "." << endl;

	s2.insert(100);
	s2.insert(++s1.begin(), --s1.end());

	cout << "s2 =";
	for (s2_pIter = s2.begin(); s2_pIter != s2.end(); s2_pIter++)
		cout << " " << *s2_pIter;
	cout << "." << endl;

	return;
	/*
	The original s1 = 10 20 30 40.
	The element 10 already exists in s1 and *( pr.first ) = 10.
	After the insertions, s1 = 10 20 30 40 50.
	s2 = 20 30 40 100.
	请按任意键继续. . .
	*/
}

//检索用于在一组命令键的比较对象的副本
void set_key_comp(void)
{
	using namespace std;

	set <int, less<int> > s1;
	set<int, less<int> >::key_compare kc1 = s1.key_comp();
	bool result1 = kc1(2, 3);
	if (result1 == true)
	{
		cout << "kc1( 2,3 ) returns value of true, "
			<< "where kc1 is the function object of s1."
			<< endl;
	}
	else
	{
		cout << "kc1( 2,3 ) returns value of false "
			<< "where kc1 is the function object of s1."
			<< endl;
	}

	set <int, greater<int> > s2;
	set<int, greater<int> >::key_compare kc2 = s2.key_comp();
	bool result2 = kc2(2, 3);
	if (result2 == true)
	{
		cout << "kc2( 2,3 ) returns value of true, "
			<< "where kc2 is the function object of s2."
			<< endl;
	}
	else
	{
		cout << "kc2( 2,3 ) returns value of false, "
			<< "where kc2 is the function object of s2."
			<< endl;
	}

	return;
	/*
	kc1( 2,3 ) returns value of true, where kc1 is the function object of s1.
	kc2( 2,3 ) returns value of false, where kc2 is the function object of s2.
	请按任意键继续. . .
	*/
}

//返回一个迭代的第一元件中的一组与键比指定的键等于或大于
void set_lower_bound(void)
{
	using namespace std;
	set <int> s1;
	set <int> ::const_iterator s1_AcIter, s1_RcIter;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);

	s1_RcIter = s1.lower_bound(20);
	cout << "The element of set s1 with a key of 20 is: "
		<< *s1_RcIter << "." << endl;

	s1_RcIter = s1.lower_bound(40);

	// If no match is found for the key, end( ) is returned
	if (s1_RcIter == s1.end())
		cout << "The set s1 doesn't have an element "
		<< "with a key of 40." << endl;
	else
		cout << "The element of set s1 with a key of 40 is: "
		<< *s1_RcIter << "." << endl;

	// The element at a specific location in the set can be found 
	// by using a dereferenced iterator that addresses the location
	s1_AcIter = s1.end();
	s1_AcIter--;
	s1_RcIter = s1.lower_bound(*s1_AcIter);
	cout << "The element of s1 with a key matching "
		<< "that of the last element is: "
		<< *s1_RcIter << "." << endl;

	return;
	/*
	The element of set s1 with a key of 20 is: 20.
	The set s1 doesn't have an element with a key of 40.
	The element of s1 with a key matching that of the last element is: 30.
	请按任意键继续. . .
	*/
}

//返回该组的最大长度
void set_max_size(void)
{
	using namespace std;
	set <int> s1;
	set <int>::size_type i;

	i = s1.max_size();
	cout << "The maximum possible length "
		<< "of the set is " << i << "." << endl;

	return;
	/*
	The maximum possible length of the set is 214748364.
	请按任意键继续. . .
	*/
}

//返回一个迭代解决的第一个元素的集合逆转
void set_rbegin(void)
{
	using namespace std;
	set <int> s1;
	set <int>::iterator s1_Iter;
	set <int>::reverse_iterator s1_rIter;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);

	s1_rIter = s1.rbegin();
	cout << "The first element in the reversed set is "
		<< *s1_rIter << "." << endl;

	// begin can be used to start an iteration 
	// throught a set in a forward order
	cout << "The set is:";
	for (s1_Iter = s1.begin(); s1_Iter != s1.end(); s1_Iter++)
		cout << " " << *s1_Iter;
	cout << endl;

	// rbegin can be used to start an iteration 
	// throught a set in a reverse order
	cout << "The reversed set is:";
	for (s1_rIter = s1.rbegin(); s1_rIter != s1.rend(); s1_rIter++)
		cout << " " << *s1_rIter;
	cout << endl;

	// A set element can be erased by dereferencing to its key 
	s1_rIter = s1.rbegin();
	s1.erase(*s1_rIter);

	s1_rIter = s1.rbegin();
	cout << "After the erasure, the first element "
		<< "in the reversed set is " << *s1_rIter << "." << endl;

	return;
	/*
	The first element in the reversed set is 30.
	The set is: 10 20 30
	The reversed set is: 30 20 10
	After the erasure, the first element in the reversed set is 20.
	请按任意键继续. . .
	*/
}

//返回在颠倒一套解决成功的最后一个元素的位置的迭代器
void set_rend(void)
{
	using namespace std;
	set <int> s1;
	set <int>::iterator s1_Iter;
	set <int>::reverse_iterator s1_rIter;
	set <int>::const_reverse_iterator s1_crIter;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);

	s1_rIter = s1.rend();
	s1_rIter--;
	cout << "The last element in the reversed set is "
		<< *s1_rIter << "." << endl;

	// end can be used to terminate an iteration 
	// throught a set in a forward order
	cout << "The set is: ";
	for (s1_Iter = s1.begin(); s1_Iter != s1.end(); s1_Iter++)
		cout << *s1_Iter << " ";
	cout << "." << endl;

	// rend can be used to terminate an iteration 
	// throught a set in a reverse order
	cout << "The reversed set is: ";
	for (s1_rIter = s1.rbegin(); s1_rIter != s1.rend(); s1_rIter++)
		cout << *s1_rIter << " ";
	cout << "." << endl;

	s1_rIter = s1.rend();
	s1_rIter--;
	s1.erase(*s1_rIter);

	s1_rIter = s1.rend();
	--s1_rIter;
	cout << "After the erasure, the last element in the "
		<< "reversed set is " << *s1_rIter << "." << endl;


	return;
	/*
	The last element in the reversed set is 10.
	The set is: 10 20 30 .
	The reversed set is: 30 20 10 .
	After the erasure, the last element in the reversed set is 20.
	请按任意键继续. . .
	*/
}

//返回集合中的元素的数量
void set_size(void)
{
	using namespace std;
	set <int> s1;
	set <int> ::size_type i;

	s1.insert(1);
	i = s1.size();
	cout << "The set length is " << i << "." << endl;

	s1.insert(2);
	i = s1.size();
	cout << "The set length is now " << i << "." << endl;

	return;
	/*
	The set length is 1.
	The set length is now 2.
	请按任意键继续. . .
	*/
}

//交换两个set元素
void set_swap(void)
{
	using namespace std;
	set <int> s1, s2, s3;
	set <int>::iterator s1_Iter;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s2.insert(100);
	s2.insert(200);
	s3.insert(300);

	cout << "The original set s1 is:";
	for (s1_Iter = s1.begin(); s1_Iter != s1.end(); s1_Iter++)
		cout << " " << *s1_Iter;
	cout << "." << endl;

	// This is the member function version of swap
	s1.swap(s2);

	cout << "After swapping with s2, list s1 is:";
	for (s1_Iter = s1.begin(); s1_Iter != s1.end(); s1_Iter++)
		cout << " " << *s1_Iter;
	cout << "." << endl;

	// This is the specialized template version of swap
	swap(s1, s3);

	cout << "After swapping with s3, list s1 is:";
	for (s1_Iter = s1.begin(); s1_Iter != s1.end(); s1_Iter++)
		cout << " " << *s1_Iter;
	cout << "." << endl;

	return;
	/*
	The original set s1 is: 10 20 30.
	After swapping with s2, list s1 is: 100 200.
	After swapping with s3, list s1 is: 300.
	请按任意键继续. . .
	*/
}

//返回一个迭代器的第一个元素一组用钥匙比指定的键更大
void set_upper_bound(void)
{
	using namespace std;
	set <int> s1;
	set <int> ::const_iterator s1_AcIter, s1_RcIter;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);

	s1_RcIter = s1.upper_bound(20);
	cout << "The first element of set s1 with a key greater "
		<< "than 20 is: " << *s1_RcIter << "." << endl;

	s1_RcIter = s1.upper_bound(30);

	// If no match is found for the key, end( ) is returned
	if (s1_RcIter == s1.end())
		cout << "The set s1 doesn't have an element "
		<< "with a key greater than 30." << endl;
	else
		cout << "The element of set s1 with a key > 40 is: "
		<< *s1_RcIter << "." << endl;

	// The element at a specific location in the set can be found 
	// by using a dereferenced iterator addressing the location
	s1_AcIter = s1.begin();
	s1_RcIter = s1.upper_bound(*s1_AcIter);
	cout << "The first element of s1 with a key greater than"
		<< endl << "that of the initial element of s1 is: "
		<< *s1_RcIter << "." << endl;

	return;
	/*
	The first element of set s1 with a key greater than 20 is: 30.
	The set s1 doesn't have an element with a key greater than 30.
	The first element of s1 with a key greater than
	that of the initial element of s1 is: 20.
	请按任意键继续. . .
	*/
}

//检索使用的比较对象的副本一组命令元素值
void set_value_comp(void)
{
	using namespace std;

	set <int, less<int> > s1;
	set <int, less<int> >::value_compare vc1 = s1.value_comp();
	bool result1 = vc1(2, 3);
	if (result1 == true)
	{
		cout << "vc1( 2,3 ) returns value of true, "
			<< "where vc1 is the function object of s1."
			<< endl;
	}
	else
	{
		cout << "vc1( 2,3 ) returns value of false, "
			<< "where vc1 is the function object of s1."
			<< endl;
	}

	set <int, greater<int> > s2;
	set<int, greater<int> >::value_compare vc2 = s2.value_comp();
	bool result2 = vc2(2, 3);
	if (result2 == true)
	{
		cout << "vc2( 2,3 ) returns value of true, "
			<< "where vc2 is the function object of s2."
			<< endl;
	}
	else
	{
		cout << "vc2( 2,3 ) returns value of false, "
			<< "where vc2 is the function object of s2."
			<< endl;
	}

	return;
	/*
	vc1( 2,3 ) returns value of true, where vc1 is the function object of s1.
	vc2( 2,3 ) returns value of false, where vc2 is the function object of s2.
	请按任意键继续. . .
	*/
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值