How to cin to a vector

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

    Other answers would have you disallow a particular number, or tell the user to enter something non-numeric in order to terminate input. Perhaps a better solution is to use std::getline() to read a line of input, then use std::istringstream to read all of the numbers from that line into the vector.

    #include <iostream>
    #include <sstream>
    #include <vector>
    
    int main(int argc, char** argv) {
    
        std::string line;
        int number;
        std::vector<int> numbers;
    
        std::cout << "Enter numbers separated by spaces: ";
        std::getline(std::cin, line);
        std::istringstream stream(line);
        while (stream >> number)
            numbers.push_back(number);
    
        write_vector(numbers);
    
    }
    

    Also, your write_vector() implementation can be replaced with a more idiomatic call to the std::copy() algorithm to copy the elements to an std::ostream_iterator to std::cout:

    #include <algorithm>
    #include <iterator>
    
    template<class T>
    void write_vector(const std::vector<T>& vector) {
        std::cout << "Numbers you entered: ";
        std::copy(vector.begin(), vector.end(),
            std::ostream_iterator<T>(std::cout, " "));
        std::cout << '\n';
    }
    

    You can also use std::copy() and a couple of handy iterators to get the values into the vector without an explicit loop:

    std::copy(std::istream_iterator<int>(stream),
        std::istream_iterator<int>(),
        std::back_inserter(numbers));
    

    But that’s probably overkill.

    0 讨论(0)
  • 2020-11-27 15:37

    If you know the size use this

    No temporary variable used just to store user input

    int main()
    {
        cout << "Hello World!\n"; 
        int n;//input size
        cin >> n;
        vector<int>a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
    
    //to verify output user input printed below
    
        for (auto x : a) {
            cout << x << " ";
        }
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 15:37

    I ran into a similar problem and this is how I did it. Using &modifying your code appropriately:

       int main()
       {
       int input;
       vector<int> V;
       cout << "Enter your numbers to be evaluated: " 
       << '\n' << "type "done" & keyboard Enter to stop entry" 
       <<   '\n';
       while ( (cin >> input) && input != "done") {
       V.push_back(input);
        }
       write_vector(V);
       return 0;
      }
       
    
    0 讨论(0)
  • 2020-11-27 15:38
    #include<bits/stdc++.h>
    using namespace std;
    
    int main()
    {
    int x,n;
    cin>>x;
    vector<int> v;
    
    cout<<"Enter numbers:\n";
    
    for(int i=0;i<x;i++)
     {
      cin>>n;
      v.push_back(n);
     }
    
    
    //displaying vector contents
    
     for(int p : v)
     cout<<p<<" ";
    }
    

    A simple way to take input in vector.

    0 讨论(0)
  • 2020-11-27 15:40

    You need a loop for that. So do this:

    while (cin >> input) //enter any non-integer to end the loop!
    {
       V.push_back(input);
    }
    

    Or use this idiomatic version:

    #include <iterator> //for std::istream_iterator 
    
    std::istream_iterator<int> begin(std::cin), end;
    std::vector<int> v(begin, end);
    write_vector(v);
    

    You could also improve your write_vector as:

     #include <algorithm> //for std::copy
    
    template <typename T>
    void write_vector(const vector<T>& v)
    {
       cout << "The numbers in the vector are: " << endl;
       std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    }
    
    0 讨论(0)
  • 2020-11-27 15:43
    #include<iostream>
    #include<vector>
    #include<string>
    using namespace std;
    int main()
    {
        vector<string>V;
        int num;
        cin>>num;
        string input;
        while (cin>>input && num != 0) //enter any non-integer to end the loop!
    {
        //cin>>input;
       V.push_back(input);
       num--;
       if(num==0)
       {
       vector<string>::iterator it;
        for(it=V.begin();it!=V.end();it++)
            cout<<*it<<endl;
       };
    
    }
    return 0;
    
    };
    
    0 讨论(0)
提交回复
热议问题