c++ - Passing vector by reference -
using normal c arrays i'd that:
void do_something(int el, int **arr) { *arr[0] = el; // else } now, want replace standard array vector, , achieve same results here:
void do_something(int el, std::vector<int> **arr) { *arr.push_front(el); // function above } but displays "expression must have class type". how properly?
you can pass container reference in order modify in function. other answers haven’t addressed std::vector not have push_front member function. can use insert() member function on vector o(n) insertion:
void do_something(int el, std::vector<int> &arr){ arr.insert(arr.begin(), el); } or use std::deque instead amortised o(1) insertion:
void do_something(int el, std::deque<int> &arr){ arr.push_front(el); }
Comments
Post a Comment