function-definition

Python3 function definition, arrow and colon [duplicate]

不羁岁月 提交于 2021-02-19 01:59:17
问题 This question already has answers here : What does -> mean in Python function definitions? (8 answers) Closed 2 years ago . I have found the following python function definition: def reverseString(self, s: 'List[str]') -> 'None': I don't quite understand 'List[str]' and -> 'None' . I have found that the arrow is a function annotation but I couldn't find anything useful and understandable for List[str]. Is it just an annotation? or does it enforce that the type of parameter s must be a string

Pointer Confusion: swap method in c

半世苍凉 提交于 2021-02-05 12:29:34
问题 #include<stdio.h> void swap(int *a,int *b){ int p=*b; *b=*a; *a=p; /*int *p=b; b=a; a=p; */ } int main(){ int a,b; scanf("%d %d",&a,&b); swap(&a,&b); printf("%d %d",a,b); } Above is the code. If I put 3 5 as an input, then it should swap its values, and 5 3 should come out as an output. I got my answer by trying int p=*b thing However I also tried commented part, but it didn't work. So, I checked their address in swap and in main. In swap int *a and int *b their address changed However, when

Why are function definitions implicitly external in C?

故事扮演 提交于 2021-01-28 06:05:53
问题 I read that the extern keyword is implicit in the context of functions, so unless you specify otherwise using the static keyword ( which if I'm not mistaken is basically a completely separate concept from the static that variables employ—they just share a keyword ), they are visible to all object files. This makes sense; having the declarations be implicitly external, while technically unnecessary when the declarations are in the same file as the definition, is useful because the programmer

Why are function definitions implicitly external in C?

 ̄綄美尐妖づ 提交于 2021-01-28 05:59:40
问题 I read that the extern keyword is implicit in the context of functions, so unless you specify otherwise using the static keyword ( which if I'm not mistaken is basically a completely separate concept from the static that variables employ—they just share a keyword ), they are visible to all object files. This makes sense; having the declarations be implicitly external, while technically unnecessary when the declarations are in the same file as the definition, is useful because the programmer

Find cardinal number of a list in C

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-27 06:31:39
问题 How can i find only the elements that appears once in the list and return the cardinal number?For example if my list consist of {3,2,1,1,2,4} i expect for return the counter to be 4 and not 6 cause we do not count the duplicate numbers. Here is the code that i have written so far. struct Node { int data; struct Node *next; }; int Find_cardinal(struct Node *start) { struct Node *ptr1, *ptr2 ptr1 = start; int counter=0; /* Pick elements one by one */ while (ptr1 != NULL && ptr1->next != NULL) {

How to pass pointer to function and dynamically allocate memory within function C++

时光怂恿深爱的人放手 提交于 2020-12-05 07:23:53
问题 I'm trying to declare a pointer and pass that pointer to a function where memory is allocated. Here is a minimal example: #include <string> #include <iostream> using namespace std; void alloc_mem(int &size, double *x); int main() { double *X; int imax; alloc_mem(imax, X); cout << "imax = " << imax << endl; for (int i = 0; i < imax; i++) { cout << "X = " << X[i] << endl; } delete[]X; return 0; } void alloc_mem(int &size, double *x) { size = 10; x = new double[size]; for (int i = 0; i < size; i

How to pass pointer to function and dynamically allocate memory within function C++

£可爱£侵袭症+ 提交于 2020-12-05 07:22:20
问题 I'm trying to declare a pointer and pass that pointer to a function where memory is allocated. Here is a minimal example: #include <string> #include <iostream> using namespace std; void alloc_mem(int &size, double *x); int main() { double *X; int imax; alloc_mem(imax, X); cout << "imax = " << imax << endl; for (int i = 0; i < imax; i++) { cout << "X = " << X[i] << endl; } delete[]X; return 0; } void alloc_mem(int &size, double *x) { size = 10; x = new double[size]; for (int i = 0; i < size; i

How to handle recursion in member functions?

天涯浪子 提交于 2020-06-27 18:35:27
问题 E.g., I have an empty function to clear a linked list: void empty(Node* head) { if (head->next) { empty(head->next); } delete head; head = nullptr; } But then I created a class for the linked list, so now I don't need to pass the head argument: void empty() { if (head->next) { empty(head->next); } delete head; head = nullptr; } But empty(head->next) line is obviously wrong as empty doesn't take any arguments. The idea comes to my mind to create a function inside a function (with lambda),

Virtual overloaded operators >> and <<

♀尐吖头ヾ 提交于 2020-06-15 21:22:26
问题 I need an interface that would require its subclasses to overload << and >> , but I'm not quite sure how since these operators aren't overloaded as member functions: std::istream& operator>> (std::istream& in, Student& student) { in >> student.name >> student.group; for (int& i : student.marks) { in >> i; } return in; } Maybe there's a way to make it a member function? 回答1: You could do something like this: class StudentInterface { public: virtual void readSelfFrom(std::istream& in) = 0; };

Should I define my Cython function using def, cdef, or cpdef for optimal performance?

那年仲夏 提交于 2020-01-01 10:53:07
问题 How can I know whether to use def, cdef or cpdef when defining a Cython function, assuming I want optimal performance? 回答1: If you want optimal performance, you should know that as mentioned in this answer to a related question: Once the function has been called there is no difference in the speed that the code inside a cdef and a def function runs at. So for optimal Cython performance you should always statically type all arguments and variables , and intuitively you would then be tempted to