Operator overloading

int main()
{
    vector<vector <int>> a={{1,2,3,4,5,6},{2,4,7,8},{1,2}};
    for (auto c: a)
    {
        cout<<c;
    }
}

the compiler is throwing error:

 no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::vector<int>’)
         cout<<c;

i want to use the same syntax of the for loop to traverse 2-D vectors , perhaps it is related to operator overloding , but i dont know how to fix it , can anybody help ?
thanks.

Hi @ayush_129
Can you post the entire code link

1 Like

what do you mean by code link ? sorry i am new to these jargons.

You can open ideone.com, paste your code there and run it and then share the generated link

https://ideone.com/sVSw2c here is the link

On line 7 make a correction

vector<vector<int> > a={{1,2,3,4,5,6},{2,4,7,8},{1,2}};

add a space between > and >

Firstly, Format your code.
Either do this

    for(const auto &vec : a){
        for(const auto &c : vec){
            cout<<c<<" ";
        }
        cout<<'\n';
    }

or
declare this at the top of your code

ostream& operator<<(ostream& out, const vector<vector<int>> &vec){
    for(int i=0;i<vec.size();i++){
        for(int j=0;j<vec[i].size();j++){
            out<<vec[i][j]<<" ";
        }
        out<<'\n';
    }
    return out;
}

Then you can type this

cout<<a;
4 Likes
1 Like

thank you for your reply, I want to know more about the second approach and operator overloading, can you suggest some resources.
And can you please tell me why you have used const for the pointer?
Thanks.

Because I’m not changing the value. it’s good practice to always declare constant variables as const.
operator overloading syntax is

"return value" operator"operator symbol"("left operand", "right operand"){
        //actual code here
} 
template<typename T>

can be used when you want the operator to do the same thing for many data types. The compiler will fill in the datatype for you.
Here’s a small example on how to use them.
cin is of type istream and cout is ostream

Operator overloading Code
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
template<typename T>
ostream& operator+(ostream& out, const vector<T> &vec){
    for(const auto &x : vec){
        out<<x<<" ";
    }
    out<<"\n";
    return out;
}
template<typename T>
istream& operator-(istream& in, vector<T> &vec){
    for(auto &x : vec){
        in>>x;
    }
    return in;
}
template<typename T>
void operator++(vector<T> &vec){
    T value;
    vec.push_back(value);
}
template<typename T>
void operator--(vector<T> &vec){
    vec.pop_back();
}
template<typename T>
void operator+=(vector<T> &vec, T val){
    vec.push_back(val);
}
template<typename T>
void operator+=(vector<T> &vec1, vector<T> &vec2){
    for(const auto &val : vec2){
        vec1+=val;
    }
}
void solve(){
    int n;
    cin>>n;
    vector<int> seq(n);
    cin-seq;
    seq+=2;
    ++seq;
    cout+seq;
    --seq;
    vector<int> seq2={1,3,4};
    seq+=seq2;
    cout+seq;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
}

thank you so much