C++ Tutorial 13 : Advanced Functions

preview_player
Показать описание

MY UDEMY COURSES ARE 87.5% OFF TIL February 13th ($9.99) One is FREE

In this tutorial we explore advanced uses of functions using C++. We'll cover how to store functions as variables, pass functions into other functions, store functions in a vector and more. Then we'll solve 2 problems. We'll receive lists of numbers and then return a list of only the odd values. We'll then generate random lists of heads and tails and add up how often each occurs.
Рекомендации по теме
Комментарии
Автор

In order to be able to pass function as a parameter I had to #include <functional>

BullPavl
Автор

bool IsItOdd(int num) {
return num & 1;
}

ironclownfish
Автор

@13:50 could just subtract 100 by H/T to reduce total instructions

LoganCTanner
Автор

a more 'std' way to generate a vector of heads/tails (or any vector of random numbers):

std::mt19937 rng(std::random_device{}());
d(0, 1);
std::generate_n(std::back_inserter(your_vector), N, [&]() { return d(rng); });

where N is the size of the vector

paolodepetris
Автор

Hi Derek! make tutorial for BFS, DFS, Minimum Spanning Tree. Prim's Algorithm, Bellman-Ford Algorithm and such other algorithms, pls

jakekudur
Автор

Hello Derek, great tutorial series! I just have a question. When you have a function returning a boolean, why do you write, for example:
if(num % 2 == 0){
return true;
}else{
return false;
}

Instead of just writing:
return (num % 2 == 0);

SuperSlugger
Автор

I get this error : main.cpp:95:17: error: 'function' is not a member of 'std'
If I use include<functinal> it works but I still get the Configure hints - Unresolved identifier in the code. And it is annoying.
And I do not understand how is it working for you without include<functinal>. I will try to find out more about this error.

emanuelkokovics
Автор

You have no idea. how much you have helped me.

worldshaper
Автор

Derek sir i want to ask something...that if i want to become specialist in website....then which things i should learn firstly so that i can manage my i need to know about coding and programming for this purpose??

sushiljolly
Автор

Aren’t you suppose to have the functions prototype declared at the top before using them or is it optional

BugattiVeyron
Автор

Hey Derek, I have to #include <functional> to use std::function, why don't you have to?

mcmiloy
Автор

I'm making a multiplayer turn based game using Firebase kind of like Tic Tac Toe. I got two problems, first is creating a connection between two players (I've already done it but it's not that good) and security issue.
Can you give me any idea about the establishing connection?

hamzaliaqat
Автор

10:15 hello, Derek. I have a question. How can you return that vector whilst it is allocated in auto memory?

Chastor
Автор

Thank you, awesome video! I'm not even two minutes in and I've already figured out how to solve a problem that has been my brick wall for weeks.

gunnaryoung
Автор

3:57 Let's see if a human can understand this, because probably not. Okay, I guess we start with the main function. You define a variable called times2. Why did you use the auto keyword? Local variables are auto, by default. So, in order to get the value of times2 we need the value of MultBy2. But to get that, we have to pass an argument into that function, and where did you do that?

tomjoad
Автор

I found it clear this way, since the we are doing basically filtering.
```
bool isOdd(int num) {
return num % 2 == 1;
}

std::vector<int> changeList(std::vector<int> numList, std::function<bool(int)> func) {
std::vector<int> result;
std::copy_if(numList.begin(), numList.end(),
std::back_inserter(result), func);
return result;
}
```

DivusMeta
Автор

Good stuff man thaks a lot... Hi from Venezuela

davidmelendez
Автор

I found a cooler way to solve the first challenge. I like C and pointers so this is how I went about it:

std::vector<int> ChangeList(std::vector<int> listOfNums, bool(*IsItOdd)(int))
{
std::vector<int> listOfOdds;
for(auto num: listOfNums)
if(IsItOdd(num))
listOfOdds.push_back(num);


return listOfOdds;
}
bool IsItOdd(int num)
{
return (num % 2 != 0);
}




std::vector<int> oddList = ChangeList(listOfNums, IsItOdd);

romuloromero
Автор

Hello. When I run the second program at 3:42, it gives 19 errors. I use Visual Studio 2017 to run the code. Why do your code work but mine not working while both are the same? Thank you.

particle
Автор

ChangeList could be written using auto as follows :-


auto changeList(std::vector<int>& v, std :: function<bool(int)>) -> typename
{...}
Though I prefer what is being showed here....

chiragpanchalreadytorock
join shbcf.ru