| | |
hash map in STL
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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:
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)
#include<map.h> #include<iostream.h> int main() { map<int,int>::iterator mp0_iter; map<int,int>mp0; map<int,int>mp1; mp1.insert(1,13); mp1.insert(2,16); mp1.insert(3,17); for(mp0_iter=mp1.begin();mp0_iter!=mp1.end();mp0_iter++) { cout<<*mp0_iter<<' '; } cout<<endl; return(0); }
Last edited by karthik.c; Mar 5th, 2009 at 8:07 am.
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
The map as a whole can not displayed using cout. Try
•
•
•
•
cout << *mp0_iter.first() << *mp0_iter.second() << '\n';
>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:
In sufficiently conforming versions of the standard library, the -> operator is defined and you can use that instead:
>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:
But that's effort you can avoid by taking advantage of the subscript operator overload:
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)
cout << (*mp0_iter).first << (*mp0_iter).second << '\n';
C++ Syntax (Toggle Plain Text)
cout << mp0_iter->first << mp0_iter->second << '\n';
>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)
mp1.insert ( make_pair ( 1, 13 ) ); mp1.insert ( make_pair ( 2, 16 ) ); mp1.insert ( make_pair ( 3, 17 ) );
C++ Syntax (Toggle Plain Text)
mp1[1] = 13; mp1[2] = 16; mp1[3] = 17;
I'm here to prove you wrong.
hi again,im having some problem while coding with hash_map
my coding was like this:
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??
my coding was like this:
c++ Syntax (Toggle Plain Text)
#include<iostream> #include<hash_map> using namespace std; int main() { typedef pair<int,int>make_pair; hash_map<int,int>::iterator hmp0_iter; hash_map<int,int>hmp0; hmp0.insert(make_pair(1,78)); hmp0.insert(make_pair(3,34)); for(hmp0_iter=hmp0.begin();hmp0_iter!=hmp0.end();hmp0_iter++) { cout<<(*hmp0_iter).second<<' '; } cout<<endl; return(0); }
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??
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
I don't know if there is any stl hash_map. U need to use just map instead, with a #include <map>
•
•
•
•
map<int,int>::iterator hmp0_iter;
map<int,int>hmp0;
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??
http://www.tenouk.com/Module29a.html
so what if their is any way to do it??
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Finding paths in a labyrinth
- Next Thread: Having some issues with a project
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






