Formación:
Una array es una colección de elementos almacenados en ubicaciones de memoria contiguas. La idea es almacenar varios artículos del mismo tipo juntos. Esto facilita el cálculo de la posición de cada elemento simplemente agregando un desplazamiento a un valor base, es decir, la ubicación de memoria del primer elemento de la array (generalmente indicado por el nombre de la array).
La representación esquemática de la array se da a continuación:
Programa 1:
a continuación se muestra una ilustración de una array 1D:
C++
// C++ program to illustrate 1D array #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // Given array int arr[] = { 6, 10, 5, 0 }; // Print the array elements for (int i = 0; i < 4; i++) { cout << arr[i] << " "; } return 0; }
Java
// Java program to illustrate 1D array class GFG{ // Driver Code public static void main(String[] args) { // Given array int arr[] = { 6, 10, 5, 0 }; // Print the array elements for (int i = 0; i < 4; i++) { System.out.print(arr[i] + " "); } } } // This code is contributed by Rohit_ranjan
Python3
# Python3 program to illustrate 1D array # Driver Code if __name__ == '__main__': # Given array arr = [6, 10, 5, 0]; # Print the array elements for i in range(0, 4): print(arr[i], end = " "); # This code is contributed by Rohit_ranjan
C#
// C# program to illustrate 1D array using System; class GFG{ // Driver Code public static void Main(String[] args) { // Given array int[] arr = {6, 10, 5, 0}; // Print the array elements for (int i = 0; i < 4; i++) { Console.Write(arr[i] + " "); } } } // This code is contributed by Rajput-Ji
Javascript
<script> // Javascript program to illustrate 1D array // Given array let arr = [6, 10, 5, 0]; // Print the array elements for (let i = 0; i < 4; i++) { document.write(arr[i] + " "); } // This code is contributed by divyeshrabadiya07. </script>
6 10 5 0
Programa 2:
a continuación se muestra una ilustración de una array 2D:
C++
// C++ program to illustrate 1D array #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // A 2D array with 3 rows and // 2 columns int x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; // Print each array element value // Traverse row for (int i = 0; i < 3; i++) { // Traverse column for (int j = 0; j < 2; j++) { // Print element cout << "Element at x[" << i << "][" << j << "]: "; cout << x[i][j] << endl; } } return 0; }
Java
// Java program to illustrate 1D array import java.util.*; class GFG{ // Driver Code public static void main(String[] args) { // A 2D array with 3 rows and // 2 columns int x[][] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; // Print each array element value // Traverse row for (int i = 0; i < 3; i++) { // Traverse column for (int j = 0; j < 2; j++) { // Print element System.out.print("Element at x[" + i + "][" + j + "]: "); System.out.print(x[i][j] + "\n"); } } } } // This code is contributed by Princi Singh
Python3
# Python3 program to illustrate 1D array # Driver Code if __name__ == '__main__': # A 2D array with 3 rows and # 2 columns x = [[0, 1], [2, 3], [4, 5]]; # Print each array element value # Traverse row for i in range(3): # Traverse column for j in range(2): # Print element print("Element at x[" , i , "][" , j , "]: ", end = ""); print(x[i][j]); # This code is contributed by sapnasingh4991
C#
// C# program to illustrate 1D array using System; class GFG{ // Driver Code public static void Main(String[] args) { // A 2D array with 3 rows and // 2 columns int [,]x = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; // Print each array element value // Traverse row for (int i = 0; i < 3; i++) { // Traverse column for (int j = 0; j < 2; j++) { // Print element Console.Write("Element at x[" + i + "," + j + "]: "); Console.Write(x[i,j] + "\n"); } } } } // This code is contributed by Princi Singh
Javascript
<script> // JavaScript program to illustrate 1D array // A 2D array with 3 rows and // 2 columns let x = [ [ 0, 1 ], [ 2, 3 ], [ 4, 5 ] ]; // Print each array element value // Traverse row for (let i = 0; i < 3; i++) { // Traverse column for (let j = 0; j < 2; j++) { // Print element document.write("Element at x[" + i + "][" + j + "]: "); document.write(x[i][j] + "</br>"); } } </script>
Element at x[0][0]: 0 Element at x[0][1]: 1 Element at x[1][0]: 2 Element at x[1][1]: 3 Element at x[2][0]: 4 Element at x[2][1]: 5
Mapa:
Un mapa es un contenedor asociativo que almacena elementos en forma de mapa. Cada elemento tiene un valor clave y un valor asignado. Dos valores asignados no pueden tener valores de clave iguales.
La representación esquemática de Mapa se da a continuación:
Programa 1:
A continuación se muestra una ilustración de un mapa:
C++
// C++ program to illustrate Map #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // Empty map container map<int, int> gquiz1; // Insert elements in Map gquiz1.insert(pair<int, int>(1, 40)); gquiz1.insert(pair<int, int>(2, 30)); gquiz1.insert(pair<int, int>(3, 60)); // Iterator to iterate Map map<int, int>::iterator itr; cout << "\nThe map gquiz1 is : \n"; cout << "\tKEY\tELEMENT\n"; // Print map gquiz1 for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) { cout << '\t' << itr->first << '\t' << itr->second << '\n'; } return 0; }
Java
// Java program to illustrate Map import java.util.*; class GFG{ // Driver Code public static void main(String[] args) { // Empty map container HashMap<Integer, Integer> gquiz1 = new HashMap<Integer, Integer>(); // Insert elements in Map gquiz1.put(1, 40); gquiz1.put(2, 30); gquiz1.put(3, 60); // Iterator to iterate Map Iterator<Map.Entry<Integer, Integer>> itr = gquiz1.entrySet(). iterator(); System.out.print("\nThe map gquiz1 is : \n"); System.out.print("KEY\tELEMENT\n"); // Print map gquiz1 while(itr.hasNext()) { Map.Entry<Integer, Integer> entry = itr.next(); System.out.print('\t' + entry.getKey() + "\t" + entry.getValue()+ "\n"); } } } // This code is contributed by shikhasingrajput
Python3
# Python3 program to illustrate Map # Driver Code if __name__ == '__main__': # Empty map container gquiz1 = dict() # Insert elements in Map gquiz1[1] = 40 gquiz1[2] = 30 gquiz1[3] = 60 print("\nThe map gquiz1 is : ") print("KEY\tELEMENT") for x, y in gquiz1.items(): print(x, "\t", y) # This code is contributed by Rajput-Ji
C#
// C# program to illustrate Map using System; using System.Collections.Generic; class GFG{ // Driver Code public static void Main(String[] args) { // Empty map container Dictionary<int, int> gquiz1 = new Dictionary<int, int>(); // Insert elements in Map gquiz1.Add(1, 40); gquiz1.Add(2, 30); gquiz1.Add(3, 60); Console.Write("\nThe map gquiz1 is : \n"); Console.Write("\tKEY\tELEMENT\n"); // Print map gquiz1 foreach(KeyValuePair<int, int> entry in gquiz1) { Console.Write("\t" + entry.Key + "\t" + entry.Value + "\n"); } } } // This code is contributed by Amit Katiyar
The map gquiz1 is : KEY ELEMENT 1 40 2 30 3 60
Programa 2:
A continuación se muestra una ilustración de un mapa desordenado:
C++
// C++ program to illustrate Map #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // Declaring umap of <string, int> // type key will be of string and // mapped value will be of double unordered_map<string, int> umap; // Insert values by using [] operator umap["GeeksforGeeks"] = 10; umap["Practice"] = 20; umap["Contribute"] = 30; // Traversing an unordered map // and print the key-value pairs for (auto x : umap) cout << x.first << " " << x.second << endl; return 0; }
Java
// Java program to illustrate Map import java.util.*; class GFG{ // Driver Code public static void main(String[] args) { // Declaring umap of <String, int> // type key will be of String and // mapped value will be of double HashMap<String, Integer> umap = new HashMap<>(); // Insert values by using [] operator umap.put("GeeksforGeeks", 10); umap.put("Practice", 20); umap.put("Contribute", 30); // Traversing an unordered map // and print the key-value pairs for(Map.Entry<String, Integer> x : umap.entrySet()) System.out.print(x.getKey() + " " + x.getValue() + "\n"); } } // This code is contributed by amal kumar choubey
C#
// C# program to illustrate Map using System; using System.Collections.Generic; class GFG{ // Driver Code public static void Main(String[] args) { // Declaring umap of <String, int> // type key will be of String and // mapped value will be of double Dictionary<String, int> umap = new Dictionary<String, int>(); // Insert values by using [] operator umap.Add("Contribute", 30); umap.Add("GeeksforGeeks", 10); umap.Add("Practice", 20); // Traversing an unordered map // and print the key-value pairs foreach(KeyValuePair<String, int> x in umap) Console.Write(x.Key + " " + x.Value + "\n"); } } // This code is contributed by Rajput-Ji
Contribute 30 GeeksforGeeks 10 Practice 20
Programa 3:
A continuación se muestra una ilustración de un mapa múltiple:
C++
// C++ program to illustrate Multimap #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // Empty multimap container multimap<int, int> gquiz1; // Insert elements gquiz1.insert(pair<int, int>(1, 40)); gquiz1.insert(pair<int, int>(2, 30)); // Iterator multimap<int, int>::iterator itr; cout << "\nThe multimap gquiz1 is : \n"; cout << "\tKEY\tELEMENT\n"; // Print multimap gquiz1 for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) { cout << '\t' << itr->first << '\t' << itr->second << '\n'; } return 0; }
The multimap gquiz1 is : KEY ELEMENT 1 40 2 30
Programa 4:
a continuación se muestra una ilustración de un multimapa desordenado:
C++
// C++ program to illustrate // unordered multimap #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // Empty initialization unordered_multimap<string, int> umm1; // Initialization by initializer list unordered_multimap<string, int> umm2( { { "apple", 1 }, { "ball", 2 }, { "apple", 10 }, { "cat", 7 }, { "dog", 9 }, { "cat", 6 }, { "apple", 1 } }); // Traversing an unordered_multimap // and print the elements stored for (auto x : umm2) { cout << x.first << " " << x.second << endl; } return 0; }
dog 9 cat 6 cat 7 ball 2 apple 1 apple 10 apple 1
Diferencia entre array y mapa
Formación | Mapa |
---|---|
Un Array es una colección de elementos del mismo tipo de datos. | El mapa es una estructura hash de pares clave y valor . |
Los índices de la lista son números enteros a partir de 0. | Las claves del Mapa pueden ser de cualquier tipo de datos. |
Se accede a los elementos a través de índices. | Se accede a los elementos a través de valores-clave. |
Se mantiene el orden de los elementos introducidos. | No hay garantía de mantener el orden. |
La array puede ser 1D, 2D o multidimensional. | Los mapas pueden ser multimapa, multimapa desordenado, mapa desordenado, etc. |
El tamaño de la array debe especificarse durante la declaración de la array. | El tamaño del mapa es dinámico. |