From the above code
int m = s[i].marks;
v[m].push_back( s[i] );
How are we able to pushback so many marks at a single index of vector.
Please explain how is this happening ?
From the above code
int m = s[i].marks;
v[m].push_back( s[i] );
How are we able to pushback so many marks at a single index of vector.
Please explain how is this happening ?
Hello @ujj1804_1156aee80205d0bf,
vector<student>v[101];
This is an array of vectors, its structure looks like this:
index -> vector<Student>
0 -> vector<Student>
1 -> vector<Student>
2 -> vector<Student>
3 -> vector<Student>
4 -> vector<Student>
.
.
.
100 -> vector<Student>
We are able to push back so many marks at a single index of the array because at each index there is a vector.
So at each index of the vector , there is a vector (inner vector), if we want to access the content directly from the inner vector ,how can we do that without using for loop , i mean if we want to print content of a specific index of inner vector ?
So at each index of the vector
It’s at each index of the array.
basically, it’s like if you want to create a normal array of size 10 of type int then you use,
int arr[10]
Now, if you want to create an array of size 10 of type vector then you use,
vector<int> arr[10]
To access inner elements, just use arr[i][j];
i is array’s index and j is vector’s index.
Sir in the above code the array is of type vector , but instead of 9 values (3 values at each index of array ) , its taking 3 values as input. Please tell why this is not taking 9 values as input as its the same syntax as written in the code i shared earlier ?
Suppose for input 1 2 3
i am getting output
1 0 0
2 0 0
3 0 0
which means that v[0][0],v[1][0], v[2][0] are getting initialized with the input and rest of the values automatically become zero.
@ujj1804_1156aee80205d0bf,
Your code is wrong,
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++){
cin>>n;
v[i].push_back(n);
}
}
Now try input,
1 2 3
4 5 6
7 8 9
Thanks sir , but in the bucket sort code which i shared earlier why we were able to pushback the elements using only one for loop . As here we require 2 for loops for entering values.
Actually, in that code, we are just iterating over the student array and pushing the respective student name with index = student mark.
Sir i have understood, i am very thankful to you for helping me and clearing the doubts.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.