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

Argus

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.

MisterC

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.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to check if an array exists in ListBox in C#

분류에서Dev

Check if element exists within root/child

분류에서Dev

check if value exists in mongo document array

분류에서Dev

how to check if a word exists on a web page

분류에서Dev

How to check if a couchdb document exists using python

분류에서Dev

How do I check if Metadata exists with ServerManager?

분류에서Dev

How do I check if a variable exists in an 'if' statement?

분류에서Dev

How to check if file already exists, if not, download on Python?

분류에서Dev

How to check if column exists in sqlite in Qt

분류에서Dev

How to check for a value in an array

분류에서Dev

How can I check whether a string of words exists in php

분류에서Dev

How do I check if System::Collections::ArrayList exists / is empty

분류에서Dev

How to check whether field exists in symfony2 form?

분류에서Dev

How to check if number exists after a substring in string using javascript?

분류에서Dev

How to check for an empty Map in a Soy template?

분류에서Dev

How to tell Microsoft Word to not check spelling and grammar within quotation marks?

분류에서Dev

How to check if a number (float or integer) is within a range (0 - 100)

분류에서Dev

Check shell command return value in c

분류에서Dev

How to check for the presence of no value in a "params[:attribute]"

분류에서Dev

How to check only numbers in value - Lua

분류에서Dev

How to check radio button with radio button value

분류에서Dev

How to check if user input is not an int value

분류에서Dev

How to check if a value/property exist in JSON data

분류에서Dev

how to check for a null value in t-sql

분류에서Dev

Flutter: how to sort a List of Map on basis of value?

분류에서Dev

How to put const string value in map

분류에서Dev

check if file exists bash not working

분류에서Dev

Check if database with specified name exists or not

분류에서Dev

How to check Internet connectivity in my Google map application (Android)

Related 관련 기사

  1. 1

    How to check if an array exists in ListBox in C#

  2. 2

    Check if element exists within root/child

  3. 3

    check if value exists in mongo document array

  4. 4

    how to check if a word exists on a web page

  5. 5

    How to check if a couchdb document exists using python

  6. 6

    How do I check if Metadata exists with ServerManager?

  7. 7

    How do I check if a variable exists in an 'if' statement?

  8. 8

    How to check if file already exists, if not, download on Python?

  9. 9

    How to check if column exists in sqlite in Qt

  10. 10

    How to check for a value in an array

  11. 11

    How can I check whether a string of words exists in php

  12. 12

    How do I check if System::Collections::ArrayList exists / is empty

  13. 13

    How to check whether field exists in symfony2 form?

  14. 14

    How to check if number exists after a substring in string using javascript?

  15. 15

    How to check for an empty Map in a Soy template?

  16. 16

    How to tell Microsoft Word to not check spelling and grammar within quotation marks?

  17. 17

    How to check if a number (float or integer) is within a range (0 - 100)

  18. 18

    Check shell command return value in c

  19. 19

    How to check for the presence of no value in a "params[:attribute]"

  20. 20

    How to check only numbers in value - Lua

  21. 21

    How to check radio button with radio button value

  22. 22

    How to check if user input is not an int value

  23. 23

    How to check if a value/property exist in JSON data

  24. 24

    how to check for a null value in t-sql

  25. 25

    Flutter: how to sort a List of Map on basis of value?

  26. 26

    How to put const string value in map

  27. 27

    check if file exists bash not working

  28. 28

    Check if database with specified name exists or not

  29. 29

    How to check Internet connectivity in my Google map application (Android)

뜨겁다태그

보관