gpt4 book ai didi

c++ - 翻译 C++ 片段

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:25:07 24 4
gpt4 key购买 nike

我正在努力将一个框架从 C++ 移植到 Java,结果比我预期的要难,因为我对 C++ 了解不多。我遇到了这个我不太明白的片段。如果有人能告诉我标记的行是做什么的,那就太棒了。

  /** Heap data, stored as a vector */
std::vector< std::pair< _Tp, _Val > > data;

/** Maps objects to their positions in the data vector */
std::map< _Tp, int> mapping;


//I understand that this method takes a pair of type <_Tp, _Val>
template <class _Tp, class _Val>
void Heap<_Tp,_Val>::push(std::pair< _Tp, _Val > x)
{
int index=data.size();

//Here is where I run into trouble
//I can't seem to figure out what this line is doing
//I know it is inserting a Key-Value pair into the map
//but why is .second being called? and what exactly is this if statement
//checking?
if (mapping.insert(std::make_pair(x.first,index)).second)
{
data.push_back(x);
percolate_up(index);
}
}

最佳答案

The insert member function returns a pair whose bool component returns true if an insertion was made and false if the map already contained an element whose key had an equivalent value in the ordering, and whose iterator component returns the address where a new element was inserted or where the element was already located.

因此该代码将一个元素添加到 map,如果该元素不存在,它会将数据推送到 vector

关于c++ - 翻译 C++ 片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7605056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com