std::map::begin

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

Parameters

(none).

Return value

Iterator to the first element.

Exceptions

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

Complexity

Constant.

Example

See also

returns an iterator to the end
(public member function)

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
doc_CPP
2016-10-11 10:04:33
Comments
Leave a Comment

Please login to continue.