0

I am student studying Computer science just started programming. I am having an issue when using VSCode. Here is my main.cpp:

#include <bits/stdc++.h>

using namespace std;

template <template <typename, typename> class C, typename K, typename V>
std::ostream &operator<<(std::ostream &os, C<K, V> a)
{
    os << "[ ";
    for (auto t : a)
    {
        os << t << " ";
    }
    os << "]";
    return os;
}
template <template <typename> class V, typename T>
std::ostream &operator<<(std::ostream &os, V<T> a)
{
    os << "[ ";
    for (T t : a)
    {
        os << t << " ";
    }
    os << "]";
    return os;
}
template <typename T, typename V>
std::ostream &operator<<(std::ostream &os, pair<T, V> a)
{
    os << "[" << a.first << ", " << a.second << "]";
    return os;
}

template <typename T>
std::istream &operator>>(std::istream &is, vector<T> &a)
{
    for (T &t : a)
    {
        is >> t;
    }
    return is;
}
template <typename T, typename V>
std::istream &operator>>(std::istream &is, pair<T, V> &a)
{
    is >> a.first >> a.second;
    return is;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    int t = 1;
    // cin >> t;
    while (t--)
    {
        vector<int> a(5);
        map<int, int> b;
        for (int i = 0; i < 5; i++)
        {
            a[i] = i * 2;
            b[i] = i * 2;
        }
        cout << a << endl;
        cout << b << endl;
    }
}

VSCode shows error on line 73 when I editting that no operator "<<" matches these operands -- operand types are: std::ostream << std::map<int, int, std::less, std::allocator<std::pair<const int, int>>>

The problem is I have compile the file perfectly (no error found) with command

g++ -std=c++20 main.cpp -o main

then I recompile with this command

g++ -std=c++14 main.cpp -o main

the compiler was starting show error like VSCode did.

Obvious, it's not an error, how do I change the version of C++ that VSCode used?

  • You edit the c_cpp_properties.json file for intellisense and tasks.json for building. The documentation explains both files here: [https://code.visualstudio.com/docs/cpp/config-mingw](https://code.visualstudio.com/docs/cpp/config-mingw) – drescherjm Jan 09 '22 at 15:44
  • 3
    [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Jan 09 '22 at 15:48
  • @Evg Thanks for attention, but that's not my question. It's not a big deal to me, I use C++ on Compete Contest – Nguyễn Đức Huy Jan 09 '22 at 15:50
  • @273K So I used operator overloading to save time. – Nguyễn Đức Huy Jan 09 '22 at 15:52
  • 1
    Map has 3 template parameters, neither of the operator>> overloads is suitable. – 273K Jan 09 '22 at 15:55
  • 3
    That's why it's just a comment and not an answer. Note that your code might be found by other people in the future, and that's why we should not spread notoriously bad practices even in questions. Especially among those who just started programming. – Evg Jan 09 '22 at 16:01
  • @drescherjm I already do search before. I added ```"cppStandard": "c++20"``` into my *c_cpp_properties.json* but it did not work. – Nguyễn Đức Huy Jan 09 '22 at 16:08
  • @273K Work perfect with -std=c++20 – Nguyễn Đức Huy Jan 09 '22 at 16:39

1 Answers1

0

I found answer for my question.

I use this following code:

template <template <typename...> class _Tr, typename... _Args>
std::ostream &operator<<(std::ostream &os, _Tr<_Args...> a)
{
    os << "[ ";
    for (T t : a)
    {
        os << t << " ";
    }
    os << "]";
    return os;
}

I tried and It works on -std=c++14 with map, multimap, set, multiset, vector, unordered_map, unordered_set

Python-like version

template <template <typename...> class _Tr, typename... _Args>
std::ostream &operator<<(std::ostream &os, _Tr<_Args...> a)
{
    os << "[";
    auto s = a.begin();
    auto e = a.end();
    e--;
    for (auto it = s; it != a.end(); it++)
    {
        cout << *it << (it == e ? "" : ", ");
    }
    os << "]";
    return os;
}