0
I would like to know if it is possible to use map
in C. I know it works in C++, but in C it always gives build error. If someone has already used map in C, it could show me how to use?
#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
}
The map type does not exist in C, only in C++. What you could do is create your own type
map
.– Felipe Avelar
The way you are I don’t think even in C++ will work, you need to write for example
using namespace std;
, and the form of insertion is wrong, should be using theinsert()
.– Math
Vlw Galera, already suspected that it did not work in C. I think map works only in object-oriented languages, I used in java but had never tested in C. Thanks for answering.
– Paulo
I don’t think you need to reinvent the wheel. Similar solutions already exist in C. See: http://stackoverflow.com/questions/647054/porting-stdmap-to-c
– Luiz Vieira
These stackoverflow people in the English version invent each complex implementation rs, using struct, pointer, memory allocation, function receiving pointer as parameter. But it’s worth it I didn’t know you could create a map this way in C.
– Paulo
Paul, for examples like the one you included in the question, a array in C (which is a very particular case of map ), works perfectly...
– JJoao
@Jjoao, I took a look at the documentation of C. And array only works in C++.
– Paulo