简体   繁体   English

为什么此代码在第 31 行向我显示 cout< 的错误

[英]Why this code shows me an error on line 31 for cout<<x1<<x2;

在此处输入图片说明Why this code shows me an error on line 31 for cout<<x1<<x2 ;为什么这段代码在第 31 行显示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.这也是我的第一个问题,所以请不要对我苛刻。

In the expression for (auto x1 : Tree) the variable x1 is an std::vector<int> .for (auto x1 : Tree)的表达式中for (auto x1 : Tree)变量x1是一个std::vector<int> It is not easy to get the index that a given x1 has in Tree to print it.获取给定x1Tree的索引来打印它并不容易。 The solution is to instead iterate over the range of indices in Tree :解决方案是迭代Tree的索引范围:

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

Now x1 is an integer type which can be printed.现在x1是可以打印的整数类型。 You can access the elements of the vector it designates by using Tree 's operator[] :您可以使用Treeoperator[]访问它指定的向量的元素:

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';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 定义一个类`MultiInherit <MyTuple<X1,X2,…> &gt;`从`X1,X2,...`继承 - define a class `MultiInherit<MyTuple<X1,X2,…>>` to inherit from `X1,X2,…` 在GLFW中,哪个按钮定义了X1和X2按钮? - In GLFW which button defines the X1 and X2 buttons? 如何在不擦除的情况下将 \\x1 \\x2 \\x3 ... 字符转换为普通字符? - How to convert \x1 \x2 \x3 … characters to normal characters without erasing? 将 tag1 tag2 tag3 替换为 x1 x2 x3 - Replace tag1 tag2 tag3 for x1 x2 x3 为什么cout没有显示x? - Why is cout not displaying x? 给定两个点(x1,y1)(x2,y2),我该如何平均计算给定点之间的直线上的N个不同点 - Given two points (x1,y1) (x2,y2), how can I compute N different points evenly lying on the line between the given points 如果我有一条从点x1,y1到x2,y2的线,我该如何绘制从点x2,y2来的两条线成45度角以制作箭头? - If I have a line from point x1, y1 to x2, y2, how can I draw 2 lines coming from point x2, y2 at 45 degree angles to make an arrow? Vector.push_back(对 <int,int> (x1,x2)); 不起作用 - Vector.push_back(pair<int,int>(x1,x2 )); does not work 找到两个x1,x2之间的素数总数,找不到错误? - Finding total of prime numbers between two x1,x2, cant find mistake? 使用 C++ 删除 x1 和 x2 范围内值的节点 - Deleting Nodes of values in range x1 and x2 using C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM