地图对象在方法调用期间被复制

Map object is copied during method call

对于功能:

void Function(int* integer, map<unsigned short, unsigned short> firstMap, map<int, unsigned char> secondMap);

每当我像这样从另一个 class 调用它时...

Object->Function(integer, firstMap, secondMap);

呼叫

std::_Tree<std::_Tmap_traits<int,unsigned char,std::less<int>,std::allocator<std::pair<int const ,unsigned char> >,0> >::_Tree<std::_Tmap_traits<int,unsigned char,std::less<int>,std::allocator<std::pair<int const ,unsigned char> >,0> > 

进入 xtree 调用的方法是:

_Tree(const _Myt& _Right)
    : _Mybase(_Right.key_comp(), _Right.get_allocator())
    {   // construct tree by copying _Right
    _Init();
    _TRY_BEGIN
    _Copy(_Right);
    _CATCH_ALL
    _Tidy();
    _RERAISE;
    _CATCH_END
    }

这是在做什么?为什么叫它?是否正在复制地图?使用 very sleepy 我发现我的循环时间的 20% 被这个调用用完了。

我认为您可能希望通过引用而不是值来传递地图

void Function(int* integer, map<unsigned short, unsigned short>& firstMap, map<int, unsigned char>& secondMap);

注意添加的“&”。

您可能感兴趣的 C++11 的另一个特性是 moving in place(如果变量只是临时使用的话)。