我不明白这是怎么回事。毕竟,如果v是std :: vector,那么 v [0]定义得很好 - 但你建议定义一个v [0] $的类b $ b定义不明确。所以它不可能是一个透明的替代品。 [见 http://www.gotw.ca/resources/clcm.htm 有关的信息] [comp.lang.c ++。moderated。第一次海报:做到这一点! ] 提供所有迭代器 - 作为typedef: 模板< typename T> 类Ovector { public: typedef T value_type; typedef vector< ; T> :: iterator iterator; typedef vector< T> :: const_iterator const_iterator; // ...与其他人相同(reverse_iterator等) iterator begin(){return d_vector.begin(); } $ / $ const_iterator begin()const {return d_vector.begin(); } iterator end(){return d_vector.end(); } $ / $ const_iterator end()const {return d_vector.end(); } // ...等 我怀疑唯一让你头疼的是 区分构造函数: 模板< typename迭代器> Ovector(迭代器开始,迭代器结束) 和: 模板< typename T> Ovector(int size,T value) 当T是int(嗯,那将是size_t,但是让我们把它变成int $ / b $ b来简化事情)。使用vector,因为它是 语言的一部分,编译器可以做一些魔术来消除这个问题的歧义。但是对于你正在创建的一个类,我想唯一的 解决方案是为所有 可能的积分tyepes提供专门的类定义! (哎哟:-() 当然,另一个解决方案是不提供那些(毕竟,你的 构造函数自然会期望一个额外的参数,如果你按照我的建议 - 下标在任意范围内的方式来实现。 但这对于矢量来说是否足够透明了?我 猜你只能决定。 HTH, Carlos - [见 http://www.gotw。 ca / resources / clcm.htm 有关的信息] [comp.lang.c ++。版主。第一次海报:做这个!] Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1.The only change this will have is in loops and when the vector returnspositions of elements etc. I am calling this uovec at the moment (forUnit-Offset VECtor). I want the class to respond correctly to all usage ofSTL containers and algorithms so that it is a transparent replacement forstd:vector. The options seems to be: 1) deriving publicly from std::vector and then define operator(), but Ibelieve that std::vector is not meant to be used as a base class so novirtual destructor (and possibly other gotchas) though in this case Iwouldn''t need to store any members in the derived class so maybe this wouldmake no difference? 2) store a std::vector inside and use it as necessary - this appears to bethe preferred option. 3) other? Deriving privately from std::vector and an interface? In any case I suspect housekeeping will be required to keep STL containersand algorithms happy. A couple of years ago I was asking these questions on the list and gotseveral helpful replies but got distracted by other tasks and dropped C++.Am now back (and better prepared) and I was interested in a critique of thisfirst attempt. It uses (2) but I''m not sure if all the required STLbehaviour is covered so I have the getout clause of direct access to thecontained std::vector. I would prefer to avoid doing this. template <class T>/////////////////////////////////// uovec - a std:vector<T> but with unit-offset via operator ()// can add other stuff to make it exactly like std::vector// can call v.foo() or use vref() if necessary but not very elegant/////////////////////////////////class uovec {typedef std::vector<T> vT; static const iter offset = 1;vT v; public:// simple constructorsuovec ( const iter s ) : v( s ) {}uovec ( const iter s, const T fill ) : v( s, fill ) {} // copy constructors and assignments will be required but not given here // access from 1 to ninline T& operator() ( const iter i ) { return v[i-offset]; }inline const T& operator() ( const iter i ) const { return v[i-offset]; } inline vT& vref () { return v; } // return reference to v incase complicated STL required... nastyinline const vT& vref () const { return v; } // return const reference toconst v // vector iteratorstypename vT::iterator begin() { return v.begin(); }typename vT::iterator end() { return v.end(); }typename vT::const_iterator begin() const { return v.begin(); }typename vT::const_iterator end() const { return v.end(); } typename vT::reverse_iterator rbegin() { return v.rbegin(); }typename vT::reverse_iterator rend() { return v.rend(); }typename vT::const_reverse_iterator rbegin() const { return v.rbegin(); }typename vT::const_reverse_iterator rend() const { return v.rend(); } };Michael _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ _/ _/ _/_/_/ Hopkins Research Ltd_/ _/ _/ _/_/_/_/_/ _/_/_/ http://www.hopkins-research.com/_/ _/ _/ _/_/ _/ _/ _/ ''touch the future'' _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ [ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ] 解决方案 I haven''t thought too much about it, but I think I would deriveprivately and use the using directive to forward all those things thatare the same: template <typename T, int offset = 1>class uovec : std::vector<T>{public:using std::vector<T>::begin;using std::vector<T>::end;using std::vector<T>::rbegin;using std::vector<T>::rend; T& operator[](int i) { return std::vector<T>::operator[](i - offset); }T const& operator[](int i) const { returnstd::vector<T>::operator[](i - offset); }}; Notice I also made offset a template argument to make this even moreinteresting, you can then have any offset you like. I also preferredusing the subscript operator over operator(), but that''s just me. Just be sure to open the standard and provide a complete interface. HTHNeal [ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ] I don''t see how that is possible. After all, if v is a std::vector, thenv[0] is well defined--but you''re proposing to define a class in which v[0]is not well defined. So it can''t possibly be a transparent replacement.[ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ] Provide all the iterators -- as typedefs: template <typename T>class Ovector{public:typedef T value_type;typedef vector<T>::iterator iterator;typedef vector<T>::const_iterator const_iterator;// ... same for others (reverse_iterator, etc.) iterator begin() { return d_vector.begin(); }const_iterator begin() const { return d_vector.begin(); }iterator end() { return d_vector.end(); }const_iterator end() const { return d_vector.end(); }// ... etc.The only thing I suspect is going to give you a headache is tomake the distinction between the constructors: template <typename Iterator>Ovector (Iterator begin, Iterator end) And: template <typename T>Ovector (int size, T value) When T is int (well, that would be size_t, but let''s make it intto simplify things). With vector, because it is part of thelanguage, the compiler is allowed to do some magic to disambiguatethe issue. But with a class that you''re creating, I guess the onlysolution would be to provided specialized class definitions for allpossible integral tyepes! (ouch :-( ) Sure, another solution would be not to provide those (after all, yourconstructor would naturally expect an extra parameter if you do itthe way I suggested -- subscript being within an arbitrary range).But would that be a transparent enough replacement for vector? Iguess only you can decide that. HTH, Carlos-- [ See http://www.gotw.ca/resources/clcm.htm for info about ][ comp.lang.c++.moderated. First time posters: Do this! ] 这篇关于首先尝试从1到n的std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 07:15