Reputation: 911
I'm trying to use Dynamic Programming to implement fibonacci. Here's my .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 the relevant part in my .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 is coming from this line: if (value == valueFinder->second)
. This is what it 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)))' from 'std::map<int, int>::iterator {aka std::_Rb_tree_iterator<std::pair<const int, int> >}' to 'bool'
It looks to me like this is a very simple error, but I'm not sure what all that stuff means. Can someone help me out, I'd really like to master this language, it seems like it would be very useful.
Upvotes: 2
Views: 21433
Reputation: 726509
There are several issues with your code.
The biggest one is not syntax, it's the placement of the cache: since fibonacci_storage
is a local variable, each recursive invocation of Fibonacci
will have its own cache, meaning that there would be no caching at all. You need to move fibonacci_storage
into the private:
section of your class.
As far as fixing the syntax goes, a common trick used to implement searching of cache is to store the result of []
in a reference, like this:
int DynamicProgramming::Fibonacci(int value)
{
int &result = fibonacci_storage[value];
if (result) {
return result;
}
if (value <= 2 ){
return (result = 1);
}
return (result = Fibonacci(value - 1) + Fibonacci(value - 2));
}
Variable result
holds a reference to the value inside the map
, so when you update it in one of the two assign-return statements the corresponding value inside the map
gets updated automatically.
Upvotes: 1
Reputation: 51
To check if a key exists in a C++ map, you can use std::map::count. It returns 0 (the key is absent) or 1 (the key is present).
To check if a value exists in a C++ map, I think that you have to iterate over all pairs.
It seems that your question is more about a compilation error though.
There are several issues in the code above.
Upvotes: 2
Reputation: 249
valueFinder
is just an iterator for the type std::map<int,int>
that is not associated to any instance of that type.
To associate it to an instance (here fibonacci_storage) you have to assign it to that instance, i.e.
valueFinder = fibonacci_storage.begin();
Finding an element can be done with source
valueFinder = fibonacci_storage.find(value);
where value is the key you are searching for. Now you check if value is in the map:
if( valueFinder != fibonacci_storage.end() )
{
// value found
}
and you're done.
Upvotes: 0
Reputation: 2700
As far as I know the only way to search through maps values is to iterate through. If you are trying to see if the key exists then you can use
map.find()
Upvotes: 1