943,734 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 31168
  • C++ RSS
Mar 5th, 2009
0

map in STL

Expand Post »
hi guys, im trying to do map in cpp using STL and when i ran a simple program it was showing error that:
error:
nomatchfor'operator<<'in'std::cout<<(&mp0_iter)->std::_Rb_tree_iterator<_Tp>::operator* [with _Tp = std::pair<const int, int>]()'....
my program was like this:
c++ Syntax (Toggle Plain Text)
  1. #include<map.h>
  2. #include<iostream.h>
  3.  
  4. int main()
  5. {
  6. map<int,int>::iterator mp0_iter;
  7. map<int,int>mp0;
  8. map<int,int>mp1;
  9.  
  10. mp1.insert(1,13);
  11. mp1.insert(2,16);
  12. mp1.insert(3,17);
  13.  
  14. for(mp0_iter=mp1.begin();mp0_iter!=mp1.end();mp0_iter++)
  15. {
  16. cout<<*mp0_iter<<' ';
  17. }
  18. cout<<endl;
  19.  
  20. return(0);
  21. }
Last edited by karthik.c; Mar 5th, 2009 at 8:07 am.
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Mar 5th, 2009
0

Re: hash map in STL

The map as a whole can not displayed using cout. Try
Quote ...
cout << *mp0_iter.first() << *mp0_iter.second() << '\n';
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Mar 5th, 2009
1

Re: hash map in STL

>cout << *mp0_iter.first() << *mp0_iter.second() << '\n';
This won't work, for several reasons. To begin with, first and second are not member functions. Also, the member operator binds more tightly than the indirection operator, so you're trying to call the first and second member functions (which still don't exist) on the iterator itself rather than the value type. You need to wrap the indirection in parens:
C++ Syntax (Toggle Plain Text)
  1. cout << (*mp0_iter).first << (*mp0_iter).second << '\n';
In sufficiently conforming versions of the standard library, the -> operator is defined and you can use that instead:
C++ Syntax (Toggle Plain Text)
  1. cout << mp0_iter->first << mp0_iter->second << '\n';
>mp1.insert(1,13);
>mp1.insert(2,16);
>mp1.insert(3,17);
The map class doesn't have an overload of the insert member function that takes a key/value pair as two arguments. You'd have to create your own pair object first:
C++ Syntax (Toggle Plain Text)
  1. mp1.insert ( make_pair ( 1, 13 ) );
  2. mp1.insert ( make_pair ( 2, 16 ) );
  3. mp1.insert ( make_pair ( 3, 17 ) );
But that's effort you can avoid by taking advantage of the subscript operator overload:
C++ Syntax (Toggle Plain Text)
  1. mp1[1] = 13;
  2. mp1[2] = 16;
  3. mp1[3] = 17;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 6th, 2009
0

Re: hash map in STL

thanks narue ,i tried out both ways of inserting:using pair object and operator overload and program is working fine.
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Mar 6th, 2009
0

Re: hash map in STL

hi again,im having some problem while coding with hash_map
my coding was like this:
c++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<hash_map>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. typedef pair<int,int>make_pair;
  8. hash_map<int,int>::iterator hmp0_iter;
  9.  
  10. hash_map<int,int>hmp0;
  11.  
  12. hmp0.insert(make_pair(1,78));
  13. hmp0.insert(make_pair(3,34));
  14.  
  15. for(hmp0_iter=hmp0.begin();hmp0_iter!=hmp0.end();hmp0_iter++)
  16. {
  17. cout<<(*hmp0_iter).second<<' ';
  18. }
  19. cout<<endl;
  20.  
  21. return(0);
  22. }
error was like this:
hashdemo.cpp:2:19: hash_map: No such file or directory
hashdemo.cpp: In function `int main()':
hashdemo.cpp:8: error: `hash_map' was not declared in this scope
hashdemo.cpp:8: error: expected primary-expression before "int"
hashdemo.cpp:8: error: expected `;' before "int"
hashdemo.cpp:10: error: expected primary-expression before "int"
hashdemo.cpp:10: error: expected `;' before "int"
hashdemo.cpp:12: error: `hmp0' was not declared in this scope
hashdemo.cpp:15: error: `hmp0_iter' was not declared in this scope

but when i replaced #include<hash_map>with #include<hash_map.h>the program ran and printed the output but it did showed some warnings saying im using deprecated header.

