0

I want to use the c++ standard map to map from a string key to another object (integer in example), but it seems as though c++ uses the pointer as the key, rather than the value of the char*:

#include <iostream>
#include <map>
#include <string.h>

using namespace std;

int main()
{
    std::map<char *, int> m;

    const char *j = "key";
    m.insert(std::make_pair((char *)j, 5));

    char *l = (char *)malloc(strlen(j));
    strcpy(l, j);

    printf("%s\n", "key");
    printf("%s\n", j);
    printf("%s\n", l);

    // Check if key in map -> 0 if it is, 1 if it's not
    printf("%d\n", m.find((char *)"key") == m.end());
    printf("%d\n", m.find((char *)j) == m.end());
    printf("%d\n", m.find((char *)l) == m.end());
}

Output:

key
key
key
0
0
1

Is there any way that I can make the key of the map the "value"/content of the key, similar to other languages like JavaScript?

Kavfixnel
  • 151
  • 3
  • 10
  • 2
    `std::map m;` ?? – John3136 Mar 09 '21 at 05:05
  • Either use `std::string` as the key or declare a custom string comparison predicate and include that in your map template. Note that if you do the latter, you must absolutely ensure that your manage your string pointers correctly. I should point out that all your const-casting to get around the non-const `char*` key value is a strong indication that your design is flawed and you are using hacks to circumvent valid compilation errors. – paddy Mar 09 '21 at 05:05
  • why do you do this stuff?! R U aware of `std::string`? – asmmo Mar 09 '21 at 05:07
  • Have a look at https://stackoverflow.com/a/25123055/2193968 – Jerry Jeremiah Mar 09 '21 at 05:07
  • FWIW, here is your example modified to use a custom comparator for C-strings: https://ideone.com/JeHTYX -- This is almost certainly to be avoided unless you really _know_ you need it (_i.e._ you have progressed far beyond being a beginner or even intermediate programmer). One valid reason why one _might_ choose this kind of approach might be if your program contains large string resources in a buffer, in the data segment or whatever, and you want to build a map at runtime without copying the strings. – paddy Mar 09 '21 at 05:18

1 Answers1

1

In C++ you really want to use std::string to represent a character-string, rather than doing strings the old/C-style char * way. Here's what your program looks like when done using std::string:

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

using namespace std;

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

    const char *j = "key";
    m.insert(std::make_pair(j, 5));

    std::string l = j;

    printf("%s\n", "key");
    printf("%s\n", j);
    printf("%s\n", l.c_str());

    // Check if key in map -> 0 if it is, 1 if it's not
    printf("%d\n", m.find("key") == m.end());
    printf("%d\n", m.find(j) == m.end());
    printf("%d\n", m.find(l) == m.end());
}
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • why do u use `printf` instead of `std::cout` or `std::format`? – asmmo Mar 09 '21 at 05:11
  • 1
    I like `printf()`, and also I wanted to demonstrate the use of `c_str()` to interface between a `std::string` and a function that expects a `const char *`. Also I didn't want to change the questioner's program more than was necessary to demonstrate what I wanted to demonstrate. – Jeremy Friesner Mar 09 '21 at 05:12