【问题标题】:Passing a boost::unordered_set as the result map to boost::split将 boost::unordered_set 作为结果映射传递给 boost::split
【发布时间】:2011-04-28 05:22:15
【问题描述】:

有谁知道将 boost::unordered_set 作为第一个参数传递给 boost::split 是否符合规定?在 libboost1.42-dev 下,这似乎会导致问题。这是一个导致问题的小示例程序,称之为 test-split.cc:

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/unordered_set.hpp>
#include <string>

int main(int argc, char **argv) {
  boost::unordered_set<std::string> tags_set;
  boost::split(tags_set, "a^b^c^",
               boost::is_any_of(std::string(1, '^')));
  return 0;
}

然后,如果我运行以下命令:

g++ -o test-split test-split.cc; valgrind ./test-split

我收到了一堆关于 valgrind 的投诉,如下所示(我有时也会看到没有 valgrind 的核心转储,尽管它似乎因时间而异):

==16843== Invalid read of size 8
==16843==    at 0x4ED07D3: std::string::end() const (in /usr/lib/libstdc++.so.6.0.13)
==16843==    by 0x401EE2: unsigned long boost::hash_value<char, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /tmp/test-split)
...
==16843==    by 0x402248: boost::unordered_set<std::string, boost::hash<std::string>, std::equal_to<std::string>, std::allocator<std::string> >& boost::algorithm::split<boost::unordered_set<std::string, boost::hash<std::string>, std::equal_to<std::string>, std::allocator<std::string> >, char const [26], boost::algorithm::detail::is_any_ofF<char> >(boost::unordered_set<std::string, boost::hash<std::string>, std::equal_to<std::string>, std::allocator<std::string> >&, char const (&) [26], boost::algorithm::detail::is_any_ofF<char>, boost::algorithm::token_compress_mode_type) (in /tmp/test-split)
==16843==    by 0x40192A: main (in /tmp/test-split)
==16843==  Address 0x5936610 is 0 bytes inside a block of size 32 free'd
==16843==    at 0x4C23E0F: operator delete(void*) (vg_replace_malloc.c:387)
==16843==    by 0x4ED1EE8: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.13)
==16843==    by 0x404A8B: void boost::unordered_detail::hash_unique_table<boost::unordered_detail::set<boost::hash<std::string>, std::equal_to<std::string>, std::allocator<std::string> > >::insert_range_impl<boost::transform_iterator<boost::algorithm::detail::copy_iterator_rangeF<std::string, char const*>, boost::algorithm::split_iterator<char const*>, boost::use_default, boost::use_default> >(std::string const&, boost::transform_iterator<boost::algorithm::detail::copy_iterator_rangeF<std::string, char const*>, boost::algorithm::split_iterator<char const*>, boost::use_default, boost::use_default>, boost::transform_iterator<boost::algorithm::detail::copy_iterator_rangeF<std::string, char const*>, boost::algorithm::split_iterator<char const*>, boost::use_default, boost::use_default>) (in /tmp/test-split)
...
==16843==    by 0x402248: boost::unordered_set<std::string, boost::hash<std::string>, std::equal_to<std::string>, std::allocator<std::string> >& boost::algorithm::split<boost::unordered_set<std::string, boost::hash<std::string>, std::equal_to<std::string>, std::allocator<std::string> >, char const [26], boost::algorithm::detail::is_any_ofF<char> >(boost::unordered_set<std::string, boost::hash<std::string>, std::equal_to<std::string>, std::allocator<std::string> >&, char const (&) [26], boost::algorithm::detail::is_any_ofF<char>, boost::algorithm::token_compress_mode_type) (in /tmp/test-split)
==16843==    by 0x40192A: main (in /tmp/test-split)

这是一个 Debian Squeeze 盒子;这是我的相关系统信息:

$ g++ --version
g++ (Debian 4.4.5-2) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ dpkg -l | grep boost
ii  libboost-iostreams1.42.0            1.42.0-4                     Boost.Iostreams Library
ii  libboost1.42-dev                    1.42.0-4                     Boost C++ Libraries development files
$ uname -a
Linux gcc44-buildvm 2.6.32-5-amd64 #1 SMP Fri Sep 17 21:50:19 UTC 2010 x86_64 GNU/Linux

