What?
Oh ok…trying…
This is correct way of input
for(int i = 0; i < n; i++){
int x;
cin >> x;
cricket[i] = x;
}
1 Like
for(int x : vector_name){
}
This iterates over the vector. So if your vector had say {5,2,1}
, x will take values 5 then 2 then 1 in each iteration.
1 Like
Well…i Tried this and it give the value I inserted…the code is:
#include<iostream>
#include<vector>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int nfo,mcr;
cin >> nfo >> mcr;
vector<int> football(nfo);
vector<int> cricket(mcr);
for(int i : football){
int x;
cin >> x;
football[i] = x;
cout << football[i] << endl;
}
for(int i : cricket){
int x;
cin >> x;
cricket[i] = x;
cout << cricket[i] << endl;
}
}
return 0;
}
Hmm…Ok…I’ll trying doing that
Try printing after taking whole input
for(int i : football){
int x;
cin >> x;
football[i] = x;
//cout << football[i] << endl;
}
for(int i=0;i<nfo;i++)
cout << football[i] << endl;
2 Likes
I thought since I gave the size of the vector I can reassign the garbage values stored in the vector using this approach…I may be wrong though!..
Problem is i
always remains 0. So, you are only updating vector at 0 th position.
vector <int> v(4);
for(int i : v)
{
int x;
cin>>x;
v[i]=x;
cout<<i<<"\n";
}
for(int i=0;i<4;i++)
cout<<v[i]<<" ";
Input 1 2 3 4
Output
0
0
0
0
4 0 0 0
2 Likes
Hmm…This is new…Thanks a lot guys…I learnt so much from this one question!
And I did it…the code got accepted!
Thanks a lot…all of you guys who helped me …are true gems!
1 Like