I always hear Tim's "also" as " else so" like: If (x) {y();} else {so();} Is that a Canadian thing or something? Thanks again Tim, you're out here changing lives bro! 💪
Thank you :) Quick performance tip: Use range based loops insted of iterators for (const auto &c : test) { freq[letter] = (letter.find(c) == freq.end()) ? 0 : freq[letter] + 1; }
@@gabriellabrito8134 This is a "range based for" loop. In other languages, similar constructs are often called "foreach". It loops over all elements that are actually contained in the variable "test". In the loop body (thatbeing the part between the opening and closing braces) we use the so-called ternary operator to assign the actual frequency to "freq". The ternary operator can be interpreted as a condensed if-else-bock looks as follows: (condition) ? value_if_true : value_if_false Here, the condition is "letter.find(c) == freq.end()", that means, if the letter c (which comes from "test") doesn't already exist as a key in our map, we set it to zero. Otherwise, if it already exists, we increase its frequency by 1
A quick correction to when you said that you can’t expect what the output is. Maps are actually ordered by key. In your case, you’ll see that the values were outputted in the lexicographical order of the char keys. There is a concept of maps that doesn’t care about order called unordered_map.
@iSpiritus I was going to write the same comment, then I found yours. thx for the tip and that doesn't change the fact about this video, it is a very good one.
When counting the number of times a letter occurs, you can just use "++freq[letter]". The index operator [] automatically inserts a new element if it doesn't exist.
I follow you since I have memory, and I love this channel because in a unequal country like Colombia when I am from i can learn with your channel, Colombia government is KILLING us, SOS
For the letter counter example, I don't think the if statement that checks if a letter is in the map is needed. Since the default value for ints in the map is 0, we can just start adding right away.
So people are aware since undefined keys will return a value of 0 in maps you don't actually have to check if the key exists for the practical map example. string test = "Hello world my name is tim! ttthhaaa"; map frequency = {}; for (int i = 0; i < test.length(); ++i) ++frequency[test[i]]; for (auto itr = frequency.begin(); itr != frequency.end(); ++itr) cout first
with the example at the end you don't actualy have to check if it already exists because if it doesnt exist it will equal 0 and (for some reason) if you just say ++ after a nonexistend pair it will make the pair.
I think this happens because the map automatically initializes and adds the pair to itself if you try to access it, the only thing is that its set to its default initialization value 0. The reason as to why it is done this way doesnt really make sense to me, I get returning the default value but it getting added to the map seems like a potential flaw
Great explanation of map! Just what I was looking for. I was curious about how it returns the map.end() if the find method doesn't contain the key one is looking for.. does this mean the map iterates through the entire structure to find the key's pointer? I'm guessing no since that would make it O(n)..
When you say that you aren't sure if you need the parentheses are you just saying you are not sure if it is a function/method (getter) or an actual persisted property/value of the object? (as it would be in other languages) or is there a specific C++ syntax that I am missing here?
I'm not sure about him but when you're learning the basic syntax of a language it's usually a lot quicker to learn by using documentation itself, some languages even give a quick written tutorial in their websites that cover all of it pretty quickly. But again, I'm not sure if it's how Tim does it.
Great lesson Tim...I have a question...can the maps be used in scenarios such as network log monitoring in incident response? I am not a pro in IR but I feel like it is possible when there are millions of logs and minimal time...I appreciate answers from all commenters.
I am trying something like this at the moment (logfile on changes in a file). Sadly map does not like std::tm as a type, so I had to convert that, basicaly making it a map of std::time_t t and std::string report. Works nice just to keep reports, but pulling data out of this is kind of a messy nightmare ^^''' ... And I have no idea how time-efficient that trick is alltogether.
Some projects that are easy to implement in Python and other programming languages are actually difficult to implement in C++. Can you do your Python projects in C++? If you kindly do that, it will help me a lot.
I came to the following solution: int main () { string test = "Hello world my name is tim! ttthhaaa"; map letter_freq; for (int i = 0; i < test.size(); i++) { letter_freq.insert(pair(test[i], letter_freq[test[i]]++)); } for (auto & itr : letter_freq) { cout
This is plain wrong. std::map is not a hashmap, it’s a RB binary tree and it has O(log n) insertion and lookup time complexity. What you are describing is std::unordered_map.
Oh very interesting. This is what I did when you typed out the string and gave the instructions: void MyOutPutTest(){ std::string test = "Hello world my name is time! ttthhaaa"; std::map frequency = {}; for (char const &thisLetter: test){ if (!frequency[thisLetter]){ //create initial value frequency[thisLetter] = 1; } else { frequency[thisLetter]++; } } //output counts std::cout
@@arctan2 first of all both functions gets executed and 1 is returned from first function and 2 is returned from second then + operator is applied from left to right so 1 + 2 = 3
crap video. All you did was iterate through the map. Your technique is similar to 'the 'Count Sort' algorithm. You did not explain how you can tell instantly if a value exists or not WITHOUT iterating through the map. There is, however, another technique that you could use to achieve this.
Well how about you share that technique? I’m always open to criticism and feedback buts it hypocritical to suggest I provided no value when you provide none yourself.
Bro, your face expressions, your voice tone, and much more besides the appearance is much like iRaphahell (search it on youtube) Idk why but you give me same vibes😂
As a teacher (math) who’s trying to learn code, you’re an excellent teacher.
Wow! wonderful explanation of map. And the last coding example is really worth it to understand real life use case of map.
Excellent! So precise and to the point. Thank you.
I always hear Tim's "also" as " else so" like:
If (x) {y();} else {so();}
Is that a Canadian thing or something?
Thanks again Tim, you're out here changing lives bro!
💪
Thank you :) Quick performance tip: Use range based loops insted of iterators
for (const auto &c : test)
{
freq[letter] = (letter.find(c) == freq.end()) ? 0 : freq[letter] + 1;
}
can u explain what did u do there? like, this type of code isnt begginers friendly
@@gabriellabrito8134 This is a "range based for" loop. In other languages, similar constructs are often called "foreach". It loops over all elements that are actually contained in the variable "test". In the loop body (thatbeing the part between the opening and closing braces) we use the so-called ternary operator to assign the actual frequency to "freq". The ternary operator can be interpreted as a condensed if-else-bock looks as follows:
(condition) ? value_if_true : value_if_false
Here, the condition is "letter.find(c) == freq.end()", that means, if the letter c (which comes from "test") doesn't already exist as a key in our map, we set it to zero. Otherwise, if it already exists, we increase its frequency by 1
Nice One!
This is the best video on RUclips explaining maps. Great vid.
You have great teaching skills. You explain every detail. Nice video!
Thank you!🚗🚗
A quick correction to when you said that you can’t expect what the output is. Maps are actually ordered by key. In your case, you’ll see that the values were outputted in the lexicographical order of the char keys. There is a concept of maps that doesn’t care about order called unordered_map.
I noticed that when I use integers as keys. Thanks.
@iSpiritus I was going to write the same comment, then I found yours. thx for the tip and that doesn't change the fact about this video, it is a very good one.
My man 👌🏾, thanks for pointing this out
in an unordered map, are there still premade indexes for the "beginning" and "end" of them?
Thanks bro Tim ...for everything.....
Appreciate your hard work ! 🙂
Such a good tutorial.
When counting the number of times a letter occurs, you can just use "++freq[letter]". The index operator [] automatically inserts a new element if it doesn't exist.
Thanks for the good explanation.
Great job dude
I follow you since I have memory, and I love this channel because in a unequal country like Colombia when I am from i can learn with your channel, Colombia government is KILLING us, SOS
Nice, really nice 👍
Tim is grinding right now!!!!!
For the letter counter example, I don't think the if statement that checks if a letter is in the map is needed. Since the default value for ints in the map is 0, we can just start adding right away.
Im thinking the same
algo expert da best !!
This lesson really helped me a lot. Thank you very much!
Great video!!!
loved it!!
good video thank you
So people are aware since undefined keys will return a value of 0 in maps you don't actually have to check if the key exists for the practical map example.
string test = "Hello world my name is tim! ttthhaaa";
map frequency = {};
for (int i = 0; i < test.length(); ++i)
++frequency[test[i]];
for (auto itr = frequency.begin(); itr != frequency.end(); ++itr)
cout first
Will I ever get a chance to see tim with long hairs before I die ?
you can see his long pubic hairs
Hey so, was looking if you were going to go into package installers and how to use them, like pip or npm
So map, in other programing languages, is a dictionary.
Exactly
I got confused at the last example. The map "freq" was only declared, not initialized. How does it have the keys and values?
with the example at the end you don't actualy have to check if it already exists because if it doesnt exist it will equal 0 and (for some reason) if you just say ++ after a nonexistend pair it will make the pair.
I think this happens because the map automatically initializes and adds the pair to itself if you try to access it, the only thing is that its set to its default initialization value 0.
The reason as to why it is done this way doesnt really make sense to me, I get returning the default value but it getting added to the map seems like a potential flaw
Hello Tim,
For maps I understand that we can create key and value... But for the line `map`, can we add one more data type to it like `map`?
Great explanation of map! Just what I was looking for. I was curious about how it returns the map.end() if the find method doesn't contain the key one is looking for.. does this mean the map iterates through the entire structure to find the key's pointer? I'm guessing no since that would make it O(n)..
You can use shorthand for loop for iterating on a map
for (auto p : mp) {
cout
Thank you 👍
An easier way for iterating through map:
for (auto i : mp)
cout
When you say that you aren't sure if you need the parentheses are you just saying you are not sure if it is a function/method (getter) or an actual persisted property/value of the object? (as it would be in other languages) or is there a specific C++ syntax that I am missing here?
I cant get the key when i use the value. The out put is 0. There is no syntax error. It only works one way and i dont know what to do?
What should I do if I want to get the max value of the map
Instead of using if else statement to cout a string letters frequecny, you can write freq[letter]++
Is this equivalent method of checking if the key is present in the map? if (freq.count(letter) == 0)
CLASSES AND OBJECT PLEASE TIM!!
From where do you learn these. Like which Udemy(or any) course you took.
And which platform you mostly follow for learning, and taking new courses.
I'm not sure about him but when you're learning the basic syntax of a language it's usually a lot quicker to learn by using documentation itself, some languages even give a quick written tutorial in their websites that cover all of it pretty quickly.
But again, I'm not sure if it's how Tim does it.
Great lesson Tim...I have a question...can the maps be used in scenarios such as network log monitoring in incident response? I am not a pro in IR but I feel like it is possible when there are millions of logs and minimal time...I appreciate answers from all commenters.
I am trying something like this at the moment (logfile on changes in a file). Sadly map does not like std::tm as a type, so I had to convert that, basicaly making it a map of std::time_t t and std::string report. Works nice just to keep reports, but pulling data out of this is kind of a messy nightmare ^^''' ... And I have no idea how time-efficient that trick is alltogether.
WOW! COOL! HI FROM RUSSIA! ))
So that's why i need to dereference my iterator, bc it returns a pointer
nice
Some projects that are easy to implement in Python and other programming languages are actually difficult to implement in C++. Can you do your Python projects in C++?
If you kindly do that, it will help me a lot.
Tnks from argentina bro
I came to the following solution:
int main ()
{
string test = "Hello world my name is tim! ttthhaaa";
map letter_freq;
for (int i = 0; i < test.size(); i++) {
letter_freq.insert(pair(test[i], letter_freq[test[i]]++));
}
for (auto & itr : letter_freq) {
cout
What happened with the private livestream??
Please make a video about vector pls
Present!
I have a query?
c++ map look like a python dictionary
Python dictionary looks like C++ maps lol.
This is plain wrong. std::map is not a hashmap, it’s a RB binary tree and it has O(log n) insertion and lookup time complexity. What you are describing is std::unordered_map.
Oh very interesting. This is what I did when you typed out the string and gave the instructions:
void MyOutPutTest(){
std::string test = "Hello world my name is time! ttthhaaa";
std::map frequency = {};
for (char const &thisLetter: test){
if (!frequency[thisLetter]){
//create initial value
frequency[thisLetter] = 1;
} else {
frequency[thisLetter]++;
}
}
//output counts
std::cout
console.log( ( () => 1)() + ( () => 2)() );
.......can someone explain a execution-trace on this code.
()=> 1 is exactly equal to
function returnOne(){
return 1;
}
rest you can understand
@@raghavgupta6186 i know but which function first execute.......
like which side does "+" operator see and execute....
the output is 3
@@arctan2 first of all both functions gets executed and 1 is returned from first function and 2 is returned from second then + operator is applied from left to right so 1 + 2 = 3
@@raghavgupta6186 yes i also thought the same......thanks for your time
@@arctan2 no problem👍
razvanon?
Klasna
👍🏼👍🏼👍🏼👍🏼👍🏼
its getting harder 😪😮💨
crap video. All you did was iterate through the map. Your technique is similar to 'the 'Count Sort' algorithm. You did not explain how you can tell instantly if a value exists or not WITHOUT iterating through the map. There is, however, another technique that you could use to achieve this.
Well how about you share that technique? I’m always open to criticism and feedback buts it hypocritical to suggest I provided no value when you provide none yourself.
Hello Tim please check discord , I have a question for you.Just like always love the content!!!
Bro, your face expressions, your voice tone, and much more besides the appearance is much like iRaphahell (search it on youtube) Idk why but you give me same vibes😂