比较 std::vector 大小时出错
我使用标准 C++ 库元素映射和向量的一些简单代码遇到了一个奇怪的问题。
我有一个表示为 map
的图,其中 Edgelist 定义为 vector
。我还有一个 match
函数,它比较两个图表,如果 pattern
包含在 subject
中,则返回 true
。
该代码是一个更大的应用程序的一部分,但显示此问题的最小编译示例如下:
#include <vector>
#include <iostream>
#include <map>
typedef int Vertex;
typedef std::vector<Vertex> EdgeList;
typedef std::map<Vertex, EdgeList> PatternGraph;
typedef std::vector<Vertex>::iterator EdgeListIter;
typedef std::map<Vertex, EdgeList>::iterator GraphIter;
const Vertex ROOT = 1;
bool match(PatternGraph &pattern, Vertex p_start,
PatternGraph &subject, Vertex s_start)
{
int num_p, num_s;
num_p = pattern[p_start].size();
num_s = subject[s_start].size();
if (pattern[p_start].size() == 0)
return true;
if (subject[s_start].size() == 0)
return false;
if (pattern[p_start].size() != subject[s_start].size())
return false;
if (pattern[p_start].size() == 1) {
Vertex pattern_child, subject_child;
pattern_child = pattern[p_start][0];
subject_child = subject[s_start][0];
return match(pattern, pattern_child, subject, subject_child);
} else {
Vertex p1, p2, s1, s2;
p1 = pattern[p_start][0];
p2 = pattern[p_start][1];
s1 = subject[s_start][0];
s2 = subject[s_start][1];
return ((match(pattern, p1, subject, s1) && match(pattern, p2, subject, s2)) ||
(match(pattern, p2, subject, s1) && match(pattern, p1, subject, s2)));
}
}
bool test_match()
{
bool passed = true;
std::cout << "Running Match Test...";
PatternGraph p1, p2, p3;
p1[1] = std::vector<int>(2,3);
p1[2] = std::vector<int>(4,5);
p1[3] = std::vector<int>();
p1[4] = std::vector<int>();
p1[5] = std::vector<int>();
p2[1] = std::vector<int>(2,3);
p2[2] = std::vector<int>(4,5);
p2[3] = std::vector<int>();
p2[4] = std::vector<int>();
p2[5] = std::vector<int>();
p3[1] = std::vector<int>(2);
p3[2] = std::vector<int>(3,4);
p3[3] = std::vector<int>();
p3[4] = std::vector<int>();
if (!match(p1, ROOT, p2, ROOT)) {
std::cout << "P1 Does not Match P2 when it should" << std::endl;
passed = false;
} else if (match(p2, ROOT, p3, ROOT)) {
std::cout << "P2 matches P3 when it shouldn't" << std::endl;
passed = false;
} else {
std::cout << "Match Test Passed." << std::endl;
}
return passed;
}
int main(int argc, char *argv[])
{
test_match();
return 0;
}
输出为“正在运行匹配测试...P2 在不应该匹配时匹配 P3” 我调试了这段代码,发现由于某种原因,当调用 match(p2, ROOT, p3, ROOT)
时, p2[1]
和 < code>p3[1] 都等于 2
,而 p2[1]
应等于 2
且 p3[1]
应等于<代码>1。
我很困惑,感谢任何帮助。我使用的编译器是Apple的g++ 4.2.1
I am running into a weird issue with some simple code using the standard C++ library elements map and vector.
I have a graph that is represented as a map<int, Edgelist>
where an Edgelist is defined as a vector<int>
. I also have a match
function which compares two graphs and returns true
if the pattern
is contained in the subject
.
The code is part of a much larger application, but the smallest compiling sample that exhibits this problem is here:
#include <vector>
#include <iostream>
#include <map>
typedef int Vertex;
typedef std::vector<Vertex> EdgeList;
typedef std::map<Vertex, EdgeList> PatternGraph;
typedef std::vector<Vertex>::iterator EdgeListIter;
typedef std::map<Vertex, EdgeList>::iterator GraphIter;
const Vertex ROOT = 1;
bool match(PatternGraph &pattern, Vertex p_start,
PatternGraph &subject, Vertex s_start)
{
int num_p, num_s;
num_p = pattern[p_start].size();
num_s = subject[s_start].size();
if (pattern[p_start].size() == 0)
return true;
if (subject[s_start].size() == 0)
return false;
if (pattern[p_start].size() != subject[s_start].size())
return false;
if (pattern[p_start].size() == 1) {
Vertex pattern_child, subject_child;
pattern_child = pattern[p_start][0];
subject_child = subject[s_start][0];
return match(pattern, pattern_child, subject, subject_child);
} else {
Vertex p1, p2, s1, s2;
p1 = pattern[p_start][0];
p2 = pattern[p_start][1];
s1 = subject[s_start][0];
s2 = subject[s_start][1];
return ((match(pattern, p1, subject, s1) && match(pattern, p2, subject, s2)) ||
(match(pattern, p2, subject, s1) && match(pattern, p1, subject, s2)));
}
}
bool test_match()
{
bool passed = true;
std::cout << "Running Match Test...";
PatternGraph p1, p2, p3;
p1[1] = std::vector<int>(2,3);
p1[2] = std::vector<int>(4,5);
p1[3] = std::vector<int>();
p1[4] = std::vector<int>();
p1[5] = std::vector<int>();
p2[1] = std::vector<int>(2,3);
p2[2] = std::vector<int>(4,5);
p2[3] = std::vector<int>();
p2[4] = std::vector<int>();
p2[5] = std::vector<int>();
p3[1] = std::vector<int>(2);
p3[2] = std::vector<int>(3,4);
p3[3] = std::vector<int>();
p3[4] = std::vector<int>();
if (!match(p1, ROOT, p2, ROOT)) {
std::cout << "P1 Does not Match P2 when it should" << std::endl;
passed = false;
} else if (match(p2, ROOT, p3, ROOT)) {
std::cout << "P2 matches P3 when it shouldn't" << std::endl;
passed = false;
} else {
std::cout << "Match Test Passed." << std::endl;
}
return passed;
}
int main(int argc, char *argv[])
{
test_match();
return 0;
}
The output is "Running Match Test...P2 matches P3 when it shouldn't"
I have debugged this code and found that for some reason, when match(p2, ROOT, p3, ROOT)
is being called, the sizes of p2[1]
and p3[1]
are both equal to 2
, when p2[1]
should be equal to 2
and p3[1]
should be equal to 1
.
I am stumped and any help is appreciated. The compiler I am using is Apple's g++ 4.2.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将创建一个具有 2 个初始化为 0 的元素的向量:
这将创建一个具有一个初始化为 2 的元素的向量:
This creates a vector with 2 elements initialized to 0:
This creates a vector with one element initialized to 2: