帮助绘制地图 C++

发布于 2024-12-02 09:27:22 字数 342 浏览 0 评论 0原文

我有一个映射,将 size_t 链接到一对 size_t 和 int

std::map<; size_type, std::pair; > 本质上我对此的

理解是,映射的工作方式类似于堆栈,并且 jst 一个接一个地插入,给出类似于:

1 -> (2,2)
2 -> (4,7)
3 -> (8,5)
etc.

我的问题是,如何获取 int 的值。即2,7,5。我想使用这个值作为 while 循环的最大大小。

I have a map that links a size_t to a pair, of size_t and int

std::map< size_type, std::pair<size_t, unsigned int> > mapVals;

essentially my understanding of this, is that maps work similar to stacks, and jst insert one after another, giving something similar to:

1 -> (2,2)
2 -> (4,7)
3 -> (8,5)
etc.

my question is, how do I obtain the value of the int. i.e. 2,7,5. I want to use this value as the max size of a while loop.

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

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

发布评论

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

评论(5

属性 2024-12-09 09:27:22

映射与堆栈不同;堆栈维持先进后出(FILO)策略。映射是将键映射到值的东西。

如果您执行以下操作:

typedef std::pair<size_t,unsigned_int> my_pair;

// Insert elements
my_map[3] = my_pair(2,2);
my_map[9] = my_pair(4,7);
my_map[7] = my_pair(8,5);

您可以检索对的第二个元素,如下所示:

my_map[9].second  // evaluates to 7

Maps are not similar to stacks; a stack maintains a first-in-last-out (FILO) strategy. A map is something that maps a key to a value.

If you do something like:

typedef std::pair<size_t,unsigned_int> my_pair;

// Insert elements
my_map[3] = my_pair(2,2);
my_map[9] = my_pair(4,7);
my_map[7] = my_pair(8,5);

You can retrieve the second element of your pair as:

my_map[9].second  // evaluates to 7
清风疏影 2024-12-09 09:27:22

要访问 int,您可以执行以下两种操作之一

unsigned int myint = mymap[key].second;

,其中 key 的类型为 size_t。这是有效的,因为在地图上使用 [size_t] 返回一个 std::pair 然后在此调用 .second你是单位。

您还可以使用迭代器

std::map<size_t, std::pair<size_t, unsigned int> >::iterator itr = mymap.begin(); // say
unsigned int myint = itr->second.second;

To access the int you can do one of two things

unsigned int myint = mymap[key].second;

where key is of type size_t. This works because using [size_t] on the map returns a std::pair<size_t, unsigned int> then calling .second on this gets you the uint.

You could also use iterators

std::map<size_t, std::pair<size_t, unsigned int> >::iterator itr = mymap.begin(); // say
unsigned int myint = itr->second.second;
转瞬即逝 2024-12-09 09:27:22
typedef std::map< size_type, std::pair<size_t, unsigned int> > mymapT;
mymapT mapVals;

... // fill the map

first = mapVals[1].second;
second = mapVals[2].second;
third = mapVals[3].second;

... // do something useful
typedef std::map< size_type, std::pair<size_t, unsigned int> > mymapT;
mymapT mapVals;

... // fill the map

first = mapVals[1].second;
second = mapVals[2].second;
third = mapVals[3].second;

... // do something useful
腹黑女流氓 2024-12-09 09:27:22

您可以执行以下操作:

typedef std::map< size_type, std::pair<size_t, unsigned int> > myMap;
myMap mapVals;
// ... populate
myVals[1] = std::pair<size_t, unsigned int>(2,2);
// ...
for (myMap::const_iterator it = myVals.begin(); it != myVals.end(); ++it)
    unsigned int each_value = it->second.second;

第一个 it->second 将为您提供 std::pair 元素。第二个 second 将为您提供该对中包含的 unsigned int

You can do something like this:

typedef std::map< size_type, std::pair<size_t, unsigned int> > myMap;
myMap mapVals;
// ... populate
myVals[1] = std::pair<size_t, unsigned int>(2,2);
// ...
for (myMap::const_iterator it = myVals.begin(); it != myVals.end(); ++it)
    unsigned int each_value = it->second.second;

The first it->second will give you the std::pair <size_t, unsigned int> element. The second second will give you the unsigned int contained in that pair.

谁许谁一生繁华 2024-12-09 09:27:22

我不确定您到底想要什么

 for (std::map< size_type, std::pair<size_t, unsigned int> >::iterator it = mapVals.begin(); it != mapVals.end() ; it++)
     cout << it->second.first << " " << it->second.second << endl;
  • it->second :是地图的 Value 部分(在本例中为 std::pair
  • it->second 。 Second :是该对的第二部分(无符号整数)

I'm not sure what you want exactly

 for (std::map< size_type, std::pair<size_t, unsigned int> >::iterator it = mapVals.begin(); it != mapVals.end() ; it++)
     cout << it->second.first << " " << it->second.second << endl;
  • it->second : is the Value part of the map (in this case std::pair<size_t, unsigned int>)
  • it->second.second : is the second part of the pair (unsigned int)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文