So, Few hours back i didn't know the full form of STL. A friend recommended me your channel.... I'm loving it. ❤️ You're the kind of teacher i ever wanted. Thanks 🙌
No, both the methods "vector vec(int size); " and "vector vec; vec.reserve(int size)" are different....... the first method will initialize a vector with the size of (int size) but in the second method the empty vector is initialized with 'capacity' of (int size).
Nice video bro.. In one of my interview I said I will use reserve in vector to fix my capacity.. But the interviewer comes up with saying that if that is the case why you are not using array.. I got locked up here.. I simply answered vector class internally has many API which will be helpful fr my logic implementation where as array doesn't... But at the end he suggested me not to use reserve in vector...
If reserve was bad it wouldn't be there, the point is if you already know your upper bound and that upper bound is never going to change then you will use array, but if you know that there is some upper bound but it may increase then you will go for vector. Never go with something is bad, everything is situational, you just have to find the correct situation to use something. And that's what i have explained in video.
In such case the answer to the interviewer would be : If i use array with a fixed size,than it never gonna extend the array capacity when it hit max limit .so with reserve(),there are two benefits: optimization by not copying each time + auto capacity extension. same is mentioned in this nice video .
I think there are two drawbacks with vector 1. copy elements to new array as you run out of space consumes time 2. Wastage of unwanted memory: consider the last case where you have 32 size and 32 capacity what if you want to add 1 more element the array increases its capacity to 64 and inserts the 33rd element and the rest other 31 locations remain unutilized. 31 may seem too less but suppose you want to insert 1025 elements the capacity goes to 2048 elements making 1023 locations unitilized. an so on..
Which one allocates the memory? reserve or resize or both? When resize is used, it initializes all the elements with zero. That means, it must be allocating the memory as well. But what reserve?
Awesome video, thanks! So, if I don't know the size of my collection but I know it will be big should I use std::list over std::vector? It seems std::list is better option on this case
Ok bro now if i created vector with size 1e6 and after inserting 1e6 elements i made pushback to insert more elements then the time of Push back in the stage of inserting 1e6+1 is O(N)?? And as regular the pushback time function in vector within the size of vector is O(1) All right?? And finally thanks alot for this fantastic explanation ❤❤❤ keep going
I have a doubt. If it really creates a new double sized array when size exceeds capacity, then the address of the array should also changed. Right? But i tried it. It is same till last. So, may be this is wrong. It is not creating a new array and copy values. It may create a new previous sized array. And may link it to the old somehow. I am not sure. Cuz, I am really new to programming. Explain this plz
but here is a problem too if we want to insert a element in between the vector then it has to again create a vector of 1000 size and again copy and all ,am i right ?
Sir, if that's so than why don't we use array with a size of 1000? By reserving the vector we are losing its dynamic property. Correct me if I'm wrong.
Sir suppose I initialised 5 elements and 5 elements space has been created, now as per the vector feature it would get double if I have filled all the 5 spaces, now when I insert the 6th element space then the capacity is 10 now. Suppose I don't need to insert any more element, then would there be any space wastage of 4 elements or is there any way out?
Hello I have data to load from what i have been given and i have been told to use this: ifstream source("file_name.txt"); vector y; double a; if (source.is_open()){ while(!source.eof()){ source >> a; y.push_back(a); } source.close(); } but i want to add the for (int i=0;..........) and i am not sure whether i am supposed to add it in between somewhere in the code given or outside of that code?
you should explain or atleast tells about 2D vector.i am stuck in 1 question for 2 hours because I don't think about 2D vector because of this lecture.btw explanation is good.
2, is the lowest positive integer that can be used to increase or decrease anything. In this case it does N * 2 for the capacity because its much faster than N + N. Also memory allocation is expensive, so adding 1 to the capacity everytime is unoptimal.
Sorry.. even i thought the same way, but then i remembered my senior once said you should listen too much or study too much to remember something about some topic after long time so its better now 😊.
You can either play the video faster with x1.5 or skip some parts. I rather have a Video that is verbose than a video that explains too rapid but not clarifying the topic in detail. @CppNuts please continue your really good content!
So, Few hours back i didn't know the full form of STL.
A friend recommended me your channel.... I'm loving it. ❤️
You're the kind of teacher i ever wanted. Thanks 🙌
Thanks dude..
Just watched this playlist one week before my placement exam...and here I am shortlisted into Wells Fargo! Thanks :)
Best of luck!
vector vec(int size); is easier than vec.resize() . vec.capacity() gets doubled when 'size' sized vector is full.
No, both the methods "vector vec(int size); " and "vector vec; vec.reserve(int size)" are different.......
the first method will initialize a vector with the size of (int size) but in the second method the empty vector is initialized with 'capacity' of (int size).
But both can be used to achieve the performance improvement, right?
If you know the size already define it using constructor.
sir I must say - You have very good teaching skills and thank you for teaching this topic in such an easy and systematic way!
You are most welcome
Everything is crystal clear. I'm on my way watching all the playlist. Thank you so much
Glad it was helpful!
Nice video bro..
In one of my interview I said I will use reserve in vector to fix my capacity.. But the interviewer comes up with saying that if that is the case why you are not using array.. I got locked up here.. I simply answered vector class internally has many API which will be helpful fr my logic implementation where as array doesn't... But at the end he suggested me not to use reserve in vector...
If reserve was bad it wouldn't be there, the point is if you already know your upper bound and that upper bound is never going to change then you will use array, but if you know that there is some upper bound but it may increase then you will go for vector.
Never go with something is bad, everything is situational, you just have to find the correct situation to use something.
And that's what i have explained in video.
@@CppNuts that's perfect explanation bro.. Thanku so much..
In such case the answer to the interviewer would be : If i use array with a fixed size,than it never gonna extend the array capacity when it hit max limit .so with reserve(),there are two benefits: optimization by not copying each time + auto capacity extension. same is mentioned in this nice video .
After watching your lectures and reading Articles from GFG my concept is totally clear. Thanks a lot !!
Glad to hear that
I think there are two drawbacks with vector
1. copy elements to new array as you run out of space consumes time
2. Wastage of unwanted memory: consider the last case where you have 32 size and 32 capacity what if you want to add 1 more element the array increases its capacity to 64 and inserts the 33rd element and the rest other 31 locations remain unutilized.
31 may seem too less
but suppose you want to insert 1025 elements the capacity goes to 2048 elements making 1023 locations unitilized. an so on..
We can make use of shrink_to_fit() to reduce the capacity of container in order to match it's size..
@@vikasmanav3715 still, copying the elements from the old array to the new array takes O(n)
gen1 sir i have watvhed 2 oof ur videos so far related to vectors and the way u explain i salute u
Best Educational channel ur brother from Syria♥️
Thanks you man!!
I like the music that comes at the start. It is so beautiful
Thank you..
bro a big salute for your awesome teaching
Thanks bro...
I think it is a great video I use vector very often but I never guess that such a complicated process is going on...
Best playlist ever for STL. Thanks a lot.
💗❤💗
💗❤❤❤💗
Glad you like it!
Keep on trucking. Good job.
Thanks man!!
Woww I understand clearly now, but how about vector& data; ??? And if I writing a sort algorithm like quicksort how I can use vectors?
Are you willing to use already provided std::sort or create your own?
CppNuts I created and finished my HW ty. 😊
You didnt explain the modifiers?? emplace,emplace_back??
Hi mr.rupesh ...your enthusiasm and the way u explain is very good ...am happy subscribing your channel .
Thanks man..
Very precise explanation, very helpful for a beginner ti understand the concept deeply..
Applause... 💐👏🏼best lecture
You have good voice and great Teaching skills. Thankyou 😌☺
Thanks man
Enjoying this series, can you also make a video on const as well
one word: Awesome
Thanks man!!
loving these videos rupesh sir
Thanks man..
The video is really great! I was expecting to see the difference between push_back and emplace_back.
I think i have video for emplace.
@@CppNuts plz give link
Nice explanation
Thank you
Sir I wish u could cover some more functions and give a brief of how it is used
Which one allocates the memory? reserve or resize or both? When resize is used, it initializes all the elements with zero. That means, it must be allocating the memory as well. But what reserve?
your explanation is good , thank you
Thanks..
Awesome video, thanks! So, if I don't know the size of my collection but I know it will be big should I use std::list over std::vector? It seems std::list is better option on this case
i love your teaching style ...can you do a Java series too like this please?
I have a long list of videos in C++, once they are done I can think for Java.
Thanks for the suggestion.
can you give a notes on complete stl and vector
great explanation sir...
Ok bro now if i created vector with size 1e6 and after inserting 1e6 elements i made pushback to insert more elements then the time of Push back in the stage of inserting 1e6+1 is O(N)?? And as regular the pushback time function in vector within the size of vector is O(1)
All right??
And finally thanks alot for this fantastic explanation ❤❤❤ keep going
Correct!!
Thanks
Very nicely explained and thanks bro keep it up 😍
thank you sir
Thanks
We are using vec.reserve(1000).....instead we should be using arr[1000 ] ..bcuz both will do the same work
when we use .reserve() then the capacity will be fix then where is the best part of linked list is used
Even after reserve you can extend it by assigning more.
extension will happen automatically on the fly .so no worries .
Please cover the modifiers....vector has to offer
Good explanation. Thanks..
Welcome..
Thanks for all your support.
Can you tell me which is the best compiler for c++
There is no best compiler, otherwise everyone will use that only.
can you also make video of creating our own vector class?
Sir didi u cover all the STL function??? Pls reply sir!!!
I have covered so many.
@@CppNuts mine questions is not many or less ,the question is all are covered or not
sir where is the separate video on modifiers in vectors
Awesome video sir!!
Thanks..
What happen to the previous vectors copy? Will they get deleted or they stay and occupy space?
Thank you very much :-) I really enjoy your way of teaching.
Thanks..
I have a doubt. If it really creates a new double sized array when size exceeds capacity, then the address of the array should also changed. Right?
But i tried it. It is same till last. So, may be this is wrong. It is not creating a new array and copy values. It may create a new previous sized array. And may link it to the old somehow. I am not sure. Cuz, I am really new to programming. Explain this plz
God You;
Thanks dude..
hello,
in my laptop this is output,
Size:1 Capacity:1
Size:2 Capacity:2
Size:3 Capacity:3
Size:4 Capacity:4
Size:5 Capacity:6
Size:6 Capacity:6
Size:7 Capacity:9
Size:8 Capacity:9
Size:9 Capacity:9
Size:10 Capacity:13
Size:11 Capacity:13
Size:12 Capacity:13
Size:13 Capacity:13
Size:14 Capacity:19
Size:15 Capacity:19
Size:16 Capacity:19
Size:17 Capacity:19
Size:18 Capacity:19
Size:19 Capacity:19
Size:20 Capacity:28
Size:21 Capacity:28
Size:22 Capacity:28
Size:23 Capacity:28
Size:24 Capacity:28
Size:25 Capacity:28
Size:26 Capacity:28
Size:27 Capacity:28
Size:28 Capacity:28
Size:29 Capacity:42
Size:30 Capacity:42
Size:31 Capacity:42
Size:32 Capacity:42
the size is not doubling.
After doubling the capacity we can't get any odd number capacity. Is this true brother?
Now which compiler are you using?
sir u saved ......THANKS ALOT
Glad it helped
Amazing explination... Thanks alot
Thanks man..
Why not using an array since we know what is the size of the vector?
Again you rock !!
Thanks..
Happy with this video 🤩
Thanks dude..
thanks bro
- Validateur
You're welcome
Hello hello hello. ..always gives goosebumps 😄😄😄😎😎
Thanks..
Thanks man!
My pleasure..
Little bit on the lengthy side could be a 9 min video.By the love your content.
but here is a problem too if we want to insert a element in between the vector then it has to again create a vector of 1000 size and again copy and all ,am i right ?
if u use reserve, then what will be the diff with array and remaining memory will be wasted if it's not used
Vector provide much more flexibility than array, and it is still applicable if you use reserve.
great work sir
Many many thanks
Came from botking's channel nice bhaiyya
Welcome dude..
He is my brother 😎
Sir, if that's so than why don't we use array with a size of 1000? By reserving the vector we are losing its dynamic property.
Correct me if I'm wrong.
Read the comments of other guys . He gave the reply of this question
You are best sir
Thanks man..
amazing content
Thanks..
Sir suppose I initialised 5 elements and 5 elements space has been created, now as per the vector feature it would get double if I have filled all the 5 spaces, now when I insert the 6th element space then the capacity is 10 now. Suppose I don't need to insert any more element, then would there be any space wastage of 4 elements or is there any way out?
Hello I have data to load from what i have been given and i have been told to use this:
ifstream source("file_name.txt");
vector y;
double a;
if (source.is_open()){
while(!source.eof()){
source >> a;
y.push_back(a);
}
source.close();
}
but i want to add the for (int i=0;..........)
and i am not sure whether i am supposed to add it in between somewhere in the code given or outside of that code?
Can we do vector.reserve(n) and get the value of n during run time?
Yes we can
you should explain or atleast tells about 2D vector.i am stuck in 1 question for 2 hours because I don't think about 2D vector because of this lecture.btw explanation is good.
so the size increases according to power of 2 manner rit? Is this true for every cpp compiler. I'm curious. And what about complexity is it order(1)
2, is the lowest positive integer that can be used to increase or decrease anything. In this case it does N * 2 for the capacity because its much faster than N + N.
Also memory allocation is expensive, so adding 1 to the capacity everytime is unoptimal.
but why it doubles the capacity? why does not it just add capacity by one?
You should know how vector works internally, this is my video please search and watch, all your doubts will get clear.
Please make video on semaphore
Yaa man!!
Right now i am focusing on DS.
Hi sir, After coyping into new doubled array previous array is deleted by free() right ?
Yes, correct!! but not using free, it will use delete in c++.
Can I use a variable in reserve like.....vec.reserve(n)
(After inputting n)
yes of course
You are best🤩🤩
Hey! Rupesh. Can i get text file of your programs along with all the comments?
Hi Anubhav, that is member only content. You need to become member of my channel. (It's pretty cheap)
what about iterators when are you going to explain
sir,give some more examples so that it makes much easier to us .
maza aa gya..
Dhanyawad bhai...
Use white theam 😊
But white theme will reduce mobile battery fast, because white pixel takes more battery. And it is not good for eyes. That's why i use dark theme.
@@CppNuts use but increase font size 😍
where are other Vector's Operation
you sound like neso academy??
Thanks
Ide name? Can anyone tell!
VS code
Please give subtitle
TH-cam generates subtitles.
I'd like to be your student :D
Thanks :D
too verbose... but good contents !
Sorry and thanks..
Too long
Sorry.. even i thought the same way, but then i remembered my senior once said you should listen too much or study too much to remember something about some topic after long time so its better now 😊.
first 3 min of this video is waste in this video
To verbose ,the whole concept could have completed in less time video
Thanks, will work on that.
You can either play the video faster with x1.5 or skip some parts. I rather have a Video that is verbose than a video that explains too rapid but not clarifying the topic in detail. @CppNuts please continue your really good content!
@@CppNuts verbose is fine as you do .some people may catch the lecture fast and some may be slow Hence let it be as its going .just a thought.
You could have said all this in 3 minutes.
Yes sometimes i drag a bit.