Namespace
Varianti

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

Da cppreference.com.
< cpp‎ | container‎ | map
 
 
 
std::map
Membri funzioni
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
map::map
map::~map
map::operator=
map::get_allocator
Elemento accesso
Original:
Element access
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
map::at
map::operator[]
Iteratori
Original:
Iterators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
map::begin
map::cbegin

(C++11)
map::end
map::cend

(C++11)
map::rbegin
map::crbegin

(C++11)
map::rend
map::crend

(C++11)
Capacità
Original:
Capacity
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
map::empty
map::size
map::max_size
Modificatori
Original:
Modifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
map::clear
map::insert
map::emplace(C++11)
map::emplace_hint(C++11)
map::erase
map::swap
Lookup
Original:
Lookup
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
map::count
map::find
map::equal_range
map::lower_bound
map::upper_bound
Osservatori
Original:
Observers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
map::key_comp
map::value_comp
 
iterator begin();
(fino al c++11)
iterator begin() noexcept;
(dal C++11)
const_iterator begin() const;
(fino al c++11)
const_iterator begin() const noexcept;
(dal C++11)
const_iterator cbegin() const noexcept;

Restituisce un iteratore al primo elemento del container.

Se il container è vuoto, l'iteratore restituito sarà uguale a end().

range-begin-end.svg

Indice

[modifica] Parametri

(nessuno)

[modifica] Valore restituito

Iteratore al primo elemento

[modifica] Complessità

Costante


[modifica] Example

#include <iostream>
#include <map>
 
int main() {
  std::map<int, float> num_map;
  num_map[4] = 4.13;
  num_map[9] = 9.24;
  num_map[1] = 1.09;
  // chiama a_map.begin() e a_map.end()
  for (auto it = num_map.begin(); it != num_map.end(); ++it) {
    std::cout << it->first << ", " << it->second << '\n';
  }
}

Output:

1, 1.09
4, 4.13
9, 9.24

Example using a custom comparison function

#include <cmath>
#include <iostream>
#include <map>
 
struct Point { double x, y; };
 
typedef Point * PointPtr;
//confronta le coordinate x dei due PointPtr
struct PointCmp {
    bool operator()(const PointPtr &lhs, const PointPtr &rhs) const { 
        return lhs->x < rhs->x; 
    }
};
 
int main() {
    //Nota che le coordinate x sono in disordine, ma la map verrà
    // iterata per x crescenti
    Point points[3] = { {2, 0}, {1, 0}, {3, 0} };
 
    //mag è una map che mappa l'indirizzo di un nodo al suo modulo nel piano x-y
    //Le keys sono puntatori a Point, ma noi vogliamo ordinare la map secondo le
    // coordinate x del punto e NON secondo gli indirizzi dei singoli Point.
    //Per ottenere questo risultato usiamo la classe PointCmp dotata di un metodo
    // per confrontare due Point.
    std::map<Point *, double, PointCmp> mag({
        { points,     2 },
        { points + 1, 1 },
        { points + 2, 3 }
    });
 
    //Cambia ogni coordinata y da 0 al suo modulo
    for(auto iter = mag.begin(); iter != mag.end(); ++iter){
        auto cur = iter->first; // puntatore a Node
        cur->y = mag[cur]; // valido anche  cur->y = iter->second;
    }
 
    //Aggiorna e stampa il modulo di ogni nodo
    for(auto iter = mag.begin(); iter != mag.end(); ++iter){
        auto cur = iter->first;
        mag[cur] = std::hypot(cur->x, cur->y);
        std::cout << "The magnitude of (" << cur->x << ", " << cur->y << ") is ";
        std::cout << iter->second << '\n';
    }
 
    //Come sopra, ma con un range-based for loop
    for(auto i : mag) {
        auto cur = i.first;
        cur->y = i.second;
        mag[cur] = std::hypot(cur->x, cur->y);
        std::cout << "The magnitude of (" << cur->x << ", " << cur->y << ") is ";
        std::cout << mag[cur] << '\n';
        //Nota che al contrario di std::cout << iter->second << '\n'; sopra,
        // std::cout << i.second << '\n'; NON stamperà il modulo aggiornato
    }
}

Output:

The magnitude of (1, 1) is 1.41421
The magnitude of (2, 2) is 2.82843
The magnitude of (3, 3) is 4.24264
The magnitude of (1, 1.41421) is 1.73205
The magnitude of (2, 2.82843) is 3.4641
The magnitude of (3, 4.24264) is 5.19615

[modifica] Vedi anche

restituisce un iteratore fino alla fine
Original:
returns an iterator to the end
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(metodo pubblico) [modifica]