C++ 如何实现排序指针向量?
我想实现一个排序指针向量,如下所示C++ 如何实现排序指针向量?,c++,containers,C++,Containers,我想实现一个排序指针向量,如下所示 #include <vector> #include <memory> #include <algorithm> //! A random accessed vector with sorted allocated elements. //! - Elements must be allocated on heap. //! - The vector manages the memories of its elements
#include <vector>
#include <memory>
#include <algorithm>
//! A random accessed vector with sorted allocated elements.
//! - Elements must be allocated on heap.
//! - The vector manages the memories of its elements.
template<class T, class Compare = std::less<T>>
class SortedPtrVector
{
public:
SortedPtrVector() {}
//! Add an element, return its index.
int Add(T* element)
{
auto position = std::lower_bound(m_vector.begin(), m_vector.end(),
element, Compare); // Wrong here due to compare smart pointers
auto newPosition = m_vector.insert(position, element);
return newPosition - m_vector.begin();
}
private:
std::vector<std::unique_ptr<T>> m_vector;
};
#包括
#包括
#包括
//! 一种随机访问的向量,具有已排序的已分配元素。
//! - 元素必须在堆上分配。
//! - 向量管理其元素的内存。
模板
类分类dptrvector
{
公众:
SortedPtrVector(){}
//!添加元素,返回其索引。
整数加(T*元素)
{
auto position=std::下限(m_vector.begin(),m_vector.end(),
元素,比较);//由于比较智能指针,此处错误
自动新位置=m_向量。插入(位置,元素);
返回newPosition-m_vector.begin();
}
私人:
std::向量m_向量;
};
如何实现Add功能?非常感谢。不用
std::less
您可以像这样实现自己的ptr\less
:
template< typename T >
class ptr_less
{
typedef bool result_type;
bool operator ()( T const& left, T const& right ) const
{
return *left < *right;
}
};
模板
类ptr_少
{
typedef bool result_type;
布尔运算符()(T常量和左、T常量和右)常量
{
返回*左<*右;
}
};
一般实现还必须检查空指针
另一种方法是使用boost::ptr_vector
而不是std::vector
auto position = std::lower_bound(m_vector.begin(), m_vector.end(),
element, Compare);
这显然是错误的<代码>比较是一种类型,而不是对象
您可以将lambda与Compare
对象一起使用。所以我认为这应该有效:
Compare cmp;
auto comparer = [&](std::unique_ptr<T> const & a, std::unique_ptr<T> const & b)
{
return cmp(*a, *b); //use cmp here!
};
std::unique_ptr<T> uniqElem(element);
auto position = std::lower_bound( m_vector.begin(),
m_vector.end(),
uniqElem, //not element!!
comparer);
如果使用std::unique_ptr
作为参数类型,请注意以下几点:
v.Add(new T()); //will not work
v.Add(std::unique_ptr<T>(new T()); //will work
std::unique_ptr<T> item(new T());
v.Add(item); //will not work
v.Add(std::move(item)); //will work
v.Add(新的T())//行不通
v、 Add(std::unique_ptr(new T());//将起作用
标准::唯一的ptr项目(新的T());
v、 添加(项);//将不起作用
v、 添加(std::move(item));//将起作用
这一切都是因为std::unique\u ptr不可复制,但它是可移动的。除了为智能指针实现比较的问题之外,如果必须对元素进行排序,为什么不使用map/multimap而不是向量呢?为了比较,std::less比较对象指针的包装不会指向使用std::less work吗?我想要元素将被随机访问。它们作为树项放在树节点下,就像文件夹结构一样。添加新文件时,我首先在树节点下找到其位置(行索引),然后将其添加到该位置。谢谢!是否需要使用特殊添加(std::unique_ptr元素)函数?似乎没有unique_ptr的公共副本构造函数。@user1899020:是的,您这样调用它:
v.Add(std::unique_ptr(new T());
。如果您编写此命令,它将不起作用:std::unique_ptr item(new T());v.Add(item)
。但是如果您编写此命令,它将起作用:v.Add(std::move(item))
。这都是因为std::unique_ptr
是不可复制的,但它是可移动的。它太麻烦了吗?我可以叫v.Add(new T())?@user1899020:Edite我的答案。
v.Add(new T()); //will not work
v.Add(std::unique_ptr<T>(new T()); //will work
std::unique_ptr<T> item(new T());
v.Add(item); //will not work
v.Add(std::move(item)); //will work