0

I am having trouble making a vector that would return the original elements of a vector squared.

This is the error I am getting.

no operator "<<" matches these operands -- operand types are: std::ostream << std::vector<int, std::allocator<int>>

#include <iostream>
#include <vector>
#include <cmath>

void squareRoot(std::vector<int> &v){

    int x;
    std::vector<int> squared;

    for(auto i:v){
        int x = i*i;
        squared.push_back(x);
    }
    std::cout << squared << std::endl; //error occurs at this line
    return;
}


int main(){

    std::vector<int> v = {2,4,8,1};
    squareRoot(v);

    return 0;
}
Eliana Lopez
  • 161
  • 1
  • 1
  • 8
  • 1
    You have to overload the `operator<<(std::ostream&, std::vector const&)`. – Ghasem Ramezani Aug 15 '21 at 14:11
  • why are you squaring the index of the vector? – y_159 Aug 15 '21 at 14:13
  • 1
    And you can't just `cout<<` the whole vector, you've to iterate it to print it's elements. – y_159 Aug 15 '21 at 14:15
  • @y_159 thanks for catching the error, I fixed it to `int x = v[i]*v[i];` – Eliana Lopez Aug 15 '21 at 14:16
  • 1
    Now what's the error? the error i suspect is you are trying to print the whole vector(python way) without iterating it. – y_159 Aug 15 '21 at 14:18
  • 2
    `for(auto i:v)` means for each _element_ in `v`, not for each `index` in `v`, so you don't need `v[i]` which will likely be an out-of-bounds. There's no `<<` operator for `std::vector`, as well. – erip Aug 15 '21 at 14:20
  • @erip correct. op is still iterating over the index. – y_159 Aug 15 '21 at 14:20
  • @y_159 No, OP isn't. Do you understand how range-based for works? – Nathan Pierson Aug 15 '21 at 14:24
  • @NathanPierson I never used it. But the highlighted line from [this](https://www.geeksforgeeks.org/range-based-loop-c/#:~:text=%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0std%3A%3Acout%20%3C%3C%20i%20%3C%3C%20%27%20%27%3B) link tells me the way op is doing this is wrong. – y_159 Aug 15 '21 at 14:28
  • It's clearly iterating for the items present in the vector/container. It's not referring to the index here. – y_159 Aug 15 '21 at 14:29
  • Should be `int x = i*i;` https://ideone.com/JepZnK – Retired Ninja Aug 15 '21 at 14:30
  • @ElianaLopez simply replace the `std::cout << squared << std::endl;` with `for` loop and `cout` each element within. – y_159 Aug 15 '21 at 14:35
  • `squareRoot` is a rather strange name for a function that does what your function does. Where's the root? Perhaps more importantly, if you want to write a function that prints, make sure that it (1) has the word "print" or similar in its name, and (2) prints and doesn't do anything else. A function named `squareRoot` should compute square roots and do nothing else. – n. m. could be an AI Aug 15 '21 at 14:35

1 Answers1

1

You can define the << operator for vector<int> (or as a template for vector<T>). Insert the following code before squareRoot:

std::ostream& operator<<(std::ostream& os, const std::vector<int>& v) {
    os << "[";
    bool first = true;
    for (const auto& x : v) {
        if (first) {
            first = false;
        } else {
            os << ", ";
        }
        os << x;
    }
    os << "]";
    return os;
}

which gives

[4, 16, 64, 1]
tueda
  • 750
  • 5
  • 14