map in C works?

0

I wonder if it is possible to use map in C. I know it works in C ++, but in C it always gives compilation error. If someone has already used map in C, could you show me how to use it?

#include <map>
#include <stdio.h>

int main()
{
    map<int, char[10]> m;

    m[1] = "Um";
    m[2] = "Dois";
    m[4] = "quatro";

   printf(m[4]); // não sei se precisa de mascara de dados
}
    
asked by anonymous 04.05.2015 / 14:27

3 answers

3

Does not work in C language. The map is a C ++ language resource ( details here ), a instead of using resources that are not found in C and are characteristic of object orientation.

What you can do is find some library that performs a similar function in C.

    
04.05.2015 / 17:57
0

Some maps work perfectly with an array! In relation to your example,

#include <stdio.h>
    int main(){
    char *  m[20];

    m[1] = "Um";
    m[2] = "Dois";
    m[4] = "quatro";

   printf("%s\n", m[4]);
}

works perfectly.

If your indexes are more complex (example strings, etc.) you can use Map implanted with:

  • hash tables (see man hsearch )
  • Search binary trees (see man tsearch )
  • array of pairs (see man lsearch )

go to richer libraries like

  • glib (see google glib)
  • lib db (for map in file,)

or until you go to C ++ (boost, etc)

    
04.05.2015 / 19:01
0

As already suggested, it is best to implement your map structure in this case. I can recommend using hash table and use dessa lib for C.

Take a look at in this article that explains about hash table .

    
04.05.2015 / 22:15