How to cin to a vector

后端 未结 20 2485
萌比男神i
萌比男神i 2020-11-27 14:44

I\'m trying to ask the user to enter numbers that are put into a vector, then using a function call to count the numbers, why is this not working? I am only able to count t

20条回答
  •  爱一瞬间的悲伤
    2020-11-27 15:31

    You can simply do this with the help of for loop
    ->Ask on runtime from a user (how many inputs he want to enter) and the treat same like arrays.

    int main() {
            int sizz,input;
            std::vector vc1;
    
            cout<< "How many Numbers you want to enter : ";
            cin >> sizz;
            cout << "Input Data : " << endl;
            for (int i = 0; i < sizz; i++) {//for taking input form the user
                cin >> input;
                vc1.push_back(input);
            }
            cout << "print data of vector : " << endl;
            for (int i = 0; i < sizz; i++) {
                cout << vc1[i] << endl;
            }
         }
    

提交回复
热议问题