【发布时间】:2014-11-17 06:23:54
【问题描述】:
我想在控制台中打印一棵二叉树,这是我编写的代码,但我无法弄清楚这个错误。有人可以帮我吗?错误是 No matching function for call to push_back。
void getVerticalOrder(Node* root, int hd,std::map<int,std::vector<int> > &m)
{
// Base case
if (node == NULL)
return;
// Store current node in map 'm'
m[hd].push_back(root->payload); //Error in this line
// Store nodes in left subtree
getVerticalOrder(root->left, hd-1, m);
// Store nodes in right subtree
getVerticalOrder(root->right, hd+1, m);
}
【问题讨论】:
-
payload是int吗? (作为一般提示,您应该在 StackOverflow 上发布整个错误消息...) -
不,它是字符串@TonyD
-
您不能在
std::vector<int>中存储字符串。你需要一个std::vector<std::string> -
那么你为什么期望能够将
string推送到std::vector<int>中呢?如果您希望string包含int的文本表示,那么.push_back(std::stoi(root->payload));- 请参阅here。如果它可能不是有效的int,并且您不希望抛出异常,请遵循 Gypati 的建议。 -
哦,是的,这是一个非常愚蠢的错误。谢谢大家。
标签: c++ vector data-structures map binary-tree