Passing to a Reference Argument by Value

前端 未结 7 933
名媛妹妹
名媛妹妹 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:31

    A solution could be as follows (even though you are making a temporary)

    template
    void replace_value_of_first(std::vector& v, const T& value)
    {
        std::replace(v.begin(), v.end(), T(v.front()), value);
    }
    

提交回复
热议问题