View Subcategory

Define an empty map

ruby
map = {}
cpp
Hashtable^ hash = gcnew Hashtable;
Generic::Dictionary<String^, String^>^ dict = gcnew Generic::Dictionary<String^, String^>();
std::map<int, std::string> m;
fsharp
let map = Map.empty
let map = new Generic.Dictionary<string, string>()
let map = new Hashtable()

Define an unmodifiable empty map

ruby
map = {}.freeze
cpp
const std::map<T1,T2> immutable_map_instance_of_type_t1_to_t2;
fsharp
// Most native fsharp data structures are immutable - updating a 'map' sees a modified copy created
let map = Map.empty

Define an initial map

Define the map {circle:1, triangle:3, square:4}
ruby
shapes = {'circle'=>1, 'triangle'=>3, 'square'=>4}
shapes = Hash['circle', 1, 'triangle', 3, 'square', 4]
shapes = { :circle => 1, :triangle => 3, :square => 4 }
cpp
Hashtable^ shapes = gcnew Hashtable;

shapes->Add("circle", 1);
shapes->Add("triangle", 3);
shapes->Add("square", 4);
Generic::Dictionary<String^, int>^ shapes = gcnew Generic::Dictionary<String^, int>();

shapes->Add("circle", 1);
shapes->Add("triangle", 3);
shapes->Add("square", 4);
map<string, int> shapes;

shapes["circle"] = 1;
shapes["triangle"] = 3;
shapes["square"] = 4;
fsharp
let shapes = Map.ofList [("circle", 1); ("triangle", 3); ("square", 4)]
let shapes = Map.empty.Add("circle", 1).Add("triangle", 3).Add("square", 4)
let shapes = new Generic.Dictionary<string, int>()
shapes.Add("circle", 1)
shapes.Add("triangle", 3)
shapes.Add("square", 4)
let shapes = Map [("circle", 1); ("triangle", 3); ("square", 4)]