std::multimap<Key,T,Compare,Allocator>::begin, std::multimap<Key,T,Compare,Allocator>::cbegin - cppreference.com (original) (raw)

iterator begin(); (1) (noexcept since C++11) (constexpr since C++26)
const_iterator begin() const; (2) (noexcept since C++11) (constexpr since C++26)
const_iterator cbegin() const noexcept; (3) (since C++11) (constexpr since C++26)

Returns an iterator to the first element of *this.

If *this is empty, the returned iterator will be equal to end().

range-begin-end.svg

[edit] Return value

Iterator to the first element.

[edit] Complexity

Constant.

Notes

libc++ backports cbegin() to C++98 mode.

[edit] Example

#include #include #include #include #include #include   int main() { auto show_node = [](const auto& node, char ending = '\n') { std::cout << "{ " << node.first << ", " << node.second << " }" << ending; };   std::multimap<std::size_t, std::string> mmap; assert(mmap.begin() == mmap.end()); // OK assert(mmap.cbegin() == mmap.cend()); // OK   mmap.insert({ sizeof(long), "LONG" }); show_node((mmap.cbegin())); assert(mmap.begin() != mmap.end()); // OK assert(mmap.cbegin() != mmap.cend()); // OK mmap.begin()->second = "long"; show_node((mmap.cbegin()));   mmap.insert({ sizeof(int), "int" }); show_node(*mmap.cbegin());   mmap.insert({ sizeof(short), "short" }); show_node(*mmap.cbegin());   mmap.insert({ sizeof(char), "char" }); show_node(*mmap.cbegin());   mmap.insert({{ sizeof(float), "float" }, { sizeof(double), "double"}});   std::cout << "mmap = { "; std::for_each(mmap.cbegin(), mmap.cend(), [&](const auto& n) { show_node(n, ' '); }); std::cout << "};\n"; }

Possible output:

{ 8, LONG } { 8, long } { 4, int } { 2, short } { 1, char } mmap = { { 1, char } { 2, short } { 4, int } { 4, float } { 8, long } { 8, double } };

[edit] See also

| | returns an iterator to the end (public member function) [edit] | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | returns an iterator to the beginning of a container or array (function template) [edit] |