Namespaces
Variants
Views
Actions

std::map::begin, std::map::cbegin

From cppreference.com
< cpp‎ | container‎ | map
iterator begin();
const_iterator begin() const;
const_iterator cbegin() const;
(since C++11)

Returns an iterator to the first element of the container.

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

range-begin-end.svg

Contents

[edit] Parameters

(none)

[edit] Return value

Iterator to the first element

[edit] Exceptions

(none) (until C++11)
noexcept specification:  
noexcept
  
(since C++11)

[edit] Complexity

Constant

[edit] Example

[edit] See also

returns an iterator to the end
(public member function) [edit]

[edit] Example

#include <map>
#include <string>
#include <iostream>
#include <iterator>
 
int main() {
  std::map<std::string,std::string> a_map;
  a_map["Geely"]    = "Chinese";
  a_map["Peugeot"]  = "French";
  a_map["Mercedes"] = "German";
  a_map["Toyota"]   = "Japanese";
  a_map["Ford"]     = "American";
  a_map["Fiat"]     = "Italian";
 
  for (auto it = a_map.cbegin(); it != std::next(a_map.cbegin(), 3); ++it) {
    std::cout << it->first << " : " << it->second << '\n';
  }
}

Output:

Fiat : Italian
Ford : American
Geely : Chinese