STL / iterator / map - I dont get it

Discussion in 'C++' started by cppquest, Feb 22, 2007.

  1. cppquest

    cppquest Guest

    Hi all,

    I am doing something terrible wrong, but I dont understand why!
    Maybe I am sitting too long in front of this box!
    This is a breakdown of some code:

    #include <map>

    class MyMap : public std::map< double* , int > { };

    MyMap test;

    void findMyDouble(double* d)
    {
    MyMap::iterator iter;
    for( iter = test.begin(); iter != test.end(); ++iter)
    {
    if(*iter->first == 2.0)
    {
    //assign t2 to d
    d = iter->first;
    // d == t2
    // *d == 2.0
    // okay until here
    break;
    }
    }
    }

    int main(int argc, char** argv)
    {
    double* t1 = new double(1.0);
    double* t2 = new double(2.0);
    double* t3 = new double(3.0);

    test.insert(MyMap::value_type(t1,10));
    test.insert(MyMap::value_type(t2,11));
    test.insert(MyMap::value_type(t3,12));

    double* search;

    findMyDouble(search);
    //search should be:
    //search == t2!
    //*search == 2.0
    //but search is again not defined

    MyMap::reverse_iterator iter;
    for( iter = test.rbegin(); iter != test.rend(); ++iter)
    delete iter->first;

    return 0;
    }

    I need to extract some map-key, which is a pointer.
    The key is the pointer-itself, but i have to find a specific pointer-
    value.
    in the local scope of findMyDouble everything is okay, but in main
    nothing happens at all.

    Thanks for help for a tired cpp-victim.
     
    cppquest, Feb 22, 2007
    #1
    1. Advertising

  2. cppquest

    red floyd Guest

    cppquest wrote:
    > Hi all,
    >
    > I am doing something terrible wrong, but I dont understand why!
    > Maybe I am sitting too long in front of this box!
    > This is a breakdown of some code:
    >
    > #include <map>
    >
    > class MyMap : public std::map< double* , int > { };
    >
    > MyMap test;
    >
    > void findMyDouble(double* d)
    > {
    > MyMap::iterator iter;
    > for( iter = test.begin(); iter != test.end(); ++iter)
    > {
    > if(*iter->first == 2.0)
    > {
    > //assign t2 to d
    > d = iter->first;
    > // d == t2
    > // *d == 2.0
    > // okay until here
    > break;
    > }
    > }
    > }
    >
    > int main(int argc, char** argv)
    > {
    > double* t1 = new double(1.0);
    > double* t2 = new double(2.0);
    > double* t3 = new double(3.0);
    >
    > test.insert(MyMap::value_type(t1,10));
    > test.insert(MyMap::value_type(t2,11));
    > test.insert(MyMap::value_type(t3,12));
    >
    > double* search;
    >
    > findMyDouble(search);
    > //search should be:
    > //search == t2!
    > //*search == 2.0
    > //but search is again not defined
    >
    > MyMap::reverse_iterator iter;
    > for( iter = test.rbegin(); iter != test.rend(); ++iter)
    > delete iter->first;
    >
    > return 0;
    > }
    >
    > I need to extract some map-key, which is a pointer.
    > The key is the pointer-itself, but i have to find a specific pointer-
    > value.
    > in the local scope of findMyDouble everything is okay, but in main
    > nothing happens at all.


    because search is passed by value. findMyDouble gets a *COPY* of
    search. You need to pass by reference, or return the found value.
     
    red floyd, Feb 22, 2007
    #2
    1. Advertising

  3. cppquest

    Mark P Guest

    cppquest wrote:
    > Hi all,
    >
    > I am doing something terrible wrong, but I dont understand why!
    > Maybe I am sitting too long in front of this box!
    > This is a breakdown of some code:
    >
    > #include <map>
    >
    > class MyMap : public std::map< double* , int > { };
    >
    > MyMap test;
    >
    > void findMyDouble(double* d)
    > {
    > MyMap::iterator iter;
    > for( iter = test.begin(); iter != test.end(); ++iter)
    > {
    > if(*iter->first == 2.0)
    > {
    > //assign t2 to d
    > d = iter->first;
    > // d == t2
    > // *d == 2.0
    > // okay until here
    > break;
    > }
    > }
    > }
    >
    > int main(int argc, char** argv)
    > {
    > double* t1 = new double(1.0);
    > double* t2 = new double(2.0);
    > double* t3 = new double(3.0);
    >
    > test.insert(MyMap::value_type(t1,10));
    > test.insert(MyMap::value_type(t2,11));
    > test.insert(MyMap::value_type(t3,12));
    >
    > double* search;
    >
    > findMyDouble(search);


    If you want to modify the value of search by calling findMyDouble, then
    findMyDouble needs to take a reference (or pointer) to double*. Declare
    it as:

    void findMyDouble( double*& d);

    > //search should be:
    > //search == t2!
    > //*search == 2.0
    > //but search is again not defined
    >
    > MyMap::reverse_iterator iter;
    > for( iter = test.rbegin(); iter != test.rend(); ++iter)
    > delete iter->first;
    >
    > return 0;
    > }
    >
    > I need to extract some map-key, which is a pointer.
    > The key is the pointer-itself, but i have to find a specific pointer-
    > value.
    > in the local scope of findMyDouble everything is okay, but in main
    > nothing happens at all.
    >
    > Thanks for help for a tired cpp-victim.
    >
     
    Mark P, Feb 22, 2007
    #3
  4. cppquest

    bnonaj Guest

    cppquest wrote:
    > Hi all,
    >
    > I am doing something terrible wrong, but I dont understand why!
    > Maybe I am sitting too long in front of this box!
    > This is a breakdown of some code:
    >
    > #include <map>
    >
    > class MyMap : public std::map< double* , int > { };
    >
    > MyMap test;
    >
    > void findMyDouble(double* d)
    > {
    > MyMap::iterator iter;
    > for( iter = test.begin(); iter != test.end(); ++iter)
    > {
    > if(*iter->first == 2.0)
    > {
    > //assign t2 to d
    > d = iter->first;
    > // d == t2
    > // *d == 2.0
    > // okay until here
    > break;
    > }
    > }
    > }
    >
    > int main(int argc, char** argv)
    > {
    > double* t1 = new double(1.0);
    > double* t2 = new double(2.0);
    > double* t3 = new double(3.0);
    >
    > test.insert(MyMap::value_type(t1,10));
    > test.insert(MyMap::value_type(t2,11));
    > test.insert(MyMap::value_type(t3,12));
    >
    > double* search;
    >
    > findMyDouble(search);
    > //search should be:
    > //search == t2!
    > //*search == 2.0
    > //but search is again not defined
    >
    > MyMap::reverse_iterator iter;
    > for( iter = test.rbegin(); iter != test.rend(); ++iter)
    > delete iter->first;
    >
    > return 0;
    > }
    >
    > I need to extract some map-key, which is a pointer.
    > The key is the pointer-itself, but i have to find a specific pointer-
    > value.
    > in the local scope of findMyDouble everything is okay, but in main
    > nothing happens at all.
    >
    > Thanks for help for a tired cpp-victim.
    >

    search needs to be initialised with a valid address. Either that
    or make it a double and call findMyDouble(&search)

    JB
     
    bnonaj, Feb 22, 2007
    #4
  5. cppquest wrote:
    > Hi all,
    >
    > I am doing something terrible wrong, but I dont understand why!
    > Maybe I am sitting too long in front of this box!
    > This is a breakdown of some code:
    >
    > [..]


    void assign_10_to(int a)
    {
    a = 10;
    }

    #include <iostream>

    int main()
    {
    int b = 42;
    assign_10_to(b);
    std::cout << b;
    }

    What's this program going to print? Why? Now, let's change it
    a little bit

    int global_ten = 10;

    void assign_10_to(int* a)
    {
    a = &global_ten;
    }

    #include <iostream>

    int main()
    {
    int b = 42;
    assign_10_to(&b);
    std::cout << b;
    }

    What's this program oging to print? Why? What do you need to
    change to make it print '10'?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 22, 2007
    #5
  6. cppquest

    Dana Good Guest

    On Feb 22, 10:48 am, "Victor Bazarov" <> wrote:
    > cppquest wrote:
    > > Hi all,

    >
    > > I am doing something terrible wrong, but I dont understand why!
    > > Maybe I am sitting too long in front of this box!
    > > This is a breakdown of some code:

    >
    > > [..]

    >
    > void assign_10_to(int a)
    > {
    > a = 10;
    > }
    >
    > #include <iostream>
    >
    > int main()
    > {
    > int b = 42;
    > assign_10_to(b);
    > std::cout << b;
    > }
    >
    > What's this program going to print? Why? Now, let's change it
    > a little bit
    >
    > int global_ten = 10;
    >
    > void assign_10_to(int* a)
    > {
    > a = &global_ten;
    > }
    >
    > #include <iostream>
    >
    > int main()
    > {
    > int b = 42;
    > assign_10_to(&b);
    > std::cout << b;
    > }
    >
    > What's this program oging to print? Why? What do you need to
    > change to make it print '10'?
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    To make it short and sweet,
    - instead of passing "search" (an uninitialized pointer value with no
    storage), pass "&search" (a pointer to the place you expect to find a
    pointer on return from findMyDouble)
    - modify findMyDouble to expect a pointer to a pointer
    - in FindMyDouble, *localSearch = (address of found thing);

    Or, as was suggested above, have findMyDouble return the address of
    the found double. Then you have "search = findMyDouble()" - much
    nicer.

    Dana
     
    Dana Good, Feb 22, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rg
    Replies:
    1
    Views:
    2,162
    tom_usenet
    Jul 22, 2004
  2. Steve Edwards
    Replies:
    11
    Views:
    25,619
    Mike Wahler
    Feb 23, 2006
  3. Rakesh Kumar

    STL Map iterator compilation error

    Rakesh Kumar, Nov 29, 2007, in forum: C++
    Replies:
    2
    Views:
    580
    Rakesh Kumar
    Nov 29, 2007
  4. Replies:
    3
    Views:
    1,007
    Andrew Koenig
    Apr 7, 2008
  5. Jim Anderson

    problem with iterator (map iterator)

    Jim Anderson, Jan 10, 2014, in forum: C++
    Replies:
    3
    Views:
    146
    Luca Risolia
    Jan 13, 2014
Loading...

Share This Page