但是,如果我将 libboost1.42-dev 降级为 libboost1.40-dev,代码似乎可以正常工作。那么这是 boost 1.42 中的一个错误,还是我通过传入一个无法处理序列的容器来滥用 boost::split?谢谢!

【问题讨论】:

  • FWIW,我只能用 boost::unordered_set 重现这些 valgrind 错误,而 GCC 的 std::unordered_set valgrinds 安静。
  • 也许以下示例值得考虑,因为它们更简单、更高效:codeproject.com/KB/recipes/Tokenizer.aspx 特别是“一些简单示例”部分。

标签: c++ boost split c++11 unordered-set


【解决方案1】:

显然,答案是no yes

使用以下代码,我在 unordered_set 上收到编译时警告和运行时断言 (Visual C++ v10),而 vector 工作正常(除了最后一个元素中的空字符串,由于尾随'^')。

boost::unordered_set<std::string> tags_set;
vector<string> SplitVec; // #2: Search for tokens
boost::split( SplitVec, "a^b^c^", boost::is_any_of("^") ); 
boost::split( tags_set, "a^b^c^", boost::is_any_of("^") );

源 (string) 和目标容器之间的迭代器兼容性是问题所在。我会发布警告错误,但这是“战争与和平”模板警告之一。

编辑:

这看起来像是 Boost unordered_set 中的一个错误?当我使用以下内容时,它会按您的预期工作:

std::unordered_set<std::string> tags_set_std;
boost::split( tags_set_std, string("a^b^c^"), boost::is_any_of(string("^")) );

【讨论】:

  • 谢谢史蒂夫。您使用的是哪个版本的 boost?
【解决方案2】:

我认为答案应该是肯定的。

读取标头(split.hppiter_find.hppsplitSequenceSequenceT&amp; Result 作为其第一个参数,并将其传递给 iter_split,后者从两个 boost::transform_iterators 进行范围构造:

SequenceSequenceT Tmp(itBegin, itEnd);
Result.swap(Tmp);
return Result;

因此,这种类型所需要的只是它有一个构造函数,该构造函数接受一对迭代器,这些迭代器取消引用std::string(或者,从技术上讲,取消引用到 BOOST_STRING_TYPENAME)。并且有一个 .swap() 成员.. 并且有一个 SequenceSequenceT::iterator 类型,其类型为 std::string

证明:

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <string>
#include <iterator>
#include <algorithm>
#include <iostream>
struct X
{
   typedef std::iterator<std::forward_iterator_tag,
           std::string, ptrdiff_t, std::string*, std::string&>
           iterator;
   X() {}
   template<typename Iter> X(Iter i1, Iter i2)
   {
       std::cout << "Constructed X: ";
       copy(i1, i2, std::ostream_iterator<std::string>(std::cout, " " ));
       std::cout << "\n";
   }
   void swap(X&) {}
};
int main()
{
  X x;
  boost::split(x, "a^b^c^", boost::is_any_of(std::string(1, '^')));
}

我认为unordered_set&lt;std::string&gt; 也应该满足这些要求。

【讨论】:

  • 谢谢@Cubbi。所以你的结论是,这是 Boost 1.42 中的一个错误,@Steve 在 VisualC++/boost 1.44 上看到的编译器警告具有误导性?
  • @Jeremy Stribling:这是我所期望的,看看我的测试和 gcc 的 unordered_set 如何在 boost 没有的地方工作,但他们可能有充分的理由。在称其为错误之前,我会在这里等待更多答案并进行更多测试。
  • @Jeremy - 见编辑,我用std::unordered_set而不是boost::unordered_set来工作
【解决方案3】:

这已在 boost-users 邮件列表中确认为 boost::unordered_set 实现中的一个错误。邮件列表上有一个可用的补丁,很快就会检查修复,希望能及时发布到 boost 1.45。

Boost-users: patch

Boost-users: confirmation

感谢大家对此进行调查!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多