STL之multiset

参见http://www.cplusplus.com/reference/set/multiset/

template < class T,                                     // multiset::key_type/value_type
                 class Compare = less<T>,        // multiset::key_compare/value_compare
                 class Alloc = allocator<T> >    // multiset::allocator_type
              > class multiset;

Multiple-key set
Multisets are containers that store elements following a specific order, and where multiple elements can have equivalent values.
[multiset中的元素会按指定次序进行排序,且元素可以重复]
In a multiset, the value of an element also identifies it (the value is itself the key, of type T). The value of the elements in a multiset cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.
[在multiset中的元素不能被修改(因此multiset中没有重载operator[]),但可以向集合中插入元素或者删除元素]]
Internally, the elements in a multiset are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).
[在内部实现中,multiset中的元素总是通过内部的比较对象按照特定的严格弱排序来进行排列]
Multisets are typically implemented as binary search trees.
[multiset的典型实现是通过二进制搜索树

/*
//constructing multiset
multiset (const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
multiset (InputIterator first, InputIterator last, const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
multiset (const multiset& x);

iterator begin();
iterator end();
reverse_iterator rbegin();
reverse_iterator rend();

void clear();
bool empty() const;
size_type size() const;
void swap(multiset& x);
size_type count(value_type& val) const;multiset& operator=(const multiset& x);iterator find(const value_type& value);*/
#include <iostream>
#include <set>

int main()
{
    int myints[] = {10, 73, 12, 22, 73, 73, 12};
    std::multiset<int> mymultiset(myints, myints+7);
    std::cout<<"73 appears "<<mymultiset.count(73)<<" times in mymultiset.\n";

    system("pause");
    return 0;
}
/*
//insert
iterator insert (const value_type& val);
iterator insert (iterator position, const value_type& val);
void insert (InputIterator first, InputIterator last);

insert element
Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted.
[通过插入的元素来有效地增加容器大小]
Internally, multiset containers keep all their elements sorted following the criterion specified by its comparison object. The elements are always inserted in its respective position following this ordering.
[在内部实现中,multiset容器会用比较对象(comparison object)将元素进行排序,因此插入操作也是根据这一点插入到相对位置]

Return value
In the versions returning a value, this is an iterator pointing to the newly inserted element in the multiset.
[该函数返回一个指向新插入元素的迭代器(如果有返回值的话)]
Member type iterator is a bidirectional iterator type that points to elements.
[成员类型iterator是一个指向元素的双向迭代器类型]

注:
position

Hint for the position where the element can be inserted.
[position的作用是建议元素被插入的位置]
The function optimizes its insertion time if position points to the element that will precede the inserted element.
[如果position对应的元素在插入元素之前的话,该函数会优化插入时间]
Notice that this is just a hint and does not force the new element to be inserted at that position within the multimap container (the elements in a multimap always follow a specific order depending on their key).
[要注意的是,position只是建议元素的插入位置,而不是强制,另外,multiset中的元素会根据前面所述方法进行排序]
*/

#include <iostream>
#include <set>

int main ()
{
    std::multiset<int> mymultiset;
    std::multiset<int>::iterator it;

    // set some initial values:
    for (int i=1; i<=5; i++) mymultiset.insert(i*10);  // 10 20 30 40 50

    it=mymultiset.insert(25);

    it=mymultiset.insert (it,27);    // max efficiency inserting
    it=mymultiset.insert (it,29);    // max efficiency inserting
    it=mymultiset.insert (it,24);    // no max efficiency inserting (24<29)

    int myints[]= {5,10,15};
    mymultiset.insert (myints,myints+3);

    std::cout << "mymultiset contains:";
    for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
        std::cout << ‘ ‘ << *it;
    std::cout << ‘\n‘;

    system("pause");
    return 0;
}
/*
//erase
void erase (iterator position);
size_type erase (const value_type& val);
void erase (iterator first, iterator last);

Return value
For the key-based version (2), the function returns the number of elements erased.
[第二种方式会返回被删除元素的个数]
*/

#include <iostream>
#include <set>

int main ()
{
    std::multiset<int> mymultiset;
    std::multiset<int>::iterator it;

    // insert some values:
    mymultiset.insert (40);                            // 40
    for (int i=1; i<7; i++) mymultiset.insert(i*10);   // 10 20 30 40 40 50 60

    it=mymultiset.begin();
    it++;                                              //    ^

    mymultiset.erase (it);                             // 10 30 40 40 50 60

    mymultiset.erase (40);                             // 10 30 50 60

    it=mymultiset.find (50);
    mymultiset.erase ( it, mymultiset.end() );         // 10 30

    std::cout << "mymultiset contains:";
    for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
        std::cout << ‘ ‘ << *it;
    std::cout << ‘\n‘;

    system("pause");
    return 0;
}
/*
iterator lower_bound(const value_type &k)            //返回>=k的第一个元素的位置
iterate upper_bound(const value_type &k)             //返回>k的第一个元素的位置 

Return iterator to lower bound
Returns an iterator pointing to the first element in the container which is not considered to go before val (i.e., either it is equivalent or goes after).
[返回一个指向容器中第一个不在k之前的元素的迭代器(即指向元素等于或大于k)]
The function uses its internal comparison object (key_comp) to determine this, returning an iterator to the first element for which key_comp(element,val) would return false.
[该函数利用内部的比较对象来比较,返回的迭代器指向的是第一个使得key_comp(element, val)返回false的元素]
If the multiset class is instantiated with the default comparison type (less), the function returns an iterator to the first element that is not less than val.
[如果multiset类的实例化使用的是默认的比较类型(less), 则该函数返回的迭代器指向的是第一个不小于(即大于等于)k的元素]
A similar member function, upper_bound, has the same behavior as lower_bound, except in the case that the multiset contains elements equivalent to val: In this case lower_bound returns an iterator pointing to the first of such elements, whereas upper_bound returns an iterator pointing to the element following the last.
[upper_bound函数的行为与lower_bound相同,只不过upper_bound返回的迭代指向的是第一个大于k的元素]
*/

#include <iostream>
#include <set>

int main ()
{
    std::multiset<int> mymultiset;
    std::multiset<int>::iterator itlow,itup;

    for (int i=1; i<8; i++) mymultiset.insert(i*10); // 10 20 30 40 50 60 70

    itlow = mymultiset.lower_bound (30);             //       ^
    itup = mymultiset.upper_bound (40);              //             ^

    mymultiset.erase(itlow,itup);                    // 10 20 50 60 70

    std::cout << "mymultiset contains:";
    for (std::multiset<int>::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
        std::cout << ‘ ‘ << *it;
    std::cout << ‘\n‘;

    system("pause");
    return 0;
}
/*
key_compare key_comp() const;
value_compare value_comp() const;

Return comparison object
Returns a copy of the comparison object used by the container.
[返回容器中被用来比较元素的比较对象的拷贝]
By default, this is a less object, which returns the same as operator<.
[默认情况下是一个less对象,该对象返回的结果如同operator<]
This object determines the order of the elements in the container: it is a function pointer or a function object that takes two arguments of the same type as the container elements, and returns true if the first argument is considered to go before the second in the strict weak ordering it defines, and false otherwise.
[比较对象决定了容器中元素的排序,比较对象是一个函数指针或者一个函数对象,该函数指针或者函数对象有两个相同类型的参数,如果按照严格弱排序,第一个参数排在第二个参数之前则返回true,否则返回false]
Two elements of a multiset are considered equivalent if key_comp returns false reflexively (i.e., no matter the order in which the elements are passed as arguments).
[如果key_comp()返回false则认为这两个key是相等的]
In multiset containers, the keys to sort the elements are the values themselves, therefore key_comp and its sibling member function value_comp are equivalent.
[在multiset容器中,用来对元素进行排序的key值就是value值本身,因此key_comp和其兄弟成员函数value_comp是相同的]
*/

#include <iostream>
#include <set>

int main ()
{
    std::multiset<int> mymultiset;

    for (int i=0; i<5; i++) mymultiset.insert(i);

    std::multiset<int>::key_compare mycomp = mymultiset.key_comp();

    std::cout << "mymultiset contains:";

    int highest = *mymultiset.rbegin();

    std::multiset<int>::iterator it = mymultiset.begin();
    do {
        std::cout << ‘ ‘ << *it;
    } while ( mycomp(*it++,highest) );

    std::cout << ‘\n‘;

    system("pause");
    return 0;
}
/*
pair<iterator,iterator> equal_range (const value_type& val) const;

Get range of equal elements
Returns the bounds of a range that includes all the elements in the container that are equivalent to val.
[返回包含所有与val相等的元素的区间]
If no matches are found, the range returned has a length of zero, with both iterators pointing to the first element that is considered to go after val according to the container‘s internal comparison object (key_comp).
[如果没有匹配元素,该区间长度为0,且两个迭代器都会指向容器中第一个大于val的元素]

Return value
The function returns a pair, whose member pair::first is the lower bound of the range (the same as lower_bound), and pair::second is the upper bound (the same as upper_bound).
[该函数返回一个pair,其中first指的是区间的lower bound,second指的是区间的upper bound]
*/

#include <iostream>
#include <set>

typedef std::multiset<int>::iterator It;

int main()
{
    int myints[]= {77,30,16,2,30,30};
    std::multiset<int> mymultiset (myints, myints+6);  // 2 16 30 30 30 77

    std::pair<It,It> ret = mymultiset.equal_range(30); //      ^        ^

    mymultiset.erase(ret.first,ret.second);

    std::cout << "mymultiset contains:";
    for (It it=mymultiset.begin(); it!=mymultiset.end(); ++it)
        std::cout << ‘ ‘ << *it;
    std::cout << ‘\n‘;

    system("pause");
    return 0;
}
时间: 2024-10-06 13:53:49

STL之multiset的相关文章

[STL] set &amp; multiset

前言 第一次写这种关于某一个类的常用方法的总结, 参考了Sam 大叔的文章STL之list容器详解, 之后根据cppreference.com网站的资料归纳而来 Set 与 multiset 容器 template< class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key> > class set; template< class Key, class Comp

stl之multiset容器的应用

与set集合容器一样,multiset多重集合容器也使用红黑树组织元素数据,仅仅是multiset容器同意将反复的元素健值插入.而set容器则不同意. set容器所使用的C++标准头文件set.事实上也是multiset容器的头文件.由于这个set头文件也包括multiset所需的红黑树和自身实现文件.仅仅要用宏语句"#include<set>"包括进来,就可对multiset容器的应用代码进行编译. 创建multiset对象 与set容器一样,multiset容器提供例如

STL学习(multiset Vector)

Vector比数组好用多了,毕竟是可变长的. 常用来建图. 需要掌握的就是构造函数,添加数据,删除数据. #include<set> #include<ctime> #include<cmath> #include<cstdio> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #define maxn 1100

C++STL之multiset多重集合容器

multiset多重集合容器 multiset与set一样, 也是使用红黑树来组织元素数据的, 唯一不同的是, multiset允许重复的元素键值插入, 而set则不允许. multiset也需要声明头文件包含"#include<set>", 由于它包含重复元素, 所以, 在插入元素, 删除元素, 查找元素上较set有差别. 1.1multiset元素的插入 下面这个程序插入了重复键值"123", 最后中序遍历了multiset对象. #include&

【multiset】hdu 5349 MZL&#39;s simple problem

[multiset]hdu 5349 MZL's simple problem 题目链接:hdu 5349 MZL's simple problem 题目大意 n次操作,插入元素.删除最小元素.查询最大元素并输出. C++STL的multiset的使用 set--多元集合(元素不可重复),multiset--可重复元素的多元集合 多元集合(MultiSets)和集合(Sets)相像,只不过支持重复对象.(具体用法请参照set容器) set和multiset内部是以平衡二叉树实现的: 从内部数据结

Codeforces-808D Array Division 【multiset】

Description Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements

Codeforces-808D Array Division (multiset 折半???)

题目链接: http://codeforces.com/problemset/problem/808/D 题意: 给定一个数列,移动0或1个数字,使数列能从某个位置分开前后两半的和相等. 思路: from: http://www.cnblogs.com/robin1998/p/6864278.html 我们可以假想有个隔板从左向右扫,每次两边的和的差值为x,若能找到某一边(看那边大)的一个数为x/2,则将它放到另一边就满足条件.扫一遍O(n),数据是无序的,如果暴力查找则总复杂度O(n^2)无法

STL------set和multiset 学习笔记

C++ STL set和multiset 1,set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就像一个集合一样.所有的操作的都是严格在logn时间之内完成,效率非常高.set和multiset的区别是:set插入的元素不能相同,但是multiset可以相同. 创建 multiset<ss> base; 删除:如果删除元素a,那么在定义的比较关系下和a相等的所有元素都会被删除 base.count( a ):set能返回0或者1,multiset是有多

算法问题分类---Top-K问题与多路归并排序

Pro1:寻找前K大数 方法1:K小根堆  后面的值若大于当前根,则替换之,并调整堆 大部分人都推荐的做法是用堆,小根堆.下面具体解释下: 如果K = 1,那么什么都不需要做,直接遍历一遍,时间复杂度O(N). 下面讨论K 比较大的情况,比如1万. 建立一个小根堆,则根是当前最小的第K个数.然后读入N-K个数,每次读入一个数就与当前的根进行比较,如果大于当前根,则替换之,并调整堆.如果小,则读入下一个. 时间复杂度O(N*logK). 方法2:利用快排分区思想: 本题还有一个时间复杂度比较好的做