c++ – How to make Vector/Array of integers indexed by strings?

Question:

How do I make a vector/array of integers with strings as an index?

Something that in Lua would be:

vetor = {
    ["eu"] = 10,
    ["voce"] = 11,
}

ps: It will be dynamic, so it will not have a fixed size.
ps²: Struct or enum won't do.

Answer:

Possibly a PHP addiction, right, I've been through it, I think the closest solution to this is std:map

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

int main()
{
    std::map <std::string, int> data{
     { "Maria", 10 },
     { "Joao", 15 },
     { "Anabelo", 34 },
     { "Fulaninho", 22 },
    };

    for (const auto& element : data)
    {
        std::cout << "Chave(key = first): " << element.first;
        std::cout << "Valor(value = second): " << element.second << '\n';
    }

    return 0;
}

Scroll to Top