Re: [Dev-C++] problem with iteration in map
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: P.D. v. Z. <pa...@di...> - 2005-08-26 15:39:02
|
I usually find it easier to just typedef the map you're using. So, in your case you'd have something like: typedef map<string, string> StringMap; It makes other code a lot shorter and easier to read. For instance, your iterators could be declared like this: StringMap::iterator iter; And your typedef for valType could be written like this: typedef StringMap::value_type valType; On another note; why do you have "void main"? This is illegal in C++, and should not compile (even after you fix the other errors in your code). Also, you should always use the pre-increment operator on iterators, as using the other variant may have unnecessary performance penalties (because it has to store and return the previous value of the iterator, which may be expensive on custom iterators). A modified version of your code that does compile and work correctly is: #include <map> #include <string> #include <iostream> using namespace std; int main() { typedef map<string, string> StringMap; typedef StringMap::value_type valType; StringMap trans_map; trans_map.insert(valType("gratz", "grateful")); trans_map.insert(valType("'em", "them" )); trans_map.insert(valType("cuz", "because" )); trans_map.insert(valType("nah", "no" )); trans_map.insert(valType("sez", "says" )); trans_map.insert(valType("tanx", "thanks" )); trans_map.insert(valType("wuz", "was" )); trans_map.insert(valType("pos", "suppose" )); StringMap::iterator iter; for (iter = trans_map.begin(); iter != trans_map.end(); ++iter) cout << (*iter).first << '\t' << (*iter).second << endl; return 0; } Regards, Paul bhadram rao wrote: > Dear frinends, > I'd a problem. The problem is that I want to know how > to iterate through the map if i am iterating like those in code there > problems like '=' cannot find the overload operator etc.. How to solve > this problem .Also i want to know the importance of allocator. why it is > used. why it is only used in iterator rather than in map definition. > > Please help me. > > > Thanking you, > yours sincerely, > bhadram > > > # include <map> > # include<string> > # include <iostream> > using namespace std; > void main() > { > map<string,string > trans_map; > typedef map<string,string >::value_type valType; > trans_map.insert( valType( "gratz", "grateful" )); > trans_map.insert( valType( "'em", "them" )); > trans_map.insert( valType( "cuz", "because" )); > trans_map.insert( valType( "nah", "no" )); > trans_map.insert( valType( "sez", "says" )); > trans_map.insert( valType( "tanx", "thanks" )); > trans_map.insert( valType( "wuz", "was" )); > trans_map.insert( valType( "pos", "suppose" )); > map<string,less<string>,allocator<string> >::iterator iter; > map<string,less<string>,allocator<stirng> >::iterator > iter_end=trans_map.end(); > for(iter=trans_map.begin();iter!=iter_end;iter++) > { > cout<<(*iter).first<<" "<<(*iter).second<<endl; > } > } > > errors are : > > D:\cpppractice\aug22\textcpp.cpp(21) : error C2679: binary '=' : no > operator defined which takes a right-hand operand of type 'class > std::_Tree<class std::basic_string<char,struct > std::char_traits<char>,class std::allocator<char> >,struct std::pair< > class std::basic_string<char,struct std::char_traits<char>,class > std::allocator<char> > const ,class std::basic_string<char,struct > std::char_traits<char>,class std::allocator<char> > >,struct > std::map<class std::basic_string<char,struct std::char_tr > aits<char>,class std::allocator<char> >,class > std::basic_string<char,struct std::char_traits<char>,class > std::allocator<char> >,struct std::less<class > std::basic_string<char,struct std::char_traits<char>,class > std::allocator<char> > >,class std::all > ocator<class std::basic_string<char,struct std::char_traits<char>,class > std::allocator<char> > > >::_Kfn,struct std::less<class > std::basic_string<char,struct std::char_traits<char>,class > std::allocator<char> > >,class std::allocator<class std::basic > _string<char,struct std::char_traits<char>,class std::allocator<char> > >> >::iterator' (or there is no acceptable conversion) > D:\cpppractice\aug22\textcpp.cpp(21) : error C2679: binary '!=' : no > operator defined which takes a right-hand operand of type 'class > std::_Tree<_K,_Ty,_Kfn,_Pr,_A>::iterator' (or there is no acceptable > conversion) > D:\cpppractice\aug22\textcpp.cpp(23) : error C2679: binary '<<' : no > operator defined which takes a right-hand operand of type 'struct > std::less<class std::basic_string<char,struct > std::char_traits<char>,class std::allocator<char> > >' (or there is > no acceptable conversion) > Error executing cl.exe. > textcpp.obj - 6 error(s), 14 warning(s) > > > > ------------------------------------------------------------------------ > Start your day with Yahoo! - make it your home page > <http://us.rd.yahoo.com/evt=34442/*http://www.yahoo.com/r/hs> |