Is this the proper convention for a vector accessor and modifier?

Due to std::vectors being of unknown size, this is the proper way to optimize, correct?

1
2
const std::vector<Point>& get_path() const {return path;}
void set_path(const std::vector<Point>& v) {path = v;}
Last edited on
It is fine.

Worth considering:
1
2
3
4
5
6
7
8
9
10
11
const std::vector<Point>& get_path() const {return path;}

// void set_path(const std::vector<Point>& v) {path = v;}

void set_path( std::vector<Point> v ) { using std::swap ; swap( path, v ) ; } // pass by value
// exception safe, and in certain kinds of usage, would be more efficient
// copy-and-swap idiom: https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Copy-and-swap

template < typename ITERATOR> void set_path( ITERATOR begin, ITERATOR end ) { path = {begin,end} ; }
// more flexible; allows setting the path with any sequence of Point objects 
// (for instance, to set the path from data in a file, the type ITERATOR could be std::istream_iterator<Point>) 
Topic archived. No new replies allowed.