How to cin to a vector

后端 未结 20 2450
萌比男神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:30

    would be easier if you specify the size of vector by taking an input :

    int main()
    {
      int input,n;
      vector<int> V;
      cout<<"Enter the number of inputs: ";
      cin>>n;
      cout << "Enter your numbers to be evaluated: " << endl;
      for(int i=0;i<n;i++){
      cin >> input;
      V.push_back(input);
      }
      write_vector(V);
      return 0;
    }
    
    0 讨论(0)
  • 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<int> 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;
            }
         }
    
    0 讨论(0)
  • 2020-11-27 15:31

    These were two methods I tried. Both are fine to use.

    int main() {
           
            int size,temp;
            cin>>size;
            vector<int> ar(size);
        //method 1 
          for(auto i=0;i<size;i++)
              {   cin>>temp;
                  ar.insert(ar.begin()+i,temp);
              }
              for (auto i:ar) 
                cout <<i<<" "; 
                
         //method 2
         for(int i=0;i<size;i++)
         {
            cin>>ar[i];
         }
         
         for (auto i:ar) 
                cout <<i<<" "; 
            return 0;
        }
    
    0 讨论(0)
  • 2020-11-27 15:33
    #include<iostream>
    #include<vector>
    #include<sstream>
    using namespace std;
    
    int main()
    {
        vector<string> v;
        string line,t;
        getline(cin,line);
        istringstream iss(line);
        while(iss>>t)
            v.push_back(t);
    
        vector<string>::iterator it;
        for(it=v.begin();it!=v.end();it++)
            cout<<*it<<endl;
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 15:34

    One-liner to read a fixed amount of numbers into a vector (C++11):

    #include <algorithm>
    #include <iterator>
    #include <iostream>
    #include <vector>
    #include <cstddef>
    
    int main()
    {
        const std::size_t LIMIT{5};
        std::vector<int> collection;
    
        std::generate_n(std::back_inserter(collection), LIMIT,
            []()
            {
                return *(std::istream_iterator<int>(std::cin));
            }
        );
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 15:34

    You probably want to read in more numbers, not only one. For this, you need a loop

    int main()
    {
      int input = 0;
      while(input != -1){
        vector<int> V;
        cout << "Enter your numbers to be evaluated: " << endl;
        cin >> input;
        V.push_back(input);
        write_vector(V);
      }
      return 0;
    }
    

    Note, with this version, it is not possible to add the number -1 as it is the "end signal". Type numbers as long as you like, it will be aborted when you type -1.

    0 讨论(0)
提交回复
热议问题