我最近写了类似下面的内容.但是,我很困惑它设法编译.那么现在它确实如此,我有一些问题......
void tree_walk(const std::pair<tree, node> &tree_root)
{
tree t = tree_root.first;
node current = tree_root.second;
// code which walks the tree, updating current as we go along.
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否调用tree.first复制树?传递std :: pair作为引用的语义是什么?
tree_rooting.first 不复制任何东西,但是
tree t = tree_root.first;
Run Code Online (Sandbox Code Playgroud)
确实.如果您不想复制树,则可以执行此操作
const tree &t = tree_root.first;
Run Code Online (Sandbox Code Playgroud)
回答你的第二个问题:你正确pair地将函数作为const参考传递给函数.如果要修改输入,只需省略const函数签名以传递非const引用.