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:

#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);
}

Dani AI

Generated

Brief recap and practical fixes drawn from the thread: the map errors came from two separate issues — trying to stream a container element as if an ostream overload existed for the whole element, and calling an insert overload that doesn’t exist. correctly explained that an iterator yields a std::pair<const Key, T> and you must access its members (first/second) rather than relying on cout << *it. was right that you can’t just hand the whole map to cout.

On the hash_map confusion: that container was never a portable part of the C++ standard. Old compilers and vendor STL ports provided non‑standard hash_map variants (different headers/namespaces and deprecated .h headers), which explains why #include <hash_map> failed on your GCC 3.4.6 but #include <hash_map.h> produced warnings. Also note a bug in the snippet: typedef pair<int,int> make_pair; shadows the standard function std::make_pair and will break calls — don’t reuse standard helper names for your own typedefs.

Modern, portable recommendation (works on current toolchains): use std::unordered_map (C++11 and later). Example pattern (C++11 compatible):

#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<int,int> m;
    m.emplace(1, 78);
    m.emplace(3, 34);

    for (const auto& p : m)
        std::cout << p.first << ' ' << p.second << '\n';
}

Practical checklist before compiling: use standard headers <map>, <unordered_map>, <iostream> (no .h); compile with -std=c++11 or newer when using unordered_map/emplace; don’t shadow make_pair; use emplace, insert(std::make_pair(...)) or operator[] to add elements; and remember unordered_map iteration order is unspecified. If you must support very old compilers, look for TR1 (<tr1/unordered_map>) or your vendor’s <ext/hash_map>/__gnu_cxx alternatives, but prefer upgrading or moving to std::unordered_map for portability and clarity (as noted by and ).

Recommended Answers

All 8 Replies

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:

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:

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:

mp1.insert ( make_pair ( 1, 13 ) );
mp1.insert ( make_pair ( 2, 16 ) );
mp1.insert ( make_pair ( 3, 17 ) );

But that's effort you can avoid by taking advantage of the subscript operator overload:

mp1[1] = 13;
mp1[2] = 16;
mp1[3] = 17;

thanks narue ,i tried out both ways of inserting:using pair object and operator overload and program is working fine.

hi again,im having some problem while coding with hash_map
my coding was like this:

#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);
}

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??

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??

hi again,im having some problem while coding with hash_map
my coding was like this:

#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);
}

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

The namespaceof hash_map is not std. Check the include file <hash_map>.

hi again,im having some problem while coding with hash_map
my coding was like this:

#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);
}

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??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.