I tried to declare vetor in predefine ? why it showing error?

vectorv;
v.reserve(4);
vector v{1,2,3,4};

@kailash_01 hey you can do vector<int> v {1, 2, 3, 4} for declaring and initialising a new vector.
for adding values to a vector that has been declared already, use v.push_back(value);
note: push_back() function will append values to the end of the vector and increase vector size by 1.

if you are trying to read user input into a vector, you can do this instead

vector<int> v;
cin >> n;
for (int i = 0; i < n; i++) {
int data;
cin >> data;
v.push_back(data);
}

@kailash_01 i hope i have solved your problem. please mark this doubt as resolved if you are satisfied

Hi Kailash since you are not responding to this thread I am marking your doubt as resolved for now. You can always reopen it if you want.

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.