Hi,

I am new to C++ and trying to use map in C++.
I have a sample program as shown below-

#include <iostream>
#include <map>
using namespace std;
int main()
{
    map<char*, char*> m;
    int i;
    char name[64];
    char val[64];
    // put pairs into map
    for(i=0; i<26; i++) {
        sprintf(name, "Hello%d",i);
        sprintf(val, "There%d",i);
        m.insert(pair<char*, char*>((char*)name, (char*)val));
    }

    for(map<char*, char*>::iterator p = m.begin(); p!= m.end(); ++p) {
        cout << p->first << ": " << p->second << endl;
    }

    return 0;
}

I expect this program to print thge output as below-

Hello0: There0
Hello1: There1
Hello2: There2
.
.
.
Hello25: There25

But the only output from this program is -

Hello25: There25

Can somebody help me achieving the required output. The re-requisite is to have key and value both as char* data.

Thanks in advance.
Shubhendu

It has to be char*? Is this even possible? The reason you only get one value is because you're not actually inserting Hello1 Hello2 Hello3 There1 There2 There3, what you're adding is the memory address of "name" and the memory address of "val", since they're pointers. And the memory address of those does not change. So you're inserting the same memory addresses over and over, and they're identical so it just overwrites those entries and you end up with 1 map entry.

You should use strings instead. But if you must use char*...well... you could put it in a struct first and then make a map of struct to struct.

It has to be char*? Is this even possible? The reason you only get one value is because you're not actually inserting Hello1 Hello2 Hello3 There1 There2 There3, what you're adding is the memory address of "name" and the memory address of "val", since they're pointers. And the memory address of those does not change. So you're inserting the same memory addresses over and over, and they're identical so it just overwrites those entries and you end up with 1 map entry.

You should use strings instead. But if you must use char*...well... you could put it in a struct first and then make a map of struct to struct.

nevermind that previous part about having to use strings or structs...you could just make a whole buncha pointers, via a 2d array... I just tested it so it should work:

#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char*, char*> m;
int i;
char name[26][64];
char val[26][64];

for(i=0; i<26; i++) {
sprintf(name[i], "Hello%d",i);
sprintf(val[i], "There%d",i);
cout << name << " " << val;
m.insert(pair<char*, char*>(name[i], val[i]));
}
cout << endl << endl;
for(map<char*, char*>::iterator p = m.begin(); p!= m.end(); ++p) {
cout << p->first << ": " << p->second << endl;
}

return 0;
}

1. use typedefs to simlify your code
2. use strings rather than char* for key in containers.
3. if you must use char *, custom comparison perdicate must be provided

Below is simple sample

#include <iostream>
#include <string>
#include <map>

int main()
{
    typedef std::map<std::string,std::string> TMap;

    TMap m;
    m["k2"] = "v2";
    m["k3"] = "v3";
    m["k1"] = "v1";

    TMap::iterator iter;
    for(iter=m.begin(); iter != m.end(); ++iter)
    {
        std::cout << "key=" << iter->first << " value=" << iter->second << std::endl;
    }
    return 0;

}

Thanks for you support.
The above code works fine and solves my issue.

Thanks a lot!

Shubhendu

Hi,

The same program I am trying to port to AIX 6.1. The compiler used here is xlc.
But the same program gives the below compilation errors -

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"testmap11.cpp", line 18.22: 1540-0218 (S) The call does not match any parameter list for "operator<<".
"/usr/vacpp/include/ostream.t", line 266.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(_Mysb *)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "std::basic_streambuf<char,std::char_traits<char> > *" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 250.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(const void *)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "const void *" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 234.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(long double)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "long double" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 218.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(double)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "double" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 202.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(float)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "float" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 185.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(unsigned long long)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "unsigned long long" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 169.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(long long)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "long long" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 152.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(unsigned long)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "unsigned long" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 135.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(long)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "long" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 119.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(unsigned int)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "unsigned int" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 97.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(int)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "int" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 80.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(unsigned short)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "unsigned short" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 58.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(short)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "short" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 41.1: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(_Bool)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "bool" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream", line 155.15: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(ios_base &(*)(ios_base &))" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "std::ios_base &(*)(ios_base &)" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream", line 147.15: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(_Myios &(*)(_Myios &))" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "std::basic_ios<char,std::char_traits<char> > &(*)(basic_ios<char,std::char_traits<char> > &)" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream", line 145.15: 1540-1283 (I) "std::basic_ostream<char,std::char_traits<char> >::operator<<(_Myt &(*)(_Myt &))" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "std::basic_ostream<char,std::char_traits<char> > &(*)(basic_ostream<char,std::char_traits<char> > &)" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 327.25: 1540-1283 (I) "template <class _E, class _Tr> std::operator<<(basic_ostream<_E,_Tr> &, const char *)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "const char *" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 372.25: 1540-1283 (I) "template <class _E, class _Tr> std::operator<<(basic_ostream<_E,_Tr> &, char)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "char" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 400.27: 1540-1283 (I) "template <class _Tr> std::operator<<(basic_ostream<char,_Tr> &, const char *)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "const char *" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 446.27: 1540-1283 (I) "template <class _Tr> std::operator<<(basic_ostream<char,_Tr> &, char)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "char" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 475.25: 1540-1283 (I) "template <class _E, class _Tr> std::operator<<(basic_ostream<_E,_Tr> &, const _E *)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0288 (I) The function template parameter of type "const _E *" cannot be initialized with an argument of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream.t", line 520.25: 1540-1283 (I) "template <class _E, class _Tr> std::operator<<(basic_ostream<_E,_Tr> &, _E)" is not a viable candidate.
"testmap11.cpp", line 18.22: 1540-0290 (I) The function template parameter "_E" has been found to have two values: "char" and "std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream", line 230.35: 1540-1283 (I) "template <class _Tr> std::operator<<(basic_ostream<char,_Tr> &, const signed char *)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "const signed char *" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream", line 234.35: 1540-1283 (I) "template <class _Tr> std::operator<<(basic_ostream<char,_Tr> &, const signed char)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "const signed char" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream", line 238.35: 1540-1283 (I) "template <class _Tr> std::operator<<(basic_ostream<char,_Tr> &, const unsigned char *)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "const unsigned char *" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/ostream", line 242.35: 1540-1283 (I) "template <class _Tr> std::operator<<(basic_ostream<char,_Tr> &, const unsigned char)" is not a viable candidate.
"testmap11.cpp", line 18.26: 1540-0256 (I) A parameter of type "const unsigned char" cannot be initialized with an expression of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Please suggest me how to go for the same.

Thanks in advance!
Shubhendu

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.