C++ 将boost::unordered_集作为结果映射传递给boost::split

C++ 将boost::unordered_集作为结果映射传递给boost::split,c++,boost,split,c++11,unordered-set,C++,Boost,Split,C++11,Unordered Set,有人知道将boost::unordered_集作为boost::split的第一个参数传递是否合乎犹太教吗?在libboost1.42-dev下,这似乎会导致问题。下面是一个导致问题的小示例程序,称之为test-split.cc: #include <boost/algorithm/string/classification.hpp> #include <boost/algorithm/string/split.hpp> #include <boost/unorde

有人知道将boost::unordered_集作为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;
}
我在valgrind中收到了很多抱怨,如下所示(我有时也会看到coredumps没有valgrind,尽管它似乎随时间而变化):

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

显然,答案是否定的是的

使用下面的代码,在未排序的集合> <代码>中获得编译时警告和运行时断言(VisualC++),而向量工作良好(除了最后一个元素中的空字符串,由于尾随“^”)。p>


我认为答案应该是肯定的

读取标题(
split.hpp
iter\u find.hpp
split
sequencet&Result
作为其第一个参数,并将其传递给
iter\u split
,该范围从两个
boost::transform\u迭代器
s构建它:

SequenceSequenceT Tmp(itBegin, itEnd);
Result.swap(Tmp);
return Result;
因此,这种类型所需要的只是它有一个构造函数,该构造函数接受一对迭代器,迭代器对
std::string
(或者,从技术上讲,是为了提升字符串类型名)。并且有一个.swap()成员。。并且具有类型为
std::string
sequencet::iterator
类型

证明:

#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, '^')));
}
#包括
#包括
#包括
#包括
#包括
#包括
结构X
{
typedef std::迭代器
迭代器;
X(){}
模板X(Iter i1、Iter i2)
{

std::cout这在boost用户邮件列表上被确认为boost::unordered_集实现中的一个bug。邮件列表上有一个可用补丁,很快将检查修复程序,希望能及时用于boost 1.45


感谢大家关注这一点!

FWIW,我只能用
boost::unordered_set
重现这些valgrind错误,而GCC的
std::unordered_set
valgrinds安静地重现。也许以下示例可能值得考虑,因为它们更简单、更有效:特别是“一些简单的示例”部分。谢谢史蒂夫。你使用的是什么版本的boost?谢谢@Cubbi。所以你的结论是,这是boost 1.42中的一个bug,并且@Steve在VisualC++/boost 1.44上看到的编译器警告是误导性的?@Jeremy Stribling:这是我所期望的,看看我的测试和gcc无序集在boost没有的情况下是如何工作的,但是ey可能有一个很好的理由。我会在这里等待更多的答案,并在称之为bug之前进行更多的测试。@Jeremy-请参阅编辑,我是用
std::unordered_set
而不是
boost::unordered_set
$ 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
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("^") );
std::unordered_set<std::string> tags_set_std;
boost::split( tags_set_std, string("a^b^c^"), boost::is_any_of(string("^")) );
SequenceSequenceT Tmp(itBegin, itEnd);
Result.swap(Tmp);
return Result;
#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, '^')));
}