Zuordnungen in C++ [] (Zuordnungen in C++), Lektion, page 722664
https://www.purl.org/stefan_ram/pub/zuordnungen_c++ (permalink) is the canonical URI of this page.
Stefan Ram
C++-Kurs

Zuordnungen in C++ 

::std::unordered_map Zuordnung von Werten zu Werten. Die Werten, denen Werte zugeordnet werden, nennt man Schlüssel. Die Zuordnung kann während des Programmablaufs verändert werden.

Aufsuchen eines Eintrags

#include <ostream>

#include <iostream>

#include <unordered_map>

int main()

{ ::std::unordered_map< ::std::string, int >const balance

{ { "Hans", 10 },

{ "Lotte", 0 }};

::std::cout <<( *balance.find( "Hans" )).second << '\n'; }

// Man beachte: second

Aufsuchen eines Eintrags mit Prüfung

#include <ostream>

#include <iostream>

#include <unordered_map>

int main()

{ ::std::unordered_map< ::std::string, int >const balance

{ { "Hans", 10 },

{ "Lotte", 0 }};

auto fritz_position = balance.find( "Fritz" );

if( fritz_position == balance.end() )

::std::cout << "\"Fritz\" not found in map.";

else

::std::cout << fritz_position->second;

::std::cout << '\n'; }

// Man beachte: \"

// Man beachte: ->

Hinzufügen eines Eintrags

#include <ostream>

#include <iostream>

#include <unordered_map>

int main()

{ ::std::unordered_map< ::std::string, int >balance

{ { "Hans", 10 },

{ "Lotte", 15 }};

balance.insert( { "Fritz", 20 });

auto fritz_position = balance.find( "Fritz" );

if( fritz_position == balance.end() )

::std::cout << "\"Fritz\" not found in map.";

else

::std::cout << fritz_position->second;

::std::cout << '\n'; }

.at(key)

Ergibt Wert zu Schlüssel oder wirft out_of_range (wie bei ::std::string).

Der Indexoperator []

Die Verwendung von [] legt bei Bedarf automatisch einen Eintrag an. Bei .at() geschieht dies nicht

wc.cpp

#include <iostream>
#include <map>
#include <string>

int main()
{ ::std::string s;
::std::unordered_map< ::std::string, int >counters;
while( ::std::cin >> s )++counters[ s ];
for( auto it = begin( counters ); it != end( counters ); ++it )
::std::cout << it->first << "\t" << it->second << '\n'; }

auto = ::std::map< ::std::string, int >::const_iterator

Zuordnung zu Funktionen

#include <iostream>
#include <string>
#include <unordered_map>

void alpha(){ ::std::cout << "alpha" << '\n'; }

void delta(){ ::std::cout << "delta" << '\n'; }

int main()

{ ::std::unordered_map< ::std::string, void(*)() >m;

m[ "alpha" ]= alpha;

m[ "delta" ]= delta;

m[ "theta" ]= []{ ::std::cout << "theta" << '\n'; };

m[ "alpha" ]();

m[ "theta" ](); }

#include <iostream>

#include <ostream>

#include <string>

#include <unordered_map>

int main()

{ ::std::unordered_map< ::std::string, void( * )() > function

{ { "alpha", [](){ ::std::cout << "Hello world"; }}};

function[ "alpha" ](); }

Behälter

Warnung Verschiedene Behälter bieten oft verlockende Möglichkeiten, aber oft ist ein ::std::vector<T> wesentlich schneller, wenn man darauf achtet, daß aufeinanderfolge Zugriffe darauf auf benachbarte Elemente erfolgen und die Position von Zugriffe sich einigermaßen regelmäßig verändert (ohne wildes Hin- und Herspringen bei den Indizes).

vector<T> reihungsähnlich mit variabler Länge

list<T> doppelt-verbundene Liste

forward_list<T> einfach-verbundene Liste

deque<T> zwei-seitige Reihe

unordered_map<K,V> Zuordnung

unordered_multimap<K,V> Multizuordnung

unordered_set<T> Menge

unordered_multiset<T> Multimenge

set<T> sortierte Menge

multiset<T> sortierte Multimenge

map<K,V> sortierte Zuordnung

multimap<K,V> sortierte Multizuordnung

About this page, Impressum  |   Form for messages to the publisher regarding this page  |   "ram@zedat.fu-berlin.de" (without the quotation marks) is the email-address of Stefan Ram.   |   A link to the start page of Stefan Ram appears at the top of this page behind the text "Stefan Ram".)  |   Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram. relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram722664 stefan_ram:722664 Zuordnungen in C++ Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd722664, slrprddef722664, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram.
https://www.purl.org/stefan_ram/pub/zuordnungen_c++