CppDS.com

C++ 98 11 14 17 20 手册

std::multiset<Key,Compare,Allocator>::begin, std::multiset<Key,Compare,Allocator>::cbegin

来自cppreference.com
< cpp‎ | container‎ | multiset

iterator begin();
(C++11 前)
iterator begin() noexcept;
(C++11 起)
const_iterator begin() const;
(C++11 前)
const_iterator begin() const noexcept;
(C++11 起)
const_iterator cbegin() const noexcept;
(C++11 起)

返回指向 multiset 首元素的迭代器。

multiset 为空,则返回的迭代器将等于 end()

range-begin-end.svg

参数

(无)

返回值

指向首元素的迭代器。

复杂度

常数。

注意

因为 iteratorconst_iterator 都是常迭代器(而且实际上可以是同一类型),故不可能通过任何这些成员函数返回的迭代器修改容器元素。

示例

#include <iostream>
#include <iterator>
#include <set>
#include <string>
 
int main()
{
    const std::multiset<std::string> words = {
        "some", "not", "sorted", "words",
        "will", "come", "out", "sorted",
    };
 
    for (auto it = words.begin(); it != words.end(); ) {
        auto cnt = words.count(*it);
        std::cout << *it << ":\t" << cnt << '\n';
        std::advance(it, cnt); // 所有元素均拥有等价的键
    }
}

输出:

come:	1
not:	1
out:	1
some:	1
sorted:	2
will:	1
words:	1

参阅

(C++11)
返回指向末尾的迭代器
(公开成员函数)
关闭