Passing to a Reference Argument by Value

前端 未结 7 920
名媛妹妹
名媛妹妹 2021-01-17 14:40

Consider this simple program:

vector foo = {0, 42, 0, 42, 0, 42};
replace(begin(foo), end(foo), foo.front(), 13);

for(const auto& i : foo) co         


        
相关标签:
7条回答
  • 2021-01-17 15:35

    One liner that should work for any type, not only numeric:

    replace(begin(foo), end(foo), make_pair(foo.front(),0).first, 13);
    

    or without creating extra field:

    replace(begin(foo), end(foo), get<0>( make_tuple(foo.front()) ), 13);
    
    0 讨论(0)
提交回复
热议问题