std::map::begin, std::map::cbegin
Da cppreference.com.
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().
Indice |
[modifica] Parametri
(nessuno)
[modifica] Valore restituito
Iteratore al primo elemento
[modifica] Complessità
Costante
[modifica] Example
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) |