##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

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

From cppreference.com
< cpp‎ | container‎ | map

[edit template]

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().

### [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] See also

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

### [edit] Example

Run this code

    #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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/container/map/begin&oldid=72255"
Category:

- unconditionally noexcept