-
-
Save parzibyte/0ba63a44296865b0aea4f637e9366e09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
int main() { | |
std::vector<int> edades; | |
// Agregar elementos | |
edades.push_back(21); | |
edades.push_back(50); | |
edades.push_back(23); | |
edades.push_back(44); | |
std::cout << "--->Iterando edades\n"; | |
std::cout << "Con indice usando size_t\n"; | |
// Iterar con índice | |
for (std::size_t i = 0; i < edades.size(); i++) { | |
int edad = edades[i]; | |
std::cout << edad << "\n"; | |
} | |
// Iterar con índice auto | |
// https://parzibyte.me/blog/2019/06/19/modificador-auto-cpp/ | |
std::cout << "Con indice usando auto\n"; | |
for (auto i = 0; i < edades.size(); i++) { | |
int edad = edades[i]; | |
std::cout << edad << "\n"; | |
} | |
// Iterar con punteros | |
std::cout << "Con punteros\n"; | |
for (auto edad = edades.begin(); edad != edades.end(); edad++) { | |
std::cout << *edad << "\n"; | |
} | |
// Otro vector, de cadenas | |
std::vector<std::string> nombres; | |
nombres.push_back("Luis"); | |
nombres.push_back("John Galt"); | |
nombres.push_back("Hank Rearden"); | |
// Iterar al revés con punteros | |
std::cout << "--->Iterando nombres\n"; | |
std::cout << "Con punteros de fin a inicio\n"; | |
for (auto nombre = nombres.rbegin(); nombre != nombres.rend(); nombre++) { | |
std::cout << *nombre << "\n"; | |
} | |
// Iterar al revés con índice | |
std::cout << "Con indice de fin a inicio\n"; | |
for (std::size_t i = nombres.size() - 1; i >= 0; i--) { | |
std::string nombre = nombres[i]; | |
std::cout << nombre << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment