Comparison of programming languages (associative array): Difference between revisions

Content deleted Content added
C++: The C++03 way of inserting elements in a map was not the way we should do it and TR1 is dead, long life C++11
Line 86:
C++ also has a form of associative array called [[map (C++)|<code>std::map</code>]] (see [[Standard Template Library#Containers]]). One could create a map with the same information as above using C++ with the following code:
 
<source lang=Cpp>
#include <map>
#include <string>
#include <utility>
int main() {
std::map<std::string, std::string> phone_book;
phone_book.insert(std::make_pair("Sally Smart", "555-9999"));
phone_book.insert(std::make_pair("John Doe", "555-1212"));
phone_book.insert(std::make_pair("J. Random Hacker", "553-1337"));
}
</source>
 
Or less efficiently as it creates temporaries <code>std::string</code> values:
<source lang=Cpp>
#include <map>
Line 116 ⟶ 130:
 
<source lang=Cpp>
std::map<std::string, std::string>::iterator curr, end;
for(curr = phone_book.begin(), end = phone_book.end(); curr != end; curr++curr)
std::cout << curr->first << " = " << curr->second << std::endl;
</source>
Line 124 ⟶ 138:
 
<source lang=Cpp>
for(const auto& curr : phone_book)
std::cout << curr.first << " = " << curr.second << std::endl;
</source>
 
In C++, the <code>std::map</code> class is [[Generic programming#Templates in C.2B.2B|templated]] which allows the [[data type]]s of keys and values to be different for different <code>map</code> instances. For a given instance of the <code>map</code> class the keys must be of the same base type. The same must be true for all of the values. Although <code>std::map</code> is typically implemented using a [[self-balancing binary search tree]], C++'s [[Technical Report 1]] (TR1)11 defines a second map called <code>[[std::tr1::unordered mapunordered_map]]</code> with the algorithmic characteristics of a hash table. This is a common vendor extension to the STL as well, usually called <code>hash_map</code>, being available from such implementations as SGI and STLPort.
 
===[[ColdFusion]]===