Specific C++ notation for function arguments? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
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).

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.

Related

Function Pointer Initialization in C/C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
As I was going through a ESP IDF's documentation; I saw that a function pointer was initialized in a certain way that does not make sense to me.
typedef void *app_driver_handle_t;
app_driver_handle_t app_driver_light_init();
app_driver_handle_t app_driver_button_init();
Etc.
I thought that in order to initialize a function pointer, you must do it the following way:
app_driver_handle_t = app_driver_button_init();
Sorry for my beginner level questions.
It would do wonders if someone could explain this.
Thanks
Let's break down the code you're looking at.
typedef void *app_driver_handle_t;
This is not a function pointer. This is a void pointer, which means it can point to basically any values. And this is a type, not a value, so app_driver_handle_t does not actually contain any pointers at all; it's merely a name that's synonymous with void*.
app_driver_handle_t = app_driver_button_init();
Given the typedef above, this syntax is never valid. You're setting a type equal to what is presumably a function call. You can't assign to types. Full stop.
What you can do is declare variables and assign them the result of function calls.
app_driver_handle_t my_variable;
my_variable = app_driver_button_init();
or you can do it in one line.
app_driver_handle_t my_variable = app_driver_button_init();
Finally, these last two lines.
app_driver_handle_t app_driver_light_init();
app_driver_handle_t app_driver_button_init();
These are also not function pointers. These are function prototypes. They're a promise to the compiler, saying, "I will eventually define two functions called app_driver_light_init and app_driver_button_init. These two functions will take no arguments and will return a void*". There's still no function pointer happening here. The functions, when they're eventually defined, will return a void*, which, again, is not a function pointer but a pointer to void.
This is effectively same as
void * app_driver_light_init();
void * app_driver_button_init();
These are not function pointers, they are functions that return void*.
A typedef'd fucntion pointer would look like this:
typedef void* (*app_driver_light_init)();

Is there a way that one function can use the variables of another function without any argument passing in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If I have 2 functions like follows:
template <typename T>
void A(int n)
{
T a[n]; // need to use this array of variables in function B without any passing
// assignment of values to a[n] variables and rest of code for function A
}
void B()
{
// need to use a[n] array here without passing
}
Can the static array T a[n] be used in function B somehow without passing the arguments to B explicitly?
Is it called function forwarding or perfect forwarding in C++?
I'm aware that creating attempting to create a static array out of a variable int isn't standard C++ and some compilers support it. My main question is can the arguments be used in B without passing?
No. Functions have a scope, names of automatic variables are inaccessible outside of that scope, and other functions are outside of that scope.
Is it called function forwarding or perfect forwarding in C++?
No, what you're asking about is not related to forwarding. Forwarding is passing of arguments which is the opposite of what you are asking for.

Return a functor based on command line input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
From command line input I am receiving a single char of '=','<', or '>' followed by a value and my goal is to iterate through and modify my underlying data container using a custom functor (for example using remove_if). The simple but slow solution would be to have a single functor that is constructed using the input char and switching between the operators within it. However, this requires a check on the operator type every time the functor's operator is called. My first solution was using a switch statement on the char and creating a base functor class with 3 derived classes. However, I realized that while this was a completely viable and fast solution, I ended up having a large amount of code duplication of the switch statements. My attempt at a solution to this was using polymorphism and having a function that accepted the input char and returned a pointer of the base class which had a virtual () operator. However, when I passed the dereferenced base class pointer, it would not accept a pure virtual() operator and it was not overriden by the operators of the derived classes. In a sense, I want a function that returns a particular lambda or comparator assosciated with the value of an input char. What would be a good way of going about this?
Since lambdas have there own type i am not aware of a way to construct a function that returns different lambdas. You could store them in std:: function and pay for the overhead.
I think a combination of function ptr and map might be a good solution. Consider using a constexpr map (like in https://youtu.be/INn3xa4pMfg), create your functions and store the mapping in that map.
Note that you can also convert a non capturing lambda to a funtion ptr using ClosureType::operator ret(*)(params)() (See https://en.cppreference.com/w/cpp/language/lambda).

New to C++, having trouble understanding nuances of the language [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've been coding in swift and python for a while, but I've been wanting to learn C++ and have been doing these daily challenges. This challenge in specific requires you to take a string, and then split each letter into an element for an array. I can do that fairly easily, but the language seems to have gotten the better of me.
#include <string>
#include <vector>
std::vector<std::string> solution(const std::string &s)
{
return {}; // Your code here
}
I am super confused by that third line. I have no idea where I'm supposed to take the input from and what means what. If someone could break down that entire third line for me that would be hugely appreciated. Thank you a ton in advance.
The third line is std::vector<std::string> solution(const std::string &s). It declares a function in C++. The declaration has three parts: return type, function name and parameters name and type. So std::vector<std::string> is the return type, solution is the function name and (const std::string &s) is the paramer
list.
First of all, std is a namespace in C++. A namespace can isolate functions so even if two function signatures are the same, as long as they live in different namespaces they can be distinguished as two functions. To use something ins a namespace :: operator will help. std::vector is a container type in C++. It is a growable array, much like NSArray in swift. std::vector is actually a C++ template which adds generics to the language (but can do a lot more).<std::string> is the type parameter, which is used to tell the compiler what type of objects will be stored in the vector.
Then in the parameter list, const is the abbreviation for constant, which means you cannot modify the content of the parameter in the function. std::string denotes the type of the parameter. Unlike swift, C++ puts the type of a variable before it but not after it with a colon. and lastly &s is the name of the parameter. Here & means a reference. Since C++ passes values instead of references to the function by default, to reduce the runtime overhead in passing a large object many parameters will be denoted as to pass a reference to it.
These should be the basic knowledge to write a C++ program, so I recommend you to first read some books about C++ to learn more on this
It's a function that returns vector of strings and takes const reference to the string value. Take a look at lvalue and rvalue, what does const means and vector container (it takes template parameter).

Few questions about initialization and lambda in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I used clang3.3 with netbeans on linux. All in C++11. And I have a question about initialization
int main()
{
int i();
}
The following code is compiled but not work properly. This value will not defined by debugger and cannot be printable. I wanted describe int with default value. Instead I can write "int{}" and it will be a perfect default initialization. But I want understand what I wrote here, just want.
Second question. Its about lambda. I want to know how lamda can be described without auto keyword.
auto lambda = [&]()mutable->int{};
Simple, what I can write here instead auto and compiler will not give me an error ? I just want understand.
Ad 1.
You've been bitten by the most vexing parse. Basically, C++ grammar causes ambiguities between statements and declarations in certain cases. In such cases, the input is interpreted as a declaration. Since int i() can be interpreted as an integer variable definition, or a function declaration, it is interpreted as a declaration of parameterless function i, returning int.
Ad 2.
As for the second question, C++11 Standard §5.1.2/3 says it all:
The type of the lambda-expression (...) is a unique, unnamed non-union class type — called the closure type (...)
So, there is no way to refer to it other than using auto.
Thats not a variable default initialization, its a function declaration, thanks to most-vexing-parse.
In a few words, the standard says if an expression can be evaluated as a function declaration, or as something else, it will be evaluated as a function declaration.
In your case, a function a without parameters and int as return value.