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 ?
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)
Bill Bryson"...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.."
What did you try?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Last edited by Masterx; 05-01-2009 at 07:41 AM.
Highlight Your Codes
The Boost C++ Libraries (online Reference)
Bill Bryson"...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.."
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.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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)
Bill Bryson"...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.."
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)
Bill Bryson"...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.."
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:
EDIT:Code:variable_declaration_position = declaration_iter->first;
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.Originally Posted by Masterx
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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)
Bill Bryson"...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.."
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)
Bill Bryson"...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.."
It depends. You could use an iterator to iterate over the entire map, or perhaps the iterator is returned by a function.Originally Posted by Masterx
You can do that with an iterator, but you can also do that with a key, since keys in a std::map are unique.Originally Posted by Masterx
Yes, using an iterator would be one way, the other being to use operator[].Originally Posted by Masterx
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.Originally Posted by Masterx
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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)
Bill Bryson"...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.."
And what else besides 0 do you expect it to return?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;
I still suspect that the whole approach must be wrong if you search in a map like that.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.