Can dictionaries be used in c++

2020-08-09 10:05发布

I have been looking up dictionaries in C# and they seem to be highly useful, and was wondering if it is possible to use them in C++ as I have tried to search for dictionaries in C++ but there doesn't seem to be an equivalent that I can find. Is there some sort of library that I could download and include to the project or is there a function which does the same thing just with a different name.

4条回答
Summer. ? 凉城
2楼-- · 2020-08-09 10:48

There is a corresponding type in STL, that's called std::map.

It has the same basic functionality as a .NET Dictionary, but the implementation is quite different. std::map is internally based on a red-black tree datastructure, while Dictionary uses a hash table internally.

If you're just looking for something with the same behaviour, std::map will do, but if you have large amounts of data you have to be aware of the different performance characteristics.

查看更多
放我归山
3楼-- · 2020-08-09 10:58

std::map is like a Dictionary.

查看更多
做自己的国王
4楼-- · 2020-08-09 10:59

We can use map in C++ The basic format of using a map is -

    std::map<Key_type, Value_Type> ;

Example -

    std::map<std::string,std::string> x = {{"A","ABC"},{"B","DEF"}}
查看更多
相关推荐>>
5楼-- · 2020-08-09 11:00

There's std::map for logarithmic access time (usually based on a tree implementation) and std::unordered_map (since C++11) for expected constant, worst-case linear access time (usually based on a hashing implementation).

查看更多
登录 后发表回答