im workin in linux using gcc(version3.4.6)so does it support hashmap??
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Mar 6th, 2009
0

Re: hash map in STL

I don't know if there is any stl hash_map. U need to use just map instead, with a #include <map>

Quote ...
map<int,int>::iterator hmp0_iter;
map<int,int>hmp0;
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Mar 6th, 2009
0

Re: hash map in STL

hi shibukumar, i actually was trying to program the code which was given in a site where they are using hash_map at the same time they are also saying hashtable Datastructure is not part of c++ standard library.

http://www.tenouk.com/Module29a.html
so what if their is any way to do it??
Reputation Points: 16
Solved Threads: 0
Light Poster
karthik.c is offline Offline
48 posts
since Feb 2009
Jan 8th, 2010
0
Re: hash map in STL
Click to Expand / Collapse  Quote originally posted by karthik.c ...
hi again,im having some problem while coding with hash_map
my coding was like this:
c++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<hash_map>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. typedef pair<int,int>make_pair;
  8. hash_map<int,int>::iterator hmp0_iter;
  9.  
  10. hash_map<int,int>hmp0;
  11.  
  12. hmp0.insert(make_pair(1,78));
  13. hmp0.insert(make_pair(3,34));
  14.  
  15. for(hmp0_iter=hmp0.begin();hmp0_iter!=hmp0.end();hmp0_iter++)
  16. {
  17. cout<<(*hmp0_iter).second<<' ';
  18. }
  19. cout<<endl;
  20.  
  21. return(0);
  22. }
error was like this:
hashdemo.cpp:2:19: hash_map: No such file or directory
hashdemo.cpp: In function `int main()':
hashdemo.cpp:8: error: `hash_map' was not declared in this scope
hashdemo.cpp:8: error: expected primary-expression before "int"
hashdemo.cpp:8: error: expected `;' before "int"
hashdemo.cpp:10: error: expected primary-expression before "int"
hashdemo.cpp:10: error: expected `;' before "int"
hashdemo.cpp:12: error: `hmp0' was not declared in this scope
hashdemo.cpp:15: error: `hmp0_iter' was not declared in this scope

but when i replaced #include<hash_map>with #include<hash_map.h>the program ran and printed the output but it did showed some warnings saying im using deprecated header.

im workin in linux using gcc(version3.4.6)so does it support hashmap??
Hash_map signature is wrong refer <ext/hash_map>
5 parameter need to be passed
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ambi_ is offline Offline
1 posts
since Jan 2010
Apr 2nd, 2010
0
Re: hash map in STL
The namespaceof hash_map is not std. Check the include file <hash_map>.

Click to Expand / Collapse  Quote originally posted by karthik.c ...
hi again,im having some problem while coding with hash_map
my coding was like this:
c++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<hash_map>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. typedef pair<int,int>make_pair;
  8. hash_map<int,int>::iterator hmp0_iter;
  9.  
  10. hash_map<int,int>hmp0;
  11.  
  12. hmp0.insert(make_pair(1,78));
  13. hmp0.insert(make_pair(3,34));
  14.  
  15. for(hmp0_iter=hmp0.begin();hmp0_iter!=hmp0.end();hmp0_iter++)
  16. {
  17. cout<<(*hmp0_iter).second<<' ';
  18. }
  19. cout<<endl;
  20.  
  21. return(0);
  22. }
error was like this:
hashdemo.cpp:2:19: hash_map: No such file or directory
hashdemo.cpp: In function `int main()':
hashdemo.cpp:8: error: `hash_map' was not declared in this scope
hashdemo.cpp:8: error: expected primary-expression before "int"
hashdemo.cpp:8: error: expected `;' before "int"
hashdemo.cpp:10: error: expected primary-expression before "int"
hashdemo.cpp:10: error: expected `;' before "int"
hashdemo.cpp:12: error: `hmp0' was not declared in this scope
hashdemo.cpp:15: error: `hmp0_iter' was not declared in this scope

but when i replaced #include<hash_map>with #include<hash_map.h>the program ran and printed the output but it did showed some warnings saying im using deprecated header.

im workin in linux using gcc(version3.4.6)so does it support hashmap??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alvinyang is offline Offline
1 posts
since Apr 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: increasing the size of an array
Next Thread in C++ Forum Timeline: C++ new balance code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC