0

enter image description here Why this code shows me an error on line 31 for cout<<x1<<x2;

//This code is used to define a tree.
#include <bits/stdc++.h>
#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

int main()

{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    vector<vector<int>> Tree;
    int edge, n1, n2;  //edges for the tree
    cin >> edge;
    Tree.resize(edge);

    for (int i = 0; i < edge; i++)
    {
        cin >> n1 >> n2;
        Tree[n1].push_back(n2);
    }

    for (auto x1 : Tree)
    {
        for (auto x2 : x1)
        {
            cout << x1 << x2; //Here, it shows error
        }
        cout << endl;
    }

    return 0;
}

Could you please explain briefly where am I wrong. Also this is my first question, so please dont be harsh on me.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 3
    `x1` is a `std::vector`. You are trying to stream the vector to `std::cout`. If that is your intention then [this question](https://stackoverflow.com/questions/10750057/how-to-print-out-the-contents-of-a-vector) is a duplicate. But it sounds like you are just trying to print each value, in which case you probably want `cout << x2;`. – François Andrieux Sep 28 '20 at 14:17
  • 1
    Please be more specific than "it shows error". Even if you can't understand the message, there's a pretty good chance that some people around here can. – molbdnilo Sep 28 '20 at 14:17
  • I assume you do not have those asterisks near `cout` in your actual code, so I removed them. If you do have them, you can roll back my edit, but then the solution of your problem is to just remove that. – Yksisarvinen Sep 28 '20 at 14:18
  • You should probably be using `Tree.at(n1)` here instead of `Tree[n1]` because you do not validate that the user input `n1` is a legal index in `Tree`. Using `at` will generate an error if the user provides an invalid value. Otherwise, you may have Undefined Behavior. – François Andrieux Sep 28 '20 at 14:20
  • @FrançoisAndrieux I've added a picture. Please have a look at it – Devang Pandey Sep 28 '20 at 14:26
  • Please provide error messages as text in the question. Posting images of text such as code and error messages is discouraged. You can read about this policy [here](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). – François Andrieux Sep 28 '20 at 14:26
  • @DevangPandey The only information my first comment is asking is what your intention is. It is already clear to me what the error is, I describe it in the comment. How to fix it depends on what you are actually trying to achieve. – François Andrieux Sep 28 '20 at 14:27
  • I wanted to add an extra piece of bit.I cant add any string to it. @FrançoisAndrieux – Devang Pandey Sep 28 '20 at 14:28
  • @DevangPandey Please be specific about what information you want `x1` to print in `cout << x1 << x2`. – François Andrieux Sep 28 '20 at 14:29
  • @FrançoisAndrieux I want to print the value of x1 as well as x2 at the same time and a space between them – Devang Pandey Sep 28 '20 at 14:30
  • @DevangPandey `x1` is a collection, it does not have a printable value. What I'm not sure of is what you expect the printable value of `x1` is. Lets try this instead : please provide a sample input and the output you expect the program to generate from that input. – François Andrieux Sep 28 '20 at 14:32
  • Lets say I wanted 6 edges. Edge no 1 has two children 2 and 3. So I wanted output to be as .... 1 __ 2 and 1 __ 3 @FrançoisAndrieux – Devang Pandey Sep 28 '20 at 14:33

1 Answers1

1

In the expression for (auto x1 : Tree) the variable x1 is an std::vector<int>. It is not easy to get the index that a given x1 has in Tree to print it. The solution is to instead iterate over the range of indices in Tree :

for (std::size_t x1 = 0; x1 < Tree.size(); ++x1)
{
    // ...
}

Now x1 is an integer type which can be printed. You can access the elements of the vector it designates by using Tree's operator[] :

for (std::size_t x1 = 0; x1 < Tree.size(); ++x1)
{
    for (auto x2 : Tree[x1])
    {
        cout << x1 << x2;
    }
}

You'll also want to add white space to your output or you'll just get a series of unformatted numbers. For example, you can add a space between the numbers and end the line after each pair :

for (std::size_t x1 = 0; x1 < Tree.size(); ++x1)
{
    for (auto x2 : Tree[x1])
    {
        cout << x1 << ' ' << x2 << '\n';
    }
}
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • 1
    In addition you should read [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), [Why should I not #include ?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) and [Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);](https://stackoverflow.com/questions/31162367/significance-of-ios-basesync-with-stdiofalse-cin-tienull). These are often a sign of [Cargo Cult Programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). – François Andrieux Sep 28 '20 at 14:44
  • would be nice to have something like pythons `enumerate` that lets you write `for (auto &x : enumerate(container)) { auto index = x.first; auto value = x.second; }`. Not too difficult to write but also not too trivial to get it right. – 463035818_is_not_an_ai Sep 28 '20 at 15:08
  • 1
    @idclev463035818 Like [this](https://github.com/therocode/enumerate/blob/master/enumerate.hpp)? Not mine, but it has the MIT license. – François Andrieux Sep 28 '20 at 15:28
  • why droping range loop? – Marek R Sep 28 '20 at 15:30
  • @MarekR Because the goal is to print to index of each inner vector has in the outer vector. – François Andrieux Sep 28 '20 at 15:31
  • yes like that :) Though, some won't be pleased when I start using such stuff at work. Things are so much easier when there is a `std::` in front – 463035818_is_not_an_ai Sep 28 '20 at 15:32
  • @idclev463035818 Consider yourself lucky. There are places where `std::` make things *harder* to use. [Not invented here](https://en.wikipedia.org/wiki/Not_invented_here) syndrome. – François Andrieux Sep 28 '20 at 15:34