名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 容器‎ | set
 
 
 
 
iterator begin();
(1) (C++11 起無異常丟擲)
const_iterator begin() const;
(2) (C++11 起無異常丟擲)
const_iterator cbegin() const noexcept;
(3) (C++11 起)

返回一個指向set中第一個元素的迭代器。

如果set為空,返回的迭代器將等於end()

range-begin-end.svg

目錄

[編輯] 返回值

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

[編輯] 複雜度

常數時間。

注意

由於 iteratorconst_iterator 都是常量迭代器(實際上可能是同一型別),因此無法透過這些成員函式返回的迭代器修改容器的元素。

libc++ 將 cbegin() 後向移植到 C++98 模式。

[編輯] 示例

#include <algorithm>
#include <iostream>
#include <set>
 
int main()
{
    std::set<int> set{3, 1, 4, 1, 5, 9, 2, 6, 5};
    std::for_each(set.cbegin(), set.cend(), [](int x)
    {
        std::cout << x << ' ';
    });
    std::cout << '\n';
}

輸出

1 2 3 4 5 6 9

[編輯] 參閱

(C++11)
返回指向末尾的迭代器
(公共成員函式) [編輯]
(C++11)(C++14)
返回指向容器或陣列開頭的迭代器
(函式模板) [編輯]