简体   繁体   English

std :: set和boost :: ptr_set之间的区别?

[英]Differences between std::set and boost::ptr_set?

I've changed some code to convert a std::set to a boost::ptr_set . 我更改了一些代码,将std::set转换为boost::ptr_set However, the code doesn't compile; 但是,代码无法编译。 the problem is that I'm assuming that the return value from a ptr_set insert is the same as a set insert (a pair<myIter, bool> ). 问题是我假设ptr_set插入的返回值与set插入( pair<myIter, bool> )相同。 After an hour on Google I found this , and it turns out that the return value from a ptr_set insert appears to be a bool. 在Google上一个小时后,我发现了这个问题 ,事实证明,从ptr_set插入返回的值似乎是布尔值。

Is there any definitive documentation on the differences between the ptr containers and the std containers? 关于ptr容器和std容器之间的区别是否有任何权威性文件? I haven't found anything on the boost website, but maybe I'm just being dumb... 我没有在boost网站上找到任何东西,但也许我只是傻瓜...

EDIT 编辑

Ok - what was confusing me was that this code 好的-让我感到困惑的是这段代码

  t.insert(s.release(s.begin()));
  p = t.insert(s.release(s.begin()));

reports no error on the first line on gcc, but reports no match for operator= on the second line, so I thought the error was in the return type. 在gcc的第一行上没有报告任何错误,但是在第二行上没有对operator=匹配报告,因此我认为错误在于返回类型。 However, if you comment out the second line, the first line is then reported as an error (release doesn't return an iterator). 但是,如果注释掉第二行,则第一行将被报告为错误(发行版不返回迭代器)。 My confusion was compounded by the link I posted, in which ptr_container's author states that "insert() in ptr_set<> returns bool". 我发布的链接使我的困惑更加复杂,其中ptr_container的作者指出“ ptr_set <>中的insert()返回bool”。 However, reading on down the link it becomes obvious that the code hadn't been finished at the time. 但是,仔细阅读链接后,很明显代码当时还没有完成。 Thanks Kerrek. 感谢Kerrek。

The following code works as expected, and the interface is the same as for std::set::insert() : 以下代码按预期工作,并且接口与std::set::insert()

#include <boost/ptr_container/ptr_set.hpp>
#include <boost/assign/ptr_list_inserter.hpp>
#include <iostream>

int main()
{
  boost::ptr_set<int> s;

  {
    auto p = s.insert(new int(4));
    std::cout << "Element " << *p.first << (p.second ? " inserted" : " already existed") << std::endl;
  }
  {
    auto p = s.insert(new int(4));
    std::cout << "Element " << *p.first << (p.second ? " inserted" : " already existed") << std::endl;
  }

  boost::assign::ptr_insert(s)(1)(2)(3)(4);

  for (auto it = s.begin(), end = s.end(); it != end; ++it) { std::cout << *it << "\n"; }
}

The documentation is perhaps not the easiest to navigate, but it's all there. 该文档可能不是最容易浏览的,但仅此而已。 You should look for the "set adapter" , though, perhaps that's not entirely obvious. 不过,您应该寻找“设置适配器” ,也许这并不完全明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM