0

How can I add class functions to vectors without going through the std lib source code? So just for example I want to add a function to vectors that shift a vector left by 1. I know I can execute it by myVector = shiftLeft(myVector), but is there a way to execute by myVector.shiftLeft()?

Thanks either way!

  • First of all, make sure that there isn't any [standard](https://en.cppreference.com/w/cpp/algorithm/rotate) [function](https://en.cppreference.com/w/cpp/algorithm/shift) doing what you already want. And if there isn't, then write you own function, preferably modeling similar standard functions. And to answer your question: No, you can't modify any standard container or add to one. You have to rely on non-member functions. – Some programmer dude Apr 04 '21 at 19:42
  • What is the type of `myVector`? Did you write this class? If yes you just need to add a member function into the `class YourVector { ...}`. If no, you cannot add a member functions to an existing class in C++. You can however create a new class for that although this is probably not a good idea here. – Jérôme Richard Apr 04 '21 at 19:43

0 Answers0