我想制作一个管理大对象的容器,它可以在复制构造和复制分配上执行深层复制.
template <class TBigObject>
class Container : public std::vector< std::shared_ptr<TBigObject> >
{
public:
Container(int nToAllocate){ /* fill with default constructed TBigObjects */ }
Container(const Container& other){ /* deep copy */ }
Container(Container&&) = default;
Container& operator = (const Container& population){ /* deep copy */ }
Container& operator = (Container&&) = default;
};
Run Code Online (Sandbox Code Playgroud)
我想知道违约是什么:
Container(Container&&) = default;
Container& operator = (Container&&) = default;
Run Code Online (Sandbox Code Playgroud)
成员实际上做了.
如果我打电话:
Container<int> makeContainer()
{
...
}
Run Code Online (Sandbox Code Playgroud)
并在以下位置设置调试断点:
Container<int> moveAssigned;
moveAssigned = makeContainer(); // EDIT corrected thanks to …Run Code Online (Sandbox Code Playgroud) 用户定义的容器是std :: vector的包装器,是继承还是包含std :: vector?
我有一个应该是容器的类.我看到两个选择:
1)从向量继承2)有一个私有成员向量并覆盖所有向量函数,使我的容器充当向量
我不确定这只是一个风格问题,还是一种方式从根本上比另一种更好?
我想要添加的额外功能很少,很少有数据成员和函数.通常,使用向量中的数据将是方便的功能.
std::stack我的代码中有一个,我需要对其进行排序。是否有任何内置功能可以做到这一点?作为std::stack没有std::end。我可以使用std::sort还是必须使用相同的旧方法使用辅助堆栈对原始堆栈进行排序?
是否可以在没有源代码的情况下从C++库扩展类?标题是否足以允许您使用继承?我正在学习C++并且正在进入理论.我会测试这个,但我不知道怎么做.
我是C++和opencv的新手.我写了一个简单的程序,你可以在下面找到,但是当我运行它时,我总是得到一个findContours(img, ctr, CV_RETR_LIST, CV_CHAIN_APPROX_NONE)由类型断言引发的异常抛出失败
OpenCV错误:断言失败(mtype == type0 ||(CV_MAT_CN(mtype)== CV_MAT_CN(type0)&&((1 << type0)&fixedDepthMask)!= 0))在create,file C:\ opencv\modu les\core\src\matrix.cpp,第1466行.
我需要一个代表单个轮廓的类并集成轮廓分析方法.我知道这CONTOUR是一个不同类型的方面,vector<Point>但因为它延伸后者,不CONTOUR应该也是一个vector<Point>类型(并以同样的方式vector<CONTOUR>也是一个vector< vector<Point> >)?我错了吗?
请注意,如果您声明CONTOUR为派生自的类vector<vector<Point>>并Ctr在下面的代码中声明作为CONTOUR对象代替vector<CONTOUR>一切正常工作.
提前谢谢了.
这是我的代码
#include "opencv2/opencv.hpp"
#include <vector>
using namespace cv;
using namespace std;
class CONTOUR : public vector<Point>
{
public:
CONTOUR() : vector<Point>(){ };
CONTOUR(const CONTOUR& orig) : vector<Point> (orig){ };
virtual ~CONTOUR(){ };
CONTOUR& operator=(const CONTOUR& rhs) …Run Code Online (Sandbox Code Playgroud) 假设我有一个代表自动机的类,其状态为编号(using state_t = unsigned),其过渡也编号(using transition_t = unsigned).当然,在某些时候我最终搞乱,因为有些电话transition_t和state_t属于同一类型,所以编译器不执行(语义)类型的安全性.通过使用由tag(struct transition_tag {}; struct state_tag {};)模板化的小类,这很容易解决,所以现在transition_t和state_t不兼容,好!
/// Lightweight state/transition handle (or index).
template <typename Tag>
struct index_t_impl
{
using index_t = unsigned;
constexpr index_t_impl(index_t i)
: s{i}
{}
// Disallow index1_t i{index2_t{42}};
template <typename T>
index_t_impl(index_t_impl<T> t) = delete;
bool operator==(index_t_impl t) const
{
return s == t.s;
}
// Disallow index1_t{42} == index2_t{42};
template <typename T>
bool operator==(index_t_impl<T> t) …Run Code Online (Sandbox Code Playgroud) 我意识到有更好的编译器,但我更喜欢坚持使用vc6,每隔一段时间,我发现奇怪的吸虫,并想知道这个是本地化我的安装或每个人的.
请记住,有问题的编译器是:
Microsoft(R)32位C/C++优化编译器版本12.00.8168 for 80x86
(链接器等不相关,因为这个问题似乎不涉及它们)
试图声明一个继承自std :: vector的类,它可以正常工作,除非您还尝试特定于定义第二个模板参数:
10: class foo : public std::vector<LPTSTR,std::allocator<LPTSTR>>
11: {
12: public:
13: foo();
14: ~foo();
15: };
Run Code Online (Sandbox Code Playgroud)
不编译并给出该错误:C:\ FOO\foo.h中(10):错误C2143:语法错误:缺少 '>' 前 '{'
现在,真正的问题是,为什么这是合法的:
10: class foo : public std::vector<LPTSTR,std::allocator<LPTSTR>>>
11: {
12: public:
13: foo();
14: ~foo();
15: };
Run Code Online (Sandbox Code Playgroud)
注意第>10行末尾的额外内容......我想知道的是:
我试图尽可能多地研究这个问题,但是大多数程序员都知道在线搜索并不是那么容易,因为搜索引擎似乎远没有限制甚至没有正规表达式搜索它会成为命中注定和/或人气竞赛(是一个受欢迎的主题,足以在谷歌上排名等).
我提前感谢你对这个问题的提示(甚至没有提示).我试着回答别人的问题,即使这对我来说似乎很荒谬,并且试着记住知识总是从缺乏知识开始.
在C++ 11中,2D矢量在时间方面如何对抗1D向量?
在给定的2D矢量中,所有内部矢量具有相同的大小.
例如:
std::vector<std::vector<int>> X{10, std::vector<int>(4)};
Run Code Online (Sandbox Code Playgroud)
VS
std::vector<int> Y(40);
Run Code Online (Sandbox Code Playgroud)
当随机访问元素时,矢量的哪个化身会表现得更好?
性能在我的应用中至关重要
我需要一些工作方式std::experimental::dynarray,所以一个数组的大小是在运行时决定的.
所以我考虑使用包装类std::vector,提供其所有功能,但没有可能调用resize,reserve或push_back.简而言之,所有改变其大小的方法(如果我错过了其中的一些,请记住我).
所以我开始写这个课:
CCVector.hpp:
template <typename T>
class CCVector{
public:
CCVector(size_t size);
T &operator[](typename std::vector<T>::size_type idx);
private:
std::vector<T> v;
};
Run Code Online (Sandbox Code Playgroud)
CCVector.cpp:
template<typename T>
CCVector<T>::CCVector(size_t size) : v(size){}
template<typename T>
T& CCVector<T>::operator[](typename std::vector<T>::size_type idx){
return v[idx];
}
Run Code Online (Sandbox Code Playgroud)
但我这一点,我认为我必须重新实施我需要的every方法std::vector!例如begin,end,size等等,我不知道如何实现所有的人......此外,这是非常糟糕的维护:当我需要一个新的方法从std::vector我需要在重新实现它CCVector.
所有这些因为我想在运行时使用固定大小的数组.如何在不使用非标准的情况下解决这个问题std::experimental::dynarray?
我定义一个模板类如下
template <class T>
class MyVector : public std::vector<boost::shared_ptr<T>>
{
public:
typedef MyVector::iterator MyIter;
};
Run Code Online (Sandbox Code Playgroud)
我在 typedef 处收到此错误MyIter
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Run Code Online (Sandbox Code Playgroud)
我正在使用 VS 2010 进行编译,我已确保包含vector和boost::shared_ptr存在。如果我删除模板T并将其替换为int所有内容,则编译不会出现错误。
我缺少什么?我想定义一个模板类并 typedef 迭代器。
我对课程有疑问
// Using private inheritance
class CardPile : private vector<Card*> {
public:
CardPile ();
virtual ~CardPile ();
void add (Card* card);
void add (CardPile & otherPile);
void remove (Card* card);
void shuffle ();
Run Code Online (Sandbox Code Playgroud)
"私人载体<卡*>"究竟是什么意思?这是否意味着我继承了vector类的所有公共成员?我是否也可以访问矢量私人会员?如果它受到保护或公开而非私密,会如何变化?澄清将不胜感激