-3

This is a simple question, but I couldn't find the answer through searching online. I was trying to work through some leetcode problems to better my understanding of C++. I was wondering if someone could walk me through the meaning behind the creation of this function.

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        
    }
};

I understand that public is used so that we can access ____ outside the Solution class, but I am not sure what exactly... it also looks like we are initializing a vector of integers named "twoSum" with the arguments of a vector of numbers and a target value... I was wondering what the meaning of the & is... etc. I guess a simple question would be can someone translate this block of code so that I can write my own versions for various problems (it seems like this is a constant block (or variation of a similar block) of code that is common throughout these leetcode problems).

  • 6
    *I was trying to work through some leetcode problems to better my understanding of C++.* Usually the wrong thing to do. Competition code is written to win the competition, not to be readable or even moderately educational. Half the job at Stack Overflow seems to be fixing the self-inflicted brain damage caused by trying to learn to program from competition code. – user4581301 Apr 12 '22 at 00:23
  • The best resource for improving one's "understanding of C++" is a [good C++ textbook](https://stackoverflow.com/questions/388242/). Unfortunately, Stackoverflow is not a replacement for a textbook and we are not a C++ tutorial site, we only answer ***specific*** programming questions. C++ is the most complicated and the hardest to learn programming language in the world today. It cannot be effectively learned by solving random coding puzzles, or from reading the results of a search engine, but only after investing significant time with a good textbook. – Sam Varshavchik Apr 12 '22 at 00:23
  • 1
    You should have a look at some good C++ learning materials as this is very standard c++ code. It declares a member function that has a return type of `vector` and takes two arguments. The first is `vector&` which is an lvalue reference to a `vector` and an integer. Going into what those are and how the syntax works is the job of a good tutorial/book/any other educational resource – Lala5th Apr 12 '22 at 00:24
  • @SamVarshavchik He did ask a very specific programming questions. `I was wondering what the meaning of the & is` – Martin York Apr 12 '22 at 00:45
  • @MartinYork You missed the "etc" and "can someone translate this block of code". – Passer By Apr 12 '22 at 06:21

1 Answers1

1

The shown code snippet, defines a member function named twoSum that has the return type of vector<int> and has 2 parameters. The first parameter named nums is an lvalue reference to a non-const vector<int> while the second parameter named target is an int.

I was wondering what the meaning of the & is

The & in the first parameter nums of the member function means that nums is an lvalue reference to a non-const vector<int>. Meaning, the argument that will be passed to the nums parameter, will be passed by reference instead of passed by value. That is, inside the member function twoSums, the nums refer to the original vector<int> that is passed as an argument.

Also note that there should be a return statement inside the member function since the return type of the member function is non-void.

Jason
  • 36,170
  • 5
  • 26
  • 60