Click here to Skip to main content
15,915,172 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
«_Superman_»2-Dec-09 12:27
professional«_Superman_»2-Dec-09 12:27 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Richard MacCutchan2-Dec-09 12:38
mveRichard MacCutchan2-Dec-09 12:38 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug252-Dec-09 12:46
doug252-Dec-09 12:46 
AnswerRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson2-Dec-09 22:57
professionalStuart Dootson2-Dec-09 22:57 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug255-Dec-09 11:47
doug255-Dec-09 11:47 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson5-Dec-09 11:59
professionalStuart Dootson5-Dec-09 11:59 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug255-Dec-09 15:54
doug255-Dec-09 15:54 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson6-Dec-09 0:22
professionalStuart Dootson6-Dec-09 0:22 
Here's a sample program

#include <vector>
#include <algorithm>
#include <iostream>

template <class Key, class Value>
bool CompareKeys(std::pair<Key, Value> const& left,
                 std::pair<Key, Value> const& right)
{
   return left.first < right.first;
}

template <class Key, class Value>
typename std::vector<std::pair<Key, Value> >::const_iterator 
Find(std::vector<std::pair<Key, Value> > const& vec, Key const& key)
{
   typename std::vector<std::pair<Key, Value> >::const_iterator
      found = std::lower_bound(vec.begin(), vec.end(),
                               std::make_pair(key, Value()), // int value doesn't matter
                               &CompareKeys<Key, Value>);

   if (found != vec.end() && found->first != key) return vec.end();
   return found;
}


int main (int argc, char const *argv[])
{
   typedef std::pair<std::string, int> MyPair;
   
   std::vector<MyPair> associativeVector;
   
   associativeVector.push_back(std::make_pair("Value", 40));
   associativeVector.push_back(std::make_pair("Hello", 10));
   associativeVector.push_back(std::make_pair("Test", 30));
   associativeVector.push_back(std::make_pair("Goodbye", 20));
   std::sort(associativeVector.begin(), associativeVector.end(),
             &CompareKeys<std::string, int>);
   
   std::vector<MyPair>::const_iterator found = Find(associativeVector, std::string("Test"));

   if (found != associativeVector.end())
   {
      std::cout << found->first << " = " << found->second << std::endl;
   }
   else std::cout << "Test not found \n";

   found = Find(associativeVector, std::string("NotThere"));

   if (found != associativeVector.end())
   {
      std::cout << found->first << std::endl;
      std::cout << found->second << std::endl;
   }
   else std::cout << "NotThere not found \n";

   return 0;
}


The function CompareKeys is a generic key-ordering function, while Find can be used for any sorted vector (so long as the type Value can be default constructed).

Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug256-Dec-09 13:07
doug256-Dec-09 13:07 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson6-Dec-09 13:41
professionalStuart Dootson6-Dec-09 13:41 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug256-Dec-09 14:20
doug256-Dec-09 14:20 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson6-Dec-09 14:37
professionalStuart Dootson6-Dec-09 14:37 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug256-Dec-09 15:07
doug256-Dec-09 15:07 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson7-Dec-09 22:57
professionalStuart Dootson7-Dec-09 22:57 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug258-Dec-09 3:07
doug258-Dec-09 3:07 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
Stuart Dootson8-Dec-09 5:59
professionalStuart Dootson8-Dec-09 5:59 
GeneralRe: implementation of a key value array (not using std::map) ? Pin
doug258-Dec-09 17:01
doug258-Dec-09 17:01 
Questionre: probelms erasing a multimap entry Pin
Alan Kurlansky2-Dec-09 9:54
Alan Kurlansky2-Dec-09 9:54 
AnswerRe: re: probelms erasing a multimap entry Pin
«_Superman_»2-Dec-09 12:25
professional«_Superman_»2-Dec-09 12:25 
GeneralRe: re: probelms erasing a multimap entry Pin
Alan Kurlansky3-Dec-09 2:51
Alan Kurlansky3-Dec-09 2:51 
QuestionTransparentBlt doesn't mask transparent color with 4-bit and 8-bit source bitmaps Pin
sashoalm2-Dec-09 8:53
sashoalm2-Dec-09 8:53 
AnswerRe: TransparentBlt doesn't mask transparent color with 4-bit and 8-bit source bitmaps Pin
Richard MacCutchan2-Dec-09 12:01
mveRichard MacCutchan2-Dec-09 12:01 
GeneralRe: TransparentBlt doesn't mask transparent color with 4-bit and 8-bit source bitmaps Pin
Rozis2-Dec-09 12:33
Rozis2-Dec-09 12:33 
GeneralRe: TransparentBlt doesn't mask transparent color with 4-bit and 8-bit source bitmaps Pin
Richard MacCutchan2-Dec-09 13:07
mveRichard MacCutchan2-Dec-09 13:07 
GeneralUsing a palette Pin
sashoalm2-Dec-09 17:43
sashoalm2-Dec-09 17:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.