有了c ++ 11,我问自己是否在c ++ 11中替换了boost :: ptr_containers.我知道我可以使用例如a std::vector<std::unique_ptr<T> >
,但我不确定这是否完全替代.处理这些案件的推荐方法是什么?
我使用的是考虑boost::ptr_container
从响应结果这个问题.我对库的最大问题是我无法在调试器中查看集合的内容,因为MSVC调试器无法识别它,因此我看不到容器的内容.(所有数据都在void *
内部存储)
我听说MSVC有一个名为"调试器可视化器"的功能,它允许用户让调试器更聪明地处理这类事情,但我从来没有写过这样的东西,而且我对这些东西并不十分熟悉.
例如,比较boost::shared_ptr
MSVC自己的行为std::tr1::shared_ptr
.在调试器中(即在Watch窗口中),boost版本显示为用于实现共享指针的大量内部变量,但MSVC版本显示为指向对象的普通指针(并且shared_ptr
内部是隐).
我怎样才能开始使用或实现这样的东西?
c++ visual-studio-2010 debuggervisualizer visual-c++ boost-ptr-container
让我们Base
和Derived
与数据成员的类:
class Base {
public:
Base(int i):f(i) { }
virtual void print() { cout << "base " << f << endl; }
int f;
};
class Derived: public Base {
public:
Derived(int i):Base(0),g(i) { }
void print() { cout << "derived " << g << endl; }
int g;
};
Run Code Online (Sandbox Code Playgroud)
现在创建的一些实例Base
,并Derived
在堆,并将它们存储在boost::ptr_vector
:
int main(int argc, char *argv[])
{
boost::ptr_vector<Base> v;
v.push_back(new Derived(1));
v.push_back(new Base(2));
v.push_back(new Base(3));
v.push_back(new Derived(4));
Run Code Online (Sandbox Code Playgroud)
打印所有对象:
for (std::size_t …
Run Code Online (Sandbox Code Playgroud) TL; DR VS2013的优化器是混淆的还是我的测量结果错误,或者全局虚拟实际上是否需要是易失性才能使测试有效或____?
免责声明:这主要是出于"学术"兴趣,我不希望我看到的差异真正影响任何生产代码.
简介:我最近的一些测量结果让我想到了这个问题,因为我看到了VS2013 之间std::vector<std::unique_ptr<T> >
和之间存在显着差异boost::ptr_vector
.(见注释有)
看来,对于我的特定测试用例,访问boost :: ptr_vector中的元素比使用unique_ptr的向量快50%!
我的测试代码在这里:http://coliru.stacked-crooked.com/a/27dc2f1b91380cca(我不会在这个问题中包括它,我将在下面包含片段)
gcc 4.8没有报告任何差异,所以这是VS2013的事情.
Start...
The timings are as follows for accessing all (1000000) elements 200 times:
* St6vectorISt10unique_ptrIjSt14default_deleteIjEESaIS3_EE: 1764 ms
* N5boost10ptr_vectorIjNS_20heap_clone_allocatorESaIPvEEE: 1781 ms
Dummy output: 500000
Run Code Online (Sandbox Code Playgroud)我的时间恰好与链接的测试代码是:
Start...
The timings are as follows for accessing all (1.000.000) elements 200 times:
* class std::vector<....>: 344 ms
* class boost::ptr_vector<unsigned int,....>: 216 ms
Dummy output: 500.000
Run Code Online (Sandbox Code Playgroud)测试循环看起来像这样,我还会在那里保留冗长的评论,这解释了我所看到的: …
我有一个boost::ptr_map
存储抽象基类(例如VectorWrapperBase)作为值,这允许我将字符串映射到不同类型的向量.
boost::ptr_map<std::string, VectorWrapperBase> memory_map;
//...
memory_map.insert(str_key, new VectorWrapper<T>());
Run Code Online (Sandbox Code Playgroud)
这似乎有效.但是,当我memory_map
作为另一个类的成员并尝试将该类存储在一个中时std::map
,编译失败.
class AgentMemory {
//...
private:
boost::ptr_map<std::string, VectorWrapperBase> memory_map;
};
std::map<std::string, AgentMemory> agent_map;
//...
agent_map.insert(std::pair<std::string, AgentMemory>(agent_name, AgentMemory()));
Run Code Online (Sandbox Code Playgroud)
最后一行失败了:
/SOMEPATH/boost_1_48_0/boost/ptr_container/clone_allocator.hpp:34
error: cannot allocate an object of abstract type ‘xyz::VectorWrapperBase’
Run Code Online (Sandbox Code Playgroud)
作为C++的新手,这是令人费解的.
我怀疑错误是由于地图插入复制了AgentMemory
涉及克隆的对象ptr_map
.由于我的VectorWrapper
对象不可复制,因此会引发错误.
我的问题是:
为了解决编译错误,我考虑了以下内容,但是没有太多C++经验无法确定哪个更合适:
我有一些继承boost :: noncopyable的预定义类型(所以我必须将指针存储在这些对象中).我使用boost :: ptr_map.据我所知,其中的第二个参数已经是一个指针.所以,代码:
ptr_map<string, boost::any> SomeMap;
typedef %Some noncopyable class/signature% NewType;
// Inserting now
boost::any *temp = new boost::any(new KeyEvent());
SomeMap.insert("SomeKey", temp);
Run Code Online (Sandbox Code Playgroud)
错误是:
error: no matching function for call to ‘boost::ptr_map<std::basic_string<char>, boost::any>::insert(const char [11], boost::any*&)’
UPD:当我没有将指针传递给any时any temp = any(new KeyEvent());
我明白了:
error: no matching function for call to ‘boost::ptr_map<std::basic_string<char>, boost::any>::insert(const char [11], boost::any&)’
是否有STL实用程序/算法可以delete *the_object_iterator;
对所有对象执行操作?这样我可以clear()
安全吗?STL容器是a set
,对象是指向用其创建的C++类的指针new
.
Boost似乎是最好的解决方案.我的目标是避免在不可复制的类上进行复制构造.
我试图使用ptr_vector来存储一些指针,但是我的main方法一出现就出错了.这是我的代码:
int main(void)
{
boost::ptr_vector<string> temp;
string s1 = "hi";
string s2 = "hello";
temp.push_back(&s1);
temp.push_back(&s2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误消息:
Critical error detected c0000374
Windows has triggered a breakpoint in Path_Tree.exe.
This may be due to a corruption of the heap, which indicates a bug in Path_Tree.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Path_Tree.exe has focus.
The output window may have more diagnostic information.
The program '[7344] …
Run Code Online (Sandbox Code Playgroud) c++ ×8
boost ×3
stl ×2
visual-c++ ×2
c++11 ×1
insert ×1
performance ×1
ptr-vector ×1
std ×1
stdmap ×1
swap ×1
types ×1
unique-ptr ×1