c++ Multisets
2016-04-30 21:20
417 查看
Multiset 多集是存储以下特定顺序的元素的容器,并在多个元素可以有等价的值。 在一个多集,一个元素的值也将其标识(该值是本身的密钥类型的T)。在一个多重的元素的值,不能在容器一旦被改性(元素总是常数),但它们可以被插入或从容器中取出。 在内部,在一个多重元素总是排列继其内部比较对象表示(类型的比较)具体严格弱排序标准。 多集容器通常比unordered_multiset集装箱其密钥来访问单个元素慢,但他们允许基于他们的订单子集上的直接迭代。 ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/4fa1acdb-03ad-4f99-b260-da1e6b46bd29.htm http://www.cplusplus.com/reference/set/multiset/?kw=multiset 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 multisetConstructor(void);
//返回的地址集的第一个元素的迭代
void multiset_begin(void);
//擦除的一组中的所有元素
void multiset_clear(void);
//返回一组,其关键的参数指定的关键字相匹配的元素数
void multiset_count(void);
//返回set是否为空
void multiset_empty(void);
//返回解决了一套成功的最后一个元素的位置的迭代器
void multiset_end(void);
//返回一对迭代的分别与第一元件中的一组与键比指定的键,并在该组用钥匙比该关键等于或大于第一元件更大
void multiset_equal_range(void);
//删除元素或一个范围从指定的位置中的一组的元素或删除匹配指定键的元素
void multiset_erase(void);
//返回迭代寻址一组具有键相当于指定的键的元素的位置
void multiset_find(void);
//返回分配器对象的副本,用于构造该组
void multiset_get_allocator(void);
//插入一个元素或一定范围内的元素为一组
void multiset_insert(void);
//检索用于在一组命令键的比较对象的副本
void multiset_key_comp(void);
//返回一个迭代的第一元件中的一组与键比指定的键等于或大于
void multiset_lower_bound(void);
//返回该组的最大长度
void multiset_max_size(void);
//返回一个迭代解决的第一个元素的集合逆转
void multiset_rbegin(void);
//返回在颠倒一套解决成功的最后一个元素的位置的迭代器
void multiset_rend(void);
//返回集合中的元素的数量
void multiset_size(void);
//交换两个set元素
void multiset_swap(void);
//返回一个迭代器的第一个元素一组用钥匙比指定的键更大
void multiset_upper_bound(void);
//检索使用的比较对象的副本一组命令元素值
void multiset_value_comp(void);
int main()
{
//multisetConstructor();
//multiset_begin();
//multiset_clear();
//multiset_count();
//multiset_empty();
//multiset_end();
//multiset_equal_range();
//multiset_erase();
//multiset_find();
//multiset_get_allocator();
//multiset_insert();
//multiset_key_comp();
//multiset_lower_bound();
//multiset_max_size();
//multiset_rbegin();
//multiset_rend();
//multiset_size();
//multiset_swap();
//multiset_upper_bound();
multiset_value_comp();
return 0;
}
//构造函数
void multisetConstructor(void)
{
using namespace std;
multiset <int>::iterator ms1_Iter, ms2_Iter, ms3_Iter;
multiset <int>::iterator ms4_Iter, ms5_Iter, ms6_Iter;
// Create an empty multiset ms0 of key type integer
multiset <int> ms0;
// Create an empty multiset ms1 with the key comparison
// function of less than, then insert 4 elements
multiset <int, less<int> > ms1;
ms1.insert(10);
ms1.insert(20);
ms1.insert(20);
ms1.insert(40);
// Create an empty multiset ms2 with the key comparison
// function of geater than, then insert 2 elements
multiset <int, greater<int> > ms2;
ms2.insert(10);
ms2.insert(20);
// Create a multiset ms3 with the
// allocator of multiset ms1
multiset <int>::allocator_type ms1_Alloc;
ms1_Alloc = ms1.get_allocator();
multiset <int> ms3(less<int>(), ms1_Alloc);
ms3.insert(30);
// Create a copy, multiset ms4, of multiset ms1
multiset <int> ms4(ms1);
// Create a multiset ms5 by copying the range ms1[_First, _Last)
multiset <int>::const_iterator ms1_bcIter, ms1_ecIter;
ms1_bcIter = ms1.begin();
ms1_ecIter = ms1.begin();
ms1_ecIter++;
ms1_ecIter++;
multiset <int> ms5(ms1_bcIter, ms1_ecIter);
// Create a multiset ms6 by copying the range ms4[_First, _Last)
// and with the allocator of multiset ms2
multiset <int>::allocator_type ms2_Alloc;
ms2_Alloc = ms2.get_allocator();
multiset <int> ms6(ms4.begin(), ++ms4.begin(), less<int>(), ms2_Alloc);
cout << "ms1 =";
for (ms1_Iter = ms1.begin(); ms1_Iter != ms1.end(); ms1_Iter++)
cout << " " << *ms1_Iter;
cout << endl;
cout << "ms2 = " << *ms2.begin() << " " << *++ms2.begin()
<< endl;
cout << "ms3 =";
for (ms3_Iter = ms3.begin(); ms3_Iter != ms3.end(); ms3_Iter++)
cout << " " << *ms3_Iter;
cout << endl;
cout << "ms4 =";
for (ms4_Iter = ms4.begin(); ms4_Iter != ms4.end(); ms4_Iter++)
cout << " " << *ms4_Iter;
cout << endl;
cout << "ms5 =";
for (ms5_Iter = ms5.begin(); ms5_Iter != ms5.end(); ms5_Iter++)
cout << " " << *ms5_Iter;
cout << endl;
cout << "ms6 =";
for (ms6_Iter = ms6.begin(); ms6_Iter != ms6.end(); ms6_Iter++)
cout << " " << *ms6_Iter;
cout << endl;
return;
/*
ms1 = 10 20 20 40
ms2 = 20 10
ms3 = 30
ms4 = 10 20 20 40
ms5 = 10 20
ms6 = 10
请按任意键继续. . .
*/
}
//返回的地址集的第一个元素的迭代
void multiset_begin(void)
{
using namespace std;
multiset <int> ms1;
multiset <int>::iterator ms1_Iter;
multiset <int>::const_iterator ms1_cIter;
ms1.insert(1);
ms1.insert(2);
ms1.insert(3);
ms1_Iter = ms1.begin();
cout << "The first element of ms1 is " << *ms1_Iter << endl;
ms1_Iter = ms1.begin();
ms1.erase(ms1_Iter);
// The following 2 lines would err as the iterator is const
// ms1_cIter = ms1.begin( );
// ms1.erase( ms1_cIter );
ms1_cIter = ms1.begin();
cout << "The first element of ms1 is now " << *ms1_cIter << endl;
return;
/*
The first element of ms1 is 1
The first element of ms1 is now 2
请按任意键继续. . .
*/
}
//擦除的一组中的所有元素
void multiset_clear(void)
{
using namespace std;
multiset <int> ms1;
ms1.insert(1);
ms1.insert(2);
cout << "The size of the multiset is initially "
<< ms1.size() << "." << endl;
ms1.clear();
cout << "The size of the multiset after clearing is "
<< ms1.size() << "." << endl;
return;
/*
The size of the multiset is initially 2.
The size of the multiset after clearing is 0.
请按任意键继续. . .
*/
}
//返回一组,其关键的参数指定的关键字相匹配的元素数
void multiset_count(void)
{
using namespace std;
multiset<int> ms1;
multiset<int>::size_type i;
ms1.insert(1);
ms1.insert(1);
ms1.insert(2);
// Elements do not need to be unique in multiset,
// so duplicates are allowed and counted.
i = ms1.count(1);
cout << "The number of elements in ms1 with a sort key of 1 is: "
<< i << "." << endl;
i = ms1.count(2);
cout << "The number of elements in ms1 with a sort key of 2 is: "
<< i << "." << endl;
i = ms1.count(3);
cout << "The number of elements in ms1 with a sort key of 3 is: "
<< i << "." << endl;
return;
/*
The number of elements in ms1 with a sort key of 1 is: 2.
The number of elements in ms1 with a sort key of 2 is: 1.
The number of elements in ms1 with a sort key of 3 is: 0.
请按任意键继续. . .
*/
}
//返回set是否为空
void multiset_empty(void)
{
using namespace std;
multiset <int> ms1, ms2;
ms1.insert(1);
if (ms1.empty())
cout << "The multiset ms1 is empty." << endl;
else
cout << "The multiset ms1 is not empty." << endl;
if (ms2.empty())
cout << "The multiset ms2 is empty." << endl;
else
cout << "The multiset ms2 is not empty." << endl;
return;
/*
The multiset ms1 is not empty.
The multiset ms2 is empty.
请按任意键继续. . .
*/
}
//返回解决了一套成功的最后一个元素的位置的迭代器
void multiset_end(void)
{
using namespace std;
multiset <int> ms1;
multiset <int> ::iterator ms1_Iter;
multiset <int> ::const_iterator ms1_cIter;
ms1.insert(1);
ms1.insert(2);
ms1.insert(3);
ms1_Iter = ms1.end();
ms1_Iter--;
cout << "The last element of ms1 is " << *ms1_Iter << endl;
ms1.erase(ms1_Iter);
// The following 3 lines would err as the iterator is const
// ms1_cIter = ms1.end( );
// ms1_cIter--;
// ms1.erase( ms1_cIter );
ms1_cIter = ms1.end();
ms1_cIter--;
cout << "The last element of ms1 is now " << *ms1_cIter << endl;
return;
/*
The last element of ms1 is 3
The last element of ms1 is now 2
请按任意键继续. . .
*/
}
//返回一对迭代的分别与第一元件中的一组与键比指定的键,并在该组用钥匙比该关键等于或大于第一元件更大
void multiset_equal_range(void)
{
using namespace std;
typedef multiset<int, less<int> > IntSet;
IntSet ms1;
multiset <int> ::const_iterator ms1_RcIter;
ms1.insert(10);
ms1.insert(20);
ms1.insert(30);
pair <IntSet::const_iterator, IntSet::const_iterator> p1, p2;
p1 = ms1.equal_range(20);
cout << "The upper bound of the element with "
<< "a key of 20 in the multiset ms1 is: "
<< *(p1.second) << "." << endl;
cout << "The lower bound of the element with "
<< "a key of 20 in the multiset ms1 is: "
<< *(p1.first) << "." << endl;
// Compare the upper_bound called directly
ms1_RcIter = ms1.upper_bound(20);
cout << "A direct call of upper_bound( 20 ) gives "
<< *ms1_RcIter << "," << endl
<< "matching the 2nd element of the pair"
<< " returned by equal_range( 20 )." << endl;
p2 = ms1.equal_range(40);
// If no match is found for the key,
// both elements of the pair return end( )
if ((p2.first == ms1.end()) && (p2.second == ms1.end()))
cout << "The multiset ms1 doesn't have an element "
<< "with a key less than 40." << endl;
else
cout << "The element of multiset ms1 with a key >= 40 is: "
<< *(p1.first) << "." << endl;
return;
/*
The upper bound of the element with a key of 20 in the multiset ms1 is: 30.
The lower bound of the element with a key of 20 in the multiset ms1 is: 20.
A direct call of upper_bound( 20 ) gives 30,
matching the 2nd element of the pair returned by equal_range( 20 ).
The multiset ms1 doesn't have an element with a key less than 40.
请按任意键继续. . .
*/
}
//删除元素或一个范围从指定的位置中的一组的元素或删除匹配指定键的元素
void multiset_erase(void)
{
using namespace std;
multiset<int> ms1, ms2, ms3;
multiset<int>::iterator pIter, Iter1, Iter2;
int i;
multiset<int>::size_type n;
for (i = 1; i < 5; i++)
{
ms1.insert(i);
ms2.insert(i * i);
ms3.insert(i - 1);
}
// The 1st member function removes an element at a given position
Iter1 = ++ms1.begin();
ms1.erase(Iter1);
cout << "After the 2nd element is deleted, the multiset ms1 is:";
for (pIter = ms1.begin(); pIter != ms1.end(); pIter++)
cout << " " << *pIter;
cout << "." << endl;
// The 2nd member function removes elements
// in the range [_First, _Last)
Iter1 = ++ms2.begin();
Iter2 = --ms2.end();
ms2.erase(Iter1, Iter2);
cout << "After the middle two elements are deleted, "
<< "the multiset ms2 is:";
for (pIter = ms2.begin(); pIter != ms2.end(); pIter++)
cout << " " << *pIter;
cout << "." << endl;
// The 3rd member function removes elements with a given _Key
ms3.insert(2);
n = ms3.erase(2);
cout << "After the element with a key of 2 is deleted,\n"
<< "the multiset ms3 is:";
for (pIter = ms3.begin(); pIter != ms3.end(); pIter++)
cout << " " << *pIter;
cout << "." << endl;
// The 3rd member function returns the number of elements removed
cout << "The number of elements removed from ms3 is: "
<< n << "." << endl;
// The dereferenced iterator can also be used to specify a key
Iter1 = ++ms3.begin();
ms3.erase(Iter1);
cout << "After another element with a key"
<< endl;
cout << "equal to that of the 2nd element is deleted, "
<< "the multiset ms3 is:";
for (pIter = ms3.begin(); pIter != ms3.end(); pIter++)
cout << " " << *pIter;
cout << "." << endl;
return;
/*
After the 2nd element is deleted, the multiset ms1 is: 1 3 4.
After the middle two elements are deleted, the multiset ms2 is: 1 16.
After the element with a key of 2 is deleted,
the multiset ms3 is: 0 1 3.
The number of elements removed from ms3 is: 2.
After another element with a key
equal to that of the 2nd element is deleted, the multiset ms3 is: 0 3.
请按任意键继续. . .
*/
}
//返回迭代寻址一组具有键相当于指定的键的元素的位置
void multiset_find(void)
{
using namespace std;
multiset <int> ms1;
multiset <int> ::const_iterator ms1_AcIter, ms1_RcIter;
ms1.insert(10);
ms1.insert(20);
ms1.insert(20);
ms1_RcIter = ms1.find(20);
cout << "The first element of multiset ms1 with a key of 20 is: "
<< *ms1_RcIter << "." << endl;
ms1_RcIter = ms1.find(40);
// If no match is found for the key, end( ) is returned
if (ms1_RcIter == ms1.end())
cout << "The multiset ms1 doesn't have an element "
<< "with a key of 40." << endl;
else
cout << "The element of multiset ms1 with a key of 40 is: "
<< *ms1_RcIter << "." << endl;
// The element at a specific location in the multiset can be
// found using a dereferenced iterator addressing the location
ms1_AcIter = ms1.end();
ms1_AcIter--;
ms1_RcIter = ms1.find(*ms1_AcIter);
cout << "The first element of ms1 with a key matching" << endl
<< "that of the last element is: "
<< *ms1_RcIter << "." << endl;
// Note that the first element with a key equal to
// the key of the last element is not the last element
if (ms1_RcIter == --ms1.end())
cout << "This is the last element of multiset ms1."
<< endl;
else
cout << "This is not the last element of multiset ms1."
<< endl;
return;
/*
The first element of multiset ms1 with a key of 20 is: 20.
The multiset ms1 doesn't have an element with a key of 40.
The first element of ms1 with a key matching
that of the last element is: 20.
This is not the last element of multiset ms1.
请按任意键继续. . .
*/
}
//返回分配器对象的副本,用于构造该组
void multiset_get_allocator(void)
{
using namespace std;
multiset <int>::allocator_type ms1_Alloc;
multiset <int>::allocator_type ms2_Alloc;
multiset <double>::allocator_type ms3_Alloc;
multiset <int>::allocator_type ms4_Alloc;
/*
// The following lines declare objects
// that use the default allocator.
multiset <int> ms1;
multiset <int, allocator<int> > ms2;
multiset <double, allocator<double> > ms3;
cout << "The number of integers that can be allocated"
<< endl << "before free memory is exhausted: "
<< ms2.max_size() << "." << endl;
cout << "The number of doubles that can be allocated"
<< endl << "before free memory is exhausted: "
<< ms3.max_size() << "." << endl;
// The following lines create a multiset ms4
// with the allocator of multiset ms1
ms1_Alloc = ms1.get_allocator();
multiset <int> ms4(less<int>(), ms1_Alloc);
ms4_Alloc = ms4.get_allocator();
// Two allocators are interchangeable if
// storage allocated from each can be
// deallocated with the other
if (ms1_Alloc == ms4_Alloc)
{
cout << "Allocators are interchangeable."
<< endl;
}
else
{
cout << "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.
Allocators are interchangeable.
*/
}
//插入一个元素或一定范围内的元素为一组
void multiset_insert(void)
{
using namespace std;
multiset <int>::iterator ms1_pIter, ms2_pIter;
multiset <int, less<int> > ms1, ms2;
ms1.insert(10);
ms1.insert(20);
ms1.insert(30);
ms1.insert(40);
cout << "The original ms1 =";
for (ms1_pIter = ms1.begin(); ms1_pIter != ms1.end(); ms1_pIter++)
cout << " " << *ms1_pIter;
cout << "." << endl;
ms1.insert(20);
ms1.insert(--ms1.end(), 50);
cout << "After the insertions, ms1 =";
for (ms1_pIter = ms1.begin(); ms1_pIter != ms1.end(); ms1_pIter++)
cout << " " << *ms1_pIter;
cout << "." << endl;
ms2.insert(100);
ms2.insert(++ms1.begin(), --ms1.end());
cout << "ms2 =";
for (ms2_pIter = ms2.begin(); ms2_pIter != ms2.end(); ms2_pIter++)
cout << " " << *ms2_pIter;
cout << "." << endl;
return;
/*
The original ms1 = 10 20 30 40.
After the insertions, ms1 = 10 20 20 30 40 50.
ms2 = 20 20 30 40 100.
请按任意键继续. . .
*/
}
//检索用于在一组命令键的比较对象的副本
void multiset_key_comp(void)
{
using namespace std;
multiset <int, less<int> > ms1;
multiset <int, less<int> >::key_compare kc1 = ms1.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 ms1."
<< endl;
}
multiset <int, greater<int> > ms2;
multiset <int, greater<int> >::key_compare kc2 = ms2.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 ms2."
<< endl;
}
else
{
cout << "kc2( 2,3 ) returns value of false, "
<< "where kc2 is the function object of ms2."
<< 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 ms2.
请按任意键继续. . .
*/
}
//返回一个迭代的第一元件中的一组与键比指定的键等于或大于
void multiset_lower_bound(void)
{
using namespace std;
multiset <int> ms1;
multiset <int> ::const_iterator ms1_AcIter, ms1_RcIter;
ms1.insert(10);
ms1.insert(20);
ms1.insert(30);
ms1_RcIter = ms1.lower_bound(20);
cout << "The element of multiset ms1 with a key of 20 is: "
<< *ms1_RcIter << "." << endl;
ms1_RcIter = ms1.lower_bound(40);
// If no match is found for the key, end( ) is returned
if (ms1_RcIter == ms1.end())
cout << "The multiset ms1 doesn't have an element "
<< "with a key of 40." << endl;
else
cout << "The element of multiset ms1 with a key of 40 is: "
<< *ms1_RcIter << "." << endl;
// The element at a specific location in the multiset can be
// found using a derefenced iterator addressing the location
ms1_AcIter = ms1.end();
ms1_AcIter--;
ms1_RcIter = ms1.lower_bound(*ms1_AcIter);
cout << "The element of ms1 with a key matching "
<< "that of the last element is: "
<< *ms1_RcIter << "." << endl;
return;
/*
The element of multiset ms1 with a key of 20 is: 20.
The multiset ms1 doesn't have an element with a key of 40.
The element of ms1 with a key matching that of the last element is: 30.
请按任意键继续. . .
*/
}
//返回该组的最大长度
void multiset_max_size(void)
{
using namespace std;
multiset <int> ms1;
multiset <int>::size_type i;
i = ms1.max_size();
cout << "The maximum possible length "
<< "of the multiset is " << i << "." << endl;
return;
/*
The maximum possible length of the multiset is 214748364.
请按任意键继续. . .
*/
}
//返回一个迭代解决的第一个元素的集合逆转
void multiset_rbegin(void)
{
using namespace std;
multiset <int> ms1;
multiset <int>::iterator ms1_Iter;
multiset <int>::reverse_iterator ms1_rIter;
ms1.insert(10);
ms1.insert(20);
ms1.insert(30);
ms1_rIter = ms1.rbegin();
cout << "The first element in the reversed multiset is "
<< *ms1_rIter << "." << endl;
// begin can be used to start an interation
// throught a multiset in a forward order
cout << "The multiset is:";
for (ms1_Iter = ms1.begin(); ms1_Iter != ms1.end(); ms1_Iter++)
cout << " " << *ms1_Iter;
cout << endl;
// rbegin can be used to start an interation
// throught a multiset in a reverse order
cout << "The reversed multiset is:";
for (ms1_rIter = ms1.rbegin(); ms1_rIter != ms1.rend(); ms1_rIter++)
cout << " " << *ms1_rIter;
cout << endl;
// A multiset element can be erased by dereferencing to its key
ms1_rIter = ms1.rbegin();
ms1.erase(*ms1_rIter);
ms1_rIter = ms1.rbegin();
cout << "After the erasure, the first element "
<< "in the reversed multiset is " << *ms1_rIter << "."
<< endl;
return;
/*
The first element in the reversed multiset is 30.
The multiset is: 10 20 30
The reversed multiset is: 30 20 10
After the erasure, the first element in the reversed multiset is 20.
请按任意键继续. . .
*/
}
//返回在颠倒一套解决成功的最后一个元素的位置的迭代器
void multiset_rend(void)
{
using namespace std;
multiset <int> ms1;
multiset <int>::iterator ms1_Iter;
multiset <int>::reverse_iterator ms1_rIter;
multiset <int>::const_reverse_iterator ms1_crIter;
ms1.insert(10);
ms1.insert(20);
ms1.insert(30);
ms1_rIter = ms1.rend();
ms1_rIter--;
cout << "The last element in the reversed multiset is "
<< *ms1_rIter << "." << endl;
// end can be used to terminate an interation
// throught a multiset in a forward order
cout << "The multiset is: ";
for (ms1_Iter = ms1.begin(); ms1_Iter != ms1.end(); ms1_Iter++)
cout << *ms1_Iter << " ";
cout << "." << endl;
// rend can be used to terminate an interation
// throught a multiset in a reverse order
cout << "The reversed multiset is: ";
for (ms1_rIter = ms1.rbegin(); ms1_rIter != ms1.rend(); ms1_rIter++)
cout << *ms1_rIter << " ";
cout << "." << endl;
ms1_rIter = ms1.rend();
ms1_rIter--;
ms1.erase(*ms1_rIter);
ms1_rIter = ms1.rend();
--ms1_rIter;
cout << "After the erasure, the last element in the "
<< "reversed multiset is " << *ms1_rIter << "." << endl;
return;
/*
The last element in the reversed multiset is 10.
The multiset is: 10 20 30 .
The reversed multiset is: 30 20 10 .
After the erasure, the last element in the reversed multiset is 20.
请按任意键继续. . .
*/
}
//返回集合中的元素的数量
void multiset_size(void)
{
using namespace std;
multiset <int> ms1;
multiset <int> ::size_type i;
ms1.insert(1);
i = ms1.size();
cout << "The multiset length is " << i << "." << endl;
ms1.insert(2);
i = ms1.size();
cout << "The multiset length is now " << i << "." << endl;
return;
/*
The multiset length is 1.
The multiset length is now 2.
请按任意键继续. . .
*/
}
//交换两个set元素
void multiset_swap(void)
{
using namespace std;
multiset <int> ms1, ms2, ms3;
multiset <int>::iterator ms1_Iter;
ms1.insert(10);
ms1.insert(20);
ms1.insert(30);
ms2.insert(100);
ms2.insert(200);
ms3.insert(300);
cout << "The original multiset ms1 is:";
for (ms1_Iter = ms1.begin(); ms1_Iter != ms1.end(); ms1_Iter++)
cout << " " << *ms1_Iter;
cout << "." << endl;
// This is the member function version of swap
ms1.swap(ms2);
cout << "After swapping with ms2, list ms1 is:";
for (ms1_Iter = ms1.begin(); ms1_Iter != ms1.end(); ms1_Iter++)
cout << " " << *ms1_Iter;
cout << "." << endl;
// This is the specialized template version of swap
swap(ms1, ms3);
cout << "After swapping with ms3, list ms1 is:";
for (ms1_Iter = ms1.begin(); ms1_Iter != ms1.end(); ms1_Iter++)
cout << " " << *ms1_Iter;
cout << "." << endl;
return;
/*
The original multiset ms1 is: 10 20 30.
After swapping with ms2, list ms1 is: 100 200.
After swapping with ms3, list ms1 is: 300.
请按任意键继续. . .
*/
}
//返回一个迭代器的第一个元素一组用钥匙比指定的键更大
void multiset_upper_bound(void)
{
using namespace std;
multiset <int> ms1;
multiset <int> ::const_iterator ms1_AcIter, ms1_RcIter;
ms1.insert(10);
ms1.insert(20);
ms1.insert(30);
ms1_RcIter = ms1.upper_bound(20);
cout << "The first element of multiset ms1 with a key greater "
<< "than 20 is: " << *ms1_RcIter << "." << endl;
ms1_RcIter = ms1.upper_bound(30);
// If no match is found for the key, end( ) is returned
if (ms1_RcIter == ms1.end())
cout << "The multiset ms1 doesn't have an element "
<< "with a key greater than 30." << endl;
else
cout << "The element of multiset ms1 with a key > 40 is: "
<< *ms1_RcIter << "." << endl;
// The element at a specific location in the multiset can be
// found using a dereferenced iterator addressing the location
ms1_AcIter = ms1.begin();
ms1_RcIter = ms1.upper_bound(*ms1_AcIter);
cout << "The first element of ms1 with a key greater than"
<< endl << "that of the initial element of ms1 is: "
<< *ms1_RcIter << "." << endl;
return;
/*
The first element of multiset ms1 with a key greater than 20 is: 30.
The multiset ms1 doesn't have an element with a key greater than 30.
The first element of ms1 with a key greater than
that of the initial element of ms1 is: 20.
请按任意键继续. . .
*/
}
//检索使用的比较对象的副本一组命令元素值
void multiset_value_comp(void)
{
using namespace std;
multiset <int, less<int> > ms1;
multiset <int, less<int> >::value_compare vc1 = ms1.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 ms1."
<< endl;
}
else
{
cout << "vc1( 2,3 ) returns value of false, "
<< "where vc1 is the function object of ms1."
<< endl;
}
set <int, greater<int> > ms2;
set<int, greater<int> >::value_compare vc2 = ms2.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 ms2."
<< endl;
}
else
{
cout << "vc2( 2,3 ) returns value of false, "
<< "where vc2 is the function object of ms2."
<< endl;
}
return;
/*
vc1( 2,3 ) returns value of true, where vc1 is the function object of ms1.
vc2( 2,3 ) returns value of false, where vc2 is the function object of ms2.
请按任意键继续. . .
*/
}
相关文章推荐
- c++STL基本使用
- C++单元测试工具unittest++使用小结
- c++ sets
- Autoencoder的C++代码(带注释)。
- 用C++写Stash
- 1023. Have Fun with Numbers (20)
- Leetcode 14. Longest Common Prefix
- C++ 运算符重载
- C++ typedef用法详解
- Leetcode 13. Roman to Integer
- 课程设计__C++初步,C++对C的扩充
- Leetcode 12. Integer to Roman
- C++面试题汇集
- C++面试题汇集
- c++ Muitmaps
- 从c++到java的简单过渡
- c语言 翻译密码
- C++的Static函数
- 《More Effective C++》读书笔记-异常
- 程序源入坑之路5(c语言入门)