Разница между массивом и картой

Опубликовано: 6 Января, 2022

Множество:

Массив - это набор элементов, хранящихся в непрерывных ячейках памяти. Идея состоит в том, чтобы хранить вместе несколько предметов одного типа. Это упрощает вычисление положения каждого элемента, просто добавляя смещение к базовому значению, то есть к месту в памяти первого элемента массива (обычно обозначаемого именем массива).
Схематическое изображение массива приведено ниже:

Программа 1:
Ниже приведена иллюстрация одномерного массива:

C ++ 14

// 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 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 ];
# Prthe 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
Выход:
 6 10 5 0

Программа 2:
Ниже приведена иллюстрация 2D-массива:

Карта:

Карта - это ассоциативный контейнер, в котором элементы хранятся в отображенном виде. Каждый элемент имеет значение ключа и сопоставленное значение. Никакие два сопоставленных значения не могут иметь одинаковые ключевые значения.

Схематическое изображение карты приведено ниже:

Программа 1:
Ниже приведена иллюстрация карты:

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 << " The map gquiz1 is : " ;
cout << " KEY ELEMENT " ;
// Print map gquiz1
for (itr = gquiz1.begin();
itr != gquiz1.end(); ++itr) {
cout << ' ' << itr->first
<< ' ' << itr->second
<< ' ' ;
}
return 0;
}

Джава

// 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( " The map gquiz1 is : " );
System.out.print( "KEY ELEMENT " );
// Print map gquiz1
while (itr.hasNext())
{
Map.Entry<Integer,
Integer> entry = itr.next();
System.out.print( ' ' + entry.getKey()
+ " " + entry.getValue()+ " " );
}
}
}
// 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 ( " The map gquiz1 is : " )
print ( "KEY ELEMENT" )
for x, y in gquiz1.items():
print (x, " " , 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( " The map gquiz1 is : " );
Console.Write( " KEY ELEMENT " );
// Print map gquiz1
foreach (KeyValuePair< int ,
int > entry in gquiz1)
{
Console.Write( " " + entry.Key +
" " + entry.Value + " " );
}
}
}
// This code is contributed by Amit Katiyar
Выход:
 Карта gquiz1: 
    КЛЮЧЕВОЙ ЭЛЕМЕНТ
    1 40
    2 30
    3 60

Программа 2:
Ниже представлена неупорядоченная карта:

C ++ 14

// 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 doubl
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 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 doubl
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() + " " );
}
}
// 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 doubl
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 + " " );
}
}
// This code is contributed by Rajput-Ji
Выход:

Contribute 30
GeeksforGeeks 10
Practice 20