How to check if a value exists within a C++ Map -


i'm trying use dynamic programming implement fibonacci. here's .h file:

#ifndef dynamicprogramming_h #define dynamicprogramming_h #include <map>  class dynamicprogramming {     public:         dynamicprogramming ();         ~dynamicprogramming ();         int fibonacci(int value);     private: };  #endif // dynamicprogramming_h 

here's relevant part in .cpp file:

    int dynamicprogramming::fibonacci(int value) {     int result;     std::map<int,int>fibonacci_storage;     std::map<int,int>::iterator valuefinder;     if (value == valuefinder->second){         return fibonacci_storage[value];     }      if (value <= 2 ){         result = 1;     } else {         result = fibonacci(value - 1) + fibonacci(value - 2);     }     fibonacci_storage.insert(std::pair<int,int>(value,result));     return result; } 

my error coming line: if (value == valuefinder->second). says:

could not convert '((dynamicprogramming*)this)->dynamicprogramming::fibonacci_storage.std::map<_key, _tp, _compare, _alloc>::find [with _key = int, _tp = int, _compare = std::less<int>, _alloc = std::allocator<std::pair<const int, int> >, std::map<_key, _tp, _compare, _alloc>::iterator = std::_rb_tree_iterator<std::pair<const int, int> >, std::map<_key, _tp, _compare, _alloc>::key_type = int]((*(const key_type*)(& value)))' 'std::map<int, int>::iterator {aka std::_rb_tree_iterator<std::pair<const int, int> >}' 'bool'

it looks me simple error, i'm not sure stuff means. can me out, i'd master language, seems useful.

valuefinder iterator type std::map<int,int> not associated instance of type.
associate instance (here fibonacci_storage) have assign instance, i.e.

valuefinder = fibonacci_storage.begin(); 

finding element can done source

valuefinder = fibonacci_storage.find(value); 

where value key searching for. check if value in map:

if( valuefinder != fibonacci_storage.end() ) {     // value found } 

and you're done.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -