0

I need to take a vector as an input in a C++ program.

The following is my code:

int main() {
        vector<int> nums;
        int target;
        
        cin >> nums;
        cin >> target;
        
        Solution mine;
        mine.twoSum(nums,target);
}

And following is the error shown by the compiler:

Line 32: Char 13: error: invalid operands to binary expression ('std::istream' (aka 'basic_istream<char>') and 'vector<int>')
        cin >> nums;
        ~~~ ^  ~~~~
Andreas DM
  • 10,685
  • 6
  • 35
  • 62

3 Answers3

1

You need to determine how to distinguish between elements, and also where the end is.

E.g. if you read ints until there are none left in the input, you will have read in the value intended for target.

template<typename T, typename A>
std::istream& operator>>(std::istream& is, std::vector<T, A> & vec) 
{
    for(T value;/* you need to define this condition*/ && (is >> value);) 
    {
        vec.push_back(std::move(value));
    }
    return is;
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
  • Such an operator would cause [lookup problems](https://stackoverflow.com/questions/38700941/best-way-to-specialise-operator-for-stdostream-and-stdvector-with-generic), and in general should be avoided. – Igor R. Jun 16 '21 at 12:55
  • @IgorR. only when it is used before it is defined – Caleth Jun 16 '21 at 15:12
  • Whenever it should be found via ADL. – Igor R. Jun 16 '21 at 15:36
  • @IgorR. [Not all lookup is ADL](https://coliru.stacked-crooked.com/a/5f403910a1bbd696) – Caleth Jun 16 '21 at 15:41
  • Of course. But *whenever* it should be looked-up with ADL (eg. within a template context), you've got a problem. – Igor R. Jun 16 '21 at 16:06
0

Problem:

You're attempting to read nums directly from std::cin with the operator >>. There isn't any overload of the operator for operator>> that takes a std::ostream (std::cin type) and std::vector<int> (nums's type), so there is a type error.

Solution:

You have to read each number and push one by one to the vector.

Example code:

Here there is an example where the last number goes to target and the rest to the nums.

#include <iostream>
#include <vector>

void readVectorFromStdCin(std::vector<int>& to_read)
{
    for(int n; std::cin >> n;)
        to_read.push_back(n);
}

int main()
{
    std::vector<int> nums;
    int target;

    readVectorFromStdCin(nums);
    target = nums[nums.size()-1];
    nums.pop_back();

    Solution mine;
    mine.twoSum(nums,target);
}
Gary Strivin'
  • 908
  • 7
  • 20
0

How to take vector as input in C++?

std::cin has no overload that takes a vector


I suggest that you read the first line of input (assuming space separated) into a string and then the target afterwards

std::string line;
int         target;

std::getline(std::cin, line);
std::cin >> target;

You can then create a string stream out of the first line

std::istringstream iss(line);

And then create a vector using the constructor that takes two iterators

std::vector<int> nums(std::istream_iterator<int>{ iss },
                      std::istream_iterator<int>{});
Andreas DM
  • 10,685
  • 6
  • 35
  • 62