how to assign a map iterator to an int variable?

This is a discussion on how to assign a map iterator to an int variable? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by anon Code: int variable_declaration_position =0; //... int counter =0; if (iter2 != map2.end()) { typestr = "found"; ...

  1. #16
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by anon View Post
    Code:
    int variable_declaration_position =0;
    //...
    int counter =0;
    if (iter2 != map2.end())
    {
        typestr = "found";
        variable_declaration_position = counter++;
    }//end of declaraion if
    
    return variable_declaration_position;
    And what else besides 0 do you expect it to return?

    I still suspect that the whole approach must be wrong if you search in a map like that.
    i think i found another solution for the problem.
    by the way tanx, so whats your suggestion in this case?
    Last edited by Masterx; 05-01-2009 at 10:42 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #17
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    i came up wrtting this code , i used another map to store all of the statements ( lines) entered by user with its line number, now i can finally get rid of that stupid map element index!
    anyways , when i try to compile this i get an error, i dont know why! maybe because i m trying to use a function im getting it !( proly im not doing it right)
    the error im getting is :
    error: no match for 'operator=' in 'iter = (+InputtedLines)->std::map<_Key, _Tp, _Compare, _Alloc>::begin [with _Key = int, _Tp = std::string, _Compare = std::less<int>, _Alloc = std::allocator<std:air<const int, std::string> >]()'|
    would anyone help me correct this ?
    Code:
     #include <map>
     #include <string>
     #include <iostream>
    
     using namespace std;
    
    int main()
    {
    
           ~~;
           ~~;
            index = variable_location = Get_Variable_Location(StringCollector,tokenstr); 
    
    return 0;
    }
    Code:
    //in the name of God
    
     #include <map>
     #include <string>
     #include <iostream>
    
     using namespace std;
    
    int Get_Variable_Location(const map<int,string>& InputtedLines, string token)
    {
    
            int index = 0;
    
            map<int,string>::iterator iter;
    
            iter = InputtedLines.begin();
    
             while ( iter != InputtedLines.end() )
             {
                   if (  (*iter).second == token )
                    {
                            index = (*iter).first;
                    }
    
                   else
                    {
                            iter++;
                    }
             }
    
    return index;
            }//end of function
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What kind of error?

    I don't see you including the header with the declaration of that function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #19
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by anon View Post
    What kind of error?

    I don't see you including the header with the declaration of that function.
    Error message:
    Code:
    error: no match for 'operator=' in 'iter = (+InputtedLines)->std::map<_Key, _Tp, _Compare, _Alloc>::begin [with _Key = int, _Tp = std::string, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, std::string> >]()'|
    ( this indicates this line:
    iter = InputtedLines.begin();
    )

    and about declaration! its just pseudo main! i just forgot to add that!
    the actual function is placed in a separate headerfile
    so in main i have
    Code:
    #include <iostream>
    #include "Get_variable_index.h"
    int main()
    {  
    
                   
    ~~;
           ~~;
            index = variable_location = Get_Variable_Location(StringCollector,tokenstr); 
    return 0;
    }
    there is a problem with function! what could be the cause?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  5. #20
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The map is passed as const reference, and so the iterator should be

    Code:
    map<int,string>::const_iterator iter;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #21
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by anon View Post
    The map is passed as const reference, and so the iterator should be

    Code:
    map<int,string>::const_iterator iter;
    thank you dear anon , thank you very much. by the way why is it obligatory to use const with iterator! should it not be optional in this case? i mean i cant figure it why the iterator must be declared as const as the map!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  7. #22
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    anyone can help me get this one? why do we have to declare a const iterator when we are dealing with a const map!? why is it obligatory ?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  8. #23
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The difference between const_iterator and iterator is that the first allows only read access, which makes sense if the map is const and not meant to be modified.

    When the map instance is const, begin() and end() methods return a const_iterator. If the container is non-const, these methods return iterator. iterator can be implicitly converted to const_iterator (you can always assign the result of begin() and end() to a const_iterator) but not the other way round.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21