c++ cli - Accessing a dictionary key by its index -
it's easy question how access value of dictionary key in specific row. let's dict
{ 1,13; 3,14; 5,17 }
second key 3
.
how value? tried dict->key[2]
gave error , can't find reference
update: gives me need maybe there faster way.
dictionary<double, double>::keycollection^ keycoll = dict->keys; double first; double last; int counter=0; int dictionarycount = dict->count; each( double s in keycoll ) { if(counter==0){ first=s; } if(dictionarycount == counter+1){ last=s; } //dict[first] first key //dict[last] last key
the dictionary<> class has indexer, use applying []
operator directly object reference. named item
in msdn library articles. indexer dictionary takes key , returns value key. sample code:
auto dict = gcnew dictionary<int, double>(); dict->add(1, 13); dict->add(3, 14); dict->add(5, 17); auto value = dict[3];
you use trygetvalue() method instead if not sure if key present.
the dictionary
dict<double,double>
do beware using double key very troublesome. comparing floating point values equality filled surprises, none of them ones. must use dictionary(iequalitycomparer<>)
constructor , pass own comparer have hope of surviving this. ask question if necessary.
Comments
Post a Comment