-1

我正在使用Google 的 C++ B-tree,我遇到了一个可能有人能够回答的问题。

首先,我收到以下错误:

In file included from ref_impl/../include/btree_map.h:31:0,
             from ref_impl/core.cpp:48:
ref_impl/../include/btree.h: In instantiation of ‘btree::btree_node<Params>::reference btree::btree_node<Params>::value(int) [with Params = btree::btree_map_params<unsigned int, unsigned int, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> >, 256>; btree::btree_node<Params>::reference = std::pair<const unsigned int, unsigned int>&]’:
ref_impl/../include/btree.h:809:33:   required from ‘btree::btree_iterator<Node, Reference, Pointer>::pointer btree::btree_iterator<Node, Reference, Pointer>::operator->() const [with Node = btree::btree_node<btree::btree_map_params<unsigned int, unsigned int, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> >, 256> >; Reference = std::pair<const unsigned int, unsigned int>&; Pointer = std::pair<const unsigned int, unsigned int>*; btree::btree_iterator<Node, Reference, Pointer>::pointer = std::pair<const unsigned int, unsigned int>*]’
ref_impl/core.cpp:539:18:   required from here 
ref_impl/../include/btree.h:557:57: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

btree.h:

pointer operator->() const {
    return &node->value(position);
 }

这似乎是错误的原因。

我的部分代码如下:

auto range = duplicates.equal_range(query_ids_temp[i]);
    for (auto it = range.first; it != range.second; ++it) {
//error     
       std::vector<unsigned int>::iterator pos = std::find(deleted_queries.begin(), deleted_queries.end(), it->second);
    //error
        if (pos != deleted_queries.end()) {
            duplicates.erase(it);
            deleted_queries.erase(pos);

        } else {
            query_ids.push_back(it->second);
        }
    } 

错误似乎是it->second

另外我试过:

for (btree_multimap<unsigned int, unsigned int>::iterator it = range.first; it != range.second; ++it) {
                    //error
        auto temp = it->second;
                    //error
        std::vector<unsigned int>::iterator position = std::find(deleted_queries.begin(), deleted_queries.end(), temp);
        if (position != deleted_queries.end()) {
            duplicates.erase(temp);
            deleted_queries.erase(position);

        } else {
            query_ids.push_back(it->second);
        }
    }

请记住,错误似乎是在 auto temp = it->second;

我也试过:

auto p = duplicates.find(query_ids_temp[i]);
      if(p != duplicates.end()) { // found a name
        do {
                    //error
            auto temp = p->second;
                    //error
            auto pos = std::find(deleted_queries.begin(), deleted_queries.end(), temp);
            if (pos != deleted_queries.end()) {
                duplicates.erase(p->second);
                deleted_queries.erase(pos);

            } else {
                query_ids.push_back(p->second);
            }

          p++;
        } while (p != duplicates.upper_bound(query_ids_temp[i]));
      }
      else{
        cout << "Name not found.\n";
      }

问题似乎又是一样的: auto temp = p->second;

有什么建议么?

4

1 回答 1

1

谷歌的 B-tree 的中心警告是突变使迭代器无效。在循环内调用 duplicates.erase() 会使“p”无效。

出于这个原因,erase() 方法返回一个迭代器,因此您可以编写如下内容:

if (...) {
  p = duplicates.erase(...);
  ...
} else {
  ...
  p++;
}

顺便说一句,请提供有关哪个编译器版本发出警告的详细信息。

于 2013-03-25T17:22:07.787 回答