如何缩进输出?

时间:2023-01-22 20:34:59

I'm trying to print binary tree

我要打印二叉树

void print_tree(Node * root,int level )
 {
    if (root!=NULL)  
    {  
        cout<< root->value << endl;
    }
    //...
}

How can I indent output in order to indent each value with level '-' chars.

如何缩进输出,以便用level '-' chars缩进每个值。

2 个解决方案

#1


22  

You can construct a string to contain a number of repitions of a character:

您可以构造一个字符串来包含一个字符的若干表示:

std::cout << std::string(level, '-') << root->value << std::endl;

#2


1  

cout has special characters, below are two:

cout有特殊字符,以下为两个:

'\t' - tab
'\n' - new line

Hope it helped.

希望它帮助。

#1


22  

You can construct a string to contain a number of repitions of a character:

您可以构造一个字符串来包含一个字符的若干表示:

std::cout << std::string(level, '-') << root->value << std::endl;

#2


1  

cout has special characters, below are two:

cout有特殊字符,以下为两个:

'\t' - tab
'\n' - new line

Hope it helped.

希望它帮助。