声明 std::map 常量

发布于 2024-10-06 23:18:14 字数 259 浏览 0 评论 0原文

如何声明 std 地图常量,即

int a[10] = { 1, 2, 3 ,4 };
std::map <int, int> MapType[5] = { };
int main()
{
}

在 about 代码片段中,可以将值 1,2,3,4 赋予整数数组 a,类似地如何声明一些常量 MapType > 值而不是在 main() 函数中添加值。

How to declare std map constants i.e.,

int a[10] = { 1, 2, 3 ,4 };
std::map <int, int> MapType[5] = { };
int main()
{
}

In the about snippet it is possible to give values 1,2,3,4 to integer array a, similarly how to declare some constant MapType values instead of adding values inside main() function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

池木 2024-10-13 23:18:14

更新:从 C++11 开始,您可以......

std::map<int, int> my_map{ {1, 5}, {2, 15}, {-3, 17} };

或类似的,其中每对值 - 例如 {1, 5} - 编码一个键 - 1 - 和映射到的值 - 5。这同样适用于 unordered_map (哈希表版本)。


仅使用 C++03 标准例程,请考虑:

#include <iostream>
#include <map>

typedef std::map<int, std::string> Map;

const Map::value_type x[] = { std::make_pair(3, "three"),
                              std::make_pair(6, "six"),
                              std::make_pair(-2, "minus two"), 
                              std::make_pair(4, "four") };

const Map m(x, x + sizeof x / sizeof x[0]);

int main()
{
    // print it out to show it works...
    for (Map::const_iterator i = m.begin();
            i != m.end(); ++i)
        std::cout << i->first << " -> " << i->second << '\n';
}

UPDATE: with C++11 onwards you can...

std::map<int, int> my_map{ {1, 5}, {2, 15}, {-3, 17} };

...or similar, where each pair of values - e.g. {1, 5} - encodes a key - 1 - and a mapped-to value - 5. The same works for unordered_map (a hash table version) too.


Using just the C++03 Standard routines, consider:

#include <iostream>
#include <map>

typedef std::map<int, std::string> Map;

const Map::value_type x[] = { std::make_pair(3, "three"),
                              std::make_pair(6, "six"),
                              std::make_pair(-2, "minus two"), 
                              std::make_pair(4, "four") };

const Map m(x, x + sizeof x / sizeof x[0]);

int main()
{
    // print it out to show it works...
    for (Map::const_iterator i = m.begin();
            i != m.end(); ++i)
        std::cout << i->first << " -> " << i->second << '\n';
}
酸甜透明夹心 2024-10-13 23:18:14

在 C++0x 中,它将是:

map<int, int> m = {{1,2}, {3,4}};

In C++0x, it will be:

map<int, int> m = {{1,2}, {3,4}};
一人独醉 2024-10-13 23:18:14

我被 Boost.Assign 所吸引这个问题的解决方案,我大约一年半前写了一篇关于它的博客文章(就在我放弃写博客之前):

帖子中的相关代码是这样的:

#include <boost/assign/list_of.hpp>
#include <map>

static std::map<int, int>  amap = boost::assign::map_list_of
    (0, 1)
    (1, 1)
    (2, 2)
    (3, 3)
    (4, 5)
    (5, 8);


int f(int x)
{
    return amap[x];
}

I was so taken by the Boost.Assign solution to this problem that I wrote a blog post about it about a year and a half ago (just before I gave up on blogging):

The relevant code from the post was this:

#include <boost/assign/list_of.hpp>
#include <map>

static std::map<int, int>  amap = boost::assign::map_list_of
    (0, 1)
    (1, 1)
    (2, 2)
    (3, 3)
    (4, 5)
    (5, 8);


int f(int x)
{
    return amap[x];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文