Thread: how to assign a map iterator to an int variable?

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497

    how to assign a map iterator to an int variable?

    hello all, i need to store a map iterator in order to know which element of map is being used ,i already tried simple assignment stuff but well as its plain as day, didnt work!
    how can i achieve such a thing ?
    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What did you try?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    What did you try?

    Code:
           map<int,string>::iterator  declaration_iter;   
           map<int,string> declaration;
           
            ~~~~;
            ~~~~;
    
           if (declaration_iter != map2.end())
            {
                        variable_declaration_position = declaration_iter;
             }
    Last edited by Masterx; 05-01-2009 at 07:41 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


  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How about declaration_iter->first?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to post the smallest and simplest compilable program that demonstrates the problem. Speaking of which, you did not state what exactly was the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    You might want to post the smallest and simplest compilable program that demonstrates the problem. Speaking of which, you did not state what exactly was the problem.
    Ok lets say it this way :
    i have a map which is called declaration , this map holds variables and their value
    i have another map that has the variable name , i use that map , to retrieve the name, and then search the name in declaration map, then when it is found , i wan to retrieve both the variable value and the index inwhich the variable was stored at!
    look :
    Code:
     
     map<int,string>::iterator  declaration_iter;   
     map<int,string> declaration;
    
    map(value + name)
    ____________________________________________
    ____________________________________________
    index         |              |              |
    ______________|______0_______|_______1______|
                  |              |              |
    Declaration   |    (5) Num   |     (0) sum  |
    ______________|______________|______________|
    
    actually iterator is indicating the index( it is index used to point to different pairs used in a map, so i want this index! to be stored somewhere else so that i can use some calculations on it) !
    Last edited by Masterx; 05-01-2009 at 07:43 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


  7. #7
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by matsp View Post
    How about declaration_iter->first?

    --
    Mats
    tanx Mats, but i think this will give me the first element of the pair stored in the map, meaning if i have declaration[5] = "sum", this will give me , 5! but i want the index which this pair is located in the map! and cuz iterator is referring to there i want to use that iter!
    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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You know, it would be much easier if you just showed the code, or at least told us the types of certain variables in your example.

    As it stands, matsp is probably correct. variable_declaration_position is probably an int, so you want to write:
    Code:
    variable_declaration_position = declaration_iter->first;
    EDIT:
    Quote Originally Posted by Masterx
    but i think this will give me the first element of the pair stored in the map, meaning if i have declaration[5] = "sum", this will give me , 5! but i want the index which this pair is located in the map! and cuz iterator is referring to there i want to use that iter!
    What you do mean by "index which this pair is located in the map"? A map is an associative container; it has no concept of indices as with sequential containers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    What you do mean by "index which this pair is located in the map"? A map is an associative container; it has no concept of indices as with sequential containers.
    And even if you find out where it is right now, if you add/remove an element it may well have moved.

    You will probably have to explain what you intend to do with such an information, should you be able to get it...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    You know, it would be much easier if you just showed the code, or at least told us the types of certain variables in your example.

    As it stands, matsp is probably correct. variable_declaration_position is probably an int, so you want to write:
    Code:
    variable_declaration_position = declaration_iter->first;
    EDIT:

    What you do mean by "index which this pair is located in the map"? A map is an associative container; it has no concept of indices as with sequential containers.
    sorry for the vague explanation ,
    but before that would you tell me what is the function of an iterator in a typical map?
    i say , its used to indicate a specific pair stored in the container,
    i mean a map is actually a container that can hold pair of variables like " name and phone number " (phone[Alex]=12545678 ) so , when there are more than one pair of data ( information such as above) in order to access on of those pairs , you actually use the iterator ! and find what you want!

    in my case after find a typical element( lets say varibale) i want to know how many iterations are being done to reach this element .
    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


  11. #11
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by matsp View Post
    And even if you find out where it is right now, if you add/remove an element it may well have moved.

    You will probably have to explain what you intend to do with such an information, should you be able to get it...

    --
    Mats
    there is no way those maps get changed ! and here is the whole function which i use to search and get the information i want ( here iterator ).
    Code:
     #include <map>
     #include <string>
     #include <cstring>
     #include <cctype>
     #include <iostream>
     #include <sstream>
     #include <bitset>
    
     using namespace std;
    
    template<typename K1, typename V1>
    class matchSecond1
    {
    public:
            matchSecond1(const V1& value) : value(value) {}
    
            bool operator()(const std::pair<K1,V1>& element) const
            {
                    return element.second == value;
            }
    private:
        V1 value;
    };
    
    ///search in declaration
     using namespace std;
    
    int Get_Variable_info(const map<int,string>& map1, const map<int,string>& map2, string token)
    {
            int variable_declaration_position =0;
            string typestr;
    
           map<int,string>::const_iterator iter = find_if(map1.begin(), map1.end(), matchSecond1<int,string>(token));
                                    
            if (iter != map1.end())
            {
                    typestr = "Label"; 
                    variable_declaration_position = 0;//dont need this 
            }  
             //else if you couldnt find it in label,search the token in declaration until you find it
    
            map<int,string>::const_iterator iter2 = find_if(map2.begin(), map2.end(), matchSecond1<int,string>(token));
    
            if (iter2 != map2.end())
            {
                    variable_declaration_position = iter2;
           }
    return variable_declaration_position;
            }//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


  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    but before that would you tell me what is the function of an iterator in a typical map?
    It depends. You could use an iterator to iterate over the entire map, or perhaps the iterator is returned by a function.

    Quote Originally Posted by Masterx
    i say , its used to indicate a specific pair stored in the container,
    You can do that with an iterator, but you can also do that with a key, since keys in a std::map are unique.

    Quote Originally Posted by Masterx
    i mean a map is actually a container that can hold pair of variables like " name and phone number " (phone[Alex]=12545678 ) so , when there are more than one pair of data ( information such as above) in order to access on of those pairs , you actually use the iterator ! and find what you want!
    Yes, using an iterator would be one way, the other being to use operator[].

    Quote Originally Posted by Masterx
    in my case after find a typical element( lets say varibale) i want to know how many iterations are being done to reach this element .
    There is no way to determine that, though you can estimate it as log_2(n), where n is the number of elements in the map.

    EDIT:
    Must you really find the element based on the mapped object? The typical use of a map is to find the value based on the key.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Purpose of an iterator is the same as any other iterator: To walk through the contents of a container class. It is a pseudo-pointer into the stored data - it is not really a pointer, but it behaves like one (sort of).

    I still don't quite follow what you want to do.

    If you want to know how many iterations you need to iterate to get to a particular location, then I suggest you just add a counter in your loop.

    If you want something else, then you have to explain what you want to achieve a bit better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    It depends. You could use an iterator to iterate over the entire map, or perhaps the iterator is returned by a function.


    You can do that with an iterator, but you can also do that with a key, since keys in a std::map are unique.


    Yes, using an iterator would be one way, the other being to use operator[].


    There is no way to determine that, though you can estimate it as log_2(n), where n is the number of elements in the map.

    EDIT:
    Must you really find the element based on the mapped object? The typical use of a map is to find the value based on the key.
    unfortunately i happened to do this, actually i used the maps in their usual way
    but an error turned up in my project and the only way to get rid of it is to find out the index of the variable in declaration map which tells me where exactly in the program source code it is written.
    to tell you the truth, im trying to place the variable in its true address in memory!(emulated memory) and for that i must know the order the variables are declared (which one is first , which one is second and etc) and using this
    i can be able to find out and calculate the exact line number they are being declared and then place them in memory
    at first i thought , well i have the declaration map, and simply when i want to specify which came first and which came afterwards, i just look at the iterator!
    i though the elements is map( variables and their value) are sorted secquentialy so that i can just know which one is come firs!
    or at least using the map iterator, would let me know which one came first and tell me the place of the element in map!

    it seems i was wrong! and well i dont know how to go about it!
    as dear Mats told me , i tried this but its not workinG! i just get 0!

    any help!?


    Code:
    #include  <map>
     #include <string>
     #include <iostream>
    
    
    
     using namespace std;
    //map searcher
    template<typename K1, typename V1>
    class matchSecond1
    {
    public:
            matchSecond1(const V1& value) : value(value) {}
    
            bool operator()(const std::pair<K1,V1>& element) const
            {
                    return element.second == value;
            }
    private:
        V1 value;
    };
    
    
    int GET(const map<int,string>& map1, const map<int,string>& map2, string token);
    int main()
    {
    
           map<int,string>::iterator lab_it;
           map<int,string> LABEL; //is used to store/save the list of labels used so far with regards of the current line number
    
    
           map<int,string>::iterator dec_it;   //variable[value]=variable name
           map<int,string> declaration;//variable name and its value
    
    
    
            LABEL[65] = "label1";
            LABEL[12] = "label2";
            LABEL[987] = "label3";
    
            declaration[5] = "number1";
            declaration[96] = "number12";
            declaration[65] = "number13";
            declaration[36] = "number14";
            declaration[15] = "number15";
            declaration[46] = "number16";
            declaration[55] = "number17";
            declaration[54] = "number18";
    
         int answer =  GET(LABEL,declaration,"number14");
    
    
           cout<<"\nindex is "<<answer;
    
    
    
           return 0;
    }
    
    int GET(const map<int,string>& map1, const map<int,string>& map2, string token)
    {
            int variable_declaration_position =0;
            string typestr;
    
           map<int,string>::const_iterator iter = find_if(map1.begin(), map1.end(), matchSecond1<int,string>(token));
                                     //till you havent found the token in label, go on .
            if (iter != map1.end())
            {
                    typestr = "Label";
                    variable_declaration_position = 0;//dont need this
            }
    
            map<int,string>::const_iterator iter2 = find_if(map2.begin(), map2.end(), matchSecond1<int,string>(token));
    
    
    int counter =0;
            if (iter2 != map2.end())
            {
                    typestr = "found";
                    variable_declaration_position = counter++;
    
             }//end of declaraion if
    
    
    return variable_declaration_position;
            }//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


  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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 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).

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-02-2003, 12:26 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM