跳到主要内容

std::multiset begin() 方法

// Non const version
iterator begin() noexcept;

// Const version
iterator begin() const noexcept;

// Const version
const_iterator cbegin() const noexcept;

返回指向数组末尾之后元素的迭代器

指向 multiset 的第一个元素。如果数组为空,返回的迭代器将等于 end()

参数

(无)

返回值

指向第一个元素的迭代器。

异常

(无)

复杂度

常数 - O(1)

begin 和 cbegin 的区别

对于 const 容器 c,begin 和 cbegin 是相同的 - c.begin() == c.cbegin()

对于非常量类型c的容器,它们返回不同的迭代器

#include <multiset>

int main()
{
std::multiset<int> multiset = { 1, 2, 3 };
auto it = multiset.begin(); // Type: std::multiset<int>::iterator
*it = 5; // ✔ Ok
}

示例

Main.cpp
#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); // all cnt elements have equivalent keys
}
}
可能输出
come:	1
not: 1
out: 1
some: 1
sorted: 2
will: 1
words: 1
本文档源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::multiset begin() 方法

// Non const version
iterator begin() noexcept;

// Const version
iterator begin() const noexcept;

// Const version
const_iterator cbegin() const noexcept;

返回指向数组末尾之后元素的迭代器

指向 multiset 的第一个元素。如果数组为空,返回的迭代器将等于 end()

参数

(无)

返回值

指向第一个元素的迭代器。

异常

(无)

复杂度

常数 - O(1)

begin 和 cbegin 的区别

对于 const 容器 c,begin 和 cbegin 是相同的 - c.begin() == c.cbegin()

对于非常量类型c的容器,它们返回不同的迭代器

#include <multiset>

int main()
{
std::multiset<int> multiset = { 1, 2, 3 };
auto it = multiset.begin(); // Type: std::multiset<int>::iterator
*it = 5; // ✔ Ok
}

示例

Main.cpp
#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); // all cnt elements have equivalent keys
}
}
可能输出
come:	1
not: 1
out: 1
some: 1
sorted: 2
will: 1
words: 1
本文档源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。