如何将存储泛型 A 的共享指针的向量复制到集合?
#include <memory>
#include <vector>
#include <set>
template <typename T>
class A {
public:
T a;
A(const T &a_) : a(a_) {}
};
template <typename Type>
class comp
{
bool operator() (const Type & a1, const const Type &a2) const {
return (a1->get().a < a2->get().a);
}
};
不幸的是,这种结构不起作用
int main() {
using v = std::vector <std::shared_ptr <A <double> >> ;
using s = std::set <std::shared_ptr <A <double> >,comp <std::shared_ptr <A <double> > > >;
s.insert(v.begin(), v.end()); //Trying to insert
return 0;
}
并发生以下错误(VS 2015):
error C2059: syntax error: '.'
谢谢你的帮助。
更新的解决方案:
感谢您对使用提出意见,更新后的有效解决方案是:
template <typename T>
class A {
public:
T a;
A(const T &a_) : a(a_) {}
};
template <typename Type>
class comp
{
public:
bool operator() (const Type & a1, const const Type &a2) const {
return (a1.get()->a < a2.get()->a);
}
};
int main() {
std::vector <std::shared_ptr <A <double> >> v;
std::set <std::shared_ptr <A <double> >, comp <std::shared_ptr <A <double> > > > s(v.begin(), v.end());
}
当你说这样的话:
using v = std::vector <std::shared_ptr <A <double> >> ;
然后你创建一个新的类型名称v
,它是一个向量。
然后,您可以创建该类型的对象:
v av; // av is an object of type v
并可能对它们调用方法:
av.begin()
这是否有意义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句