开发者

How to set std::tuple element by index?

开发者 https://www.devze.com 2023-04-05 07:16 出处:网络
One can get an element from std::tuple by i开发者_JAVA百科ndex using std::get. Analogically, how to set tuple\'s element by index?std::get returns a reference to the value. So you set the value like t

One can get an element from std::tuple by i开发者_JAVA百科ndex using std::get. Analogically, how to set tuple's element by index?


std::get returns a reference to the value. So you set the value like this:

std::get<0>(myTuple) = newValue;

This of course assumes that myTuple is non-const. You can even move items out of a tuple via std::move, by invoking it on the tuple:

auto movedTo = std::get<0>(std::move(myTuple));


The non-const version of get returns a reference. You can assign to the reference. For example, suppose t is tuple, then: get<0>(t) = 3;

0

精彩评论

暂无评论...
验证码 换一张
取 消