使用std :: cout时的输出偏移

时间:2019-02-07 20:54:53

标签: c++ c++14 binary-tree preorder

您好亲爱的社区

使用std :: cout时,我发现了奇怪的行为。在第75行,我调用函数“ traverse(...)”。 遍历(...)将调用“ 访问(int)”。 visit(int)只会打印应用于该函数的int参数。

我在第75行上创建了一个断点,以研究二叉树上的预订如何工作,并发现涉及 visit(int)的控制台输出的奇怪行为。

如果我在第101行使用 visit(int)-功能内的

std::cout << std::endl << "Node: " << i << std::endl;

通常打印出来。但是,如果我要使用:

std::cout << std::endl << "Node: " << i;

如果先执行 visit(int)功能,则不会打印零(0)。在检查调试器时,我观察到应用于 visit(i)的变量 i 。从一开始:i = 0,将打印 NO 控制台输出,然后使用i = 2执行visit(i)的第二次运行。但是,打印2时,将打印0。 / p>

似乎失踪了

<< std::endl

将导致整个行的异常行为。

有人期望同样的问题吗?试试吧。

/* 
 * Project: Traversing of a binary-tree with the preorder (recursive).
 * Author: https://github.com/OtenMoten
 * 
 * A binary-tree is suitable for managing efficient data. 
 * "Preorder" means the algorithm will first check the root, then 
 * the left sub-tree and finally the right sub-tree.
 * 
 * Just check out https://stackoverflow.com/questions/2130416/what-are-the-applications-of-binary-trees
 * to read about the use-cases of binary-trees.
 * 
 * Coding Standard:
 * "a" = address variable (i.e. int& aTest;)
 * "i" = normal variable (i.e. int iTest;)
 * "p" = pointer variable (i.e. int* pTest;)
 * "x" = array (i.e. int xTest[10][5];)
 * 
 * Created on 6. Februar 2019, 23:05
 */

#include <cstdlib>
#include <stdio.h>
#include <iostream>

using namespace std;

constexpr int MAX_COLUMNS(4);

void print(int, int[][MAX_COLUMNS]);
void visit(int);
void height(int, int[][MAX_COLUMNS], int&, int&);
void traverse(int, int[][MAX_COLUMNS], int[]);
void out(int[]);

int main(int argc, char** argv) {

    constexpr int iCoutRows_Alpha = 9;
    constexpr int iCoutRows_Beta = 11;

    int aCurrentHeight = 0;
    int aFinalHeight = 1;
    int xDataFromTree[100] = {0};

    // Father - Left Son - Right Son - Data
    int xTreeAlpha[iCoutRows_Alpha][MAX_COLUMNS] = {
        {0, 5, 3, 0}, // Node 0 
        {4, 0, 0, 101}, // Node 1 
        {7, 0, 0, 102}, // Node 2 
        {0, 0, 0, 103}, // Node 3 
        {5, 1, 7, 0}, // Node 4 
        {0, 6, 4, 0}, // Node 5 
        {5, 0, 0, 104}, // Node 6 
        {4, 8, 2, 0}, // Node 7 
        {7, 0, 0, 105} // Node 8 
    };

    int xTreeBeta[iCoutRows_Beta][MAX_COLUMNS] = {
        {0, 2, 1, 0}, // Node 0 
        {0, 4, 8, 0}, // Node 1 
        {0, 5, 3, 0}, // Node 2 
        {2, 9, 6, 0}, // Node 3 
        {1, 10, 7, 0}, // Node 4 
        {2, 0, 0, 107}, // Node 5 
        {3, 0, 0, 102}, // Node 6 
        {4, 0, 0, 104}, // Node 7 
        {1, 0, 0, 101}, // Node 8 
        {3, 0, 0, 105}, // Node 9 
        {4, 0, 0, 103} // Node 10 
    };
    std::cout << std::endl;
    std::cout << "Preorder-Traversing" << std::endl;
    print(iCoutRows_Beta, xTreeBeta);
    std::cout << std::endl;

    traverse(0, xTreeBeta, xDataFromTree); // Start with the root, node 0.
    std::cout << std::endl << std::endl;

    height(0, xTreeBeta, aCurrentHeight, aFinalHeight);
    std::cout << "Height of the binary-tree = " << aFinalHeight;
    out(xDataFromTree);

    return 0;
};

void print(int iCountRows, int b[][MAX_COLUMNS]) {
    int iColumnA = 0;
    int iColumnB = 1;
    int iColumnC = 2;
    int iColumnD = 3;

    std::cout << std::endl;
    std::cout << "Binary-tree:";
    for (int iii = 0; iii < iCountRows; iii++) {
        std::cout << std::endl;
        std::cout << "Father - Left - Right - Data";
        printf("\n%3d %7d %7d %7d ", b[iii][iColumnA], b[iii][iColumnB], b[iii][iColumnC], b[iii][iColumnD]);
    };
};

void visit(int i) {
    std::cout << std::endl << "Node: " << i << std::endl;
};

void height(int root, int xBinaryTree[][4], int &aCurrentHeight, int &aFinalHeight) {
    aCurrentHeight++; // "aCurrentHeight" is the current layer of the node.
    if (aCurrentHeight > aFinalHeight) aFinalHeight = aCurrentHeight; // The final height is >= the current height.
    if (xBinaryTree[root][1] != 0) // The current node is NOT a leaf!
    {
        height(xBinaryTree[root][1], xBinaryTree, aCurrentHeight, aFinalHeight); // Get the height of the left sub-tree.
        height(xBinaryTree[root][2], xBinaryTree, aCurrentHeight, aFinalHeight); // Get the height of the right sub-tree.
    };
    aCurrentHeight--;
};

void traverse(int i, int baum[][4], int inhalt[]) {
    int az;
    visit(i);
    if (baum[i][1] != 0) // aktueller Knoten ist kein Blatt
    {
        traverse(baum[i][1], baum, inhalt); //linken Sohn
        traverse(baum[i][2], baum, inhalt); //rechten Sohn
    } else {
        az = inhalt[0]; // aktueller Knoten ist Blatt
        inhalt[az + 1] = baum[i][3];
        inhalt[0] = az + 1;
    };
};

void out(int xInputArray[]) {
    std::cout << std::endl;
    std::cout << "Count of leafs with data = " << xInputArray[0];
    std::cout << std::endl << std::endl;
    std::cout << "Data is:";

    for (int i = 1; i < xInputArray[0] + 1; i++) {
        std::cout << std::endl;
        std::cout << xInputArray[i];
    };
};

亲切的问候 奥滕(Oten)

1 个答案:

答案 0 :(得分:0)

std::cout对象被缓冲,这意味着它的内容不会在每个格式化输出上刷新到基础设备。 std::endl在还添加换行符后刷新对象。 在打印i之后,尝试添加此行:

std::cout.flush();

看看它是否打印i