比较 std::vector 大小时出错

发布于 2024-11-05 10:36:03 字数 2993 浏览 0 评论 0原文

我使用标准 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] 应等于 2p3[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染柒℉ 2024-11-12 10:36:03

这将创建一个具有 2 个初始化为 0 的元素的向量:

p3[1] = std::vector<int>(2);

这将创建一个具有一个初始化为 2 的元素的向量:

p3[1] = std::vector<int>(1, 2);

This creates a vector with 2 elements initialized to 0:

p3[1] = std::vector<int>(2);

This creates a vector with one element initialized to 2:

p3[1] = std::vector<int>(1, 2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文