简体   繁体   English

setter和getter在C ++中用于vector和list

[英]setter and getter for vector and list in c++

how could i use setter and getter for variable declared with vector and list . 我该如何使用setter和getter来使用vector和list声明变量。

class abc{

    private:
       vector<number> test;
       list<numb> test2; 

    public:
       void settest(const vector<number> &x){
               test=x;

         }
       vector<number> gettest(){

         return test;
       }

       void settest2(const list<numb> &y){
               test2=y;

         }
       vector<numb> gettest2(){

         return test2;
       }


};
 int main(){
   abc object;

 }

i tried to define set and get method for vector and list like above is it correct?please correct it. 我试图为上述向量和列表定义set和get方法,是否正确?请更正它。 and how to call set and get function from main??. 以及如何从main调用set和get函数?

Getters for collections is not a problem, but setter is often not the best way to do things. 收集器的吸气剂不是问题,但是坐便器通常不是做事情的最佳方法。 Setting whole collection is not very efficient as the content has to be copied. 设置整个集合不是很有效,因为必须复制内容。 Which is especially inefficient if you really just want to append an element, because you'll end up copying the whole content just for what could have been a much simpler operation. 如果您真的只想添加一个元素,那么这特别没有效率,因为您最终将只为了原本可以简单得多的操作复制整个内容。 But which operations you should provide depends on the semantics of your class, so I can't give you any general advice. 但是您应该提供哪种操作取决于类的语义,因此我无法提供任何一般性建议。

  1. Getter: For non-trivial data structures the most appropriate form is a const reference. Getter:对于非平凡的数据结构,最合适的形式是const引用。 This allows the caller to look into the vector including running any non-mutating algorithms on it while avoiding copying anything: 这使调用者可以查看向量,包括在向量上运行任何非变异算法,同时避免复制任何内容:

     vector<number> const &gettest() const { return test; } // ^^^^^ even on const abc // ^^^^^^^ read-only subinterface list<numb> const &gettest2() const { return test2; } // ^^^^^ even on const abc // ^^^^^^^ read-only subinterface 

    Note that I also marked the methods const. 请注意,我还标记了方法const。 This allows calling them also on const abc & so you can return abc by const reference from some other interface. 这也允许在const abc &上调用它们const abc &因此您可以从其他接口通过const reference返回abc

  2. Setter: There is a difference between C++03 (without move semantics) and C++11 (with move semantics). 设置者:C ++ 03(无移动语义)和C ++ 11(无移动语义)之间是有区别的。

    1. C++03: Since C++03 only has copying semantics and copying vector is expensive, you should pass the argument again by const reference. C ++ 03:由于C ++ 03仅具有复制语义,并且复制向量很昂贵,因此应通过const引用再次传递参数。

        void settest(const vector<number> &x){ test=x; } void settest2(const list<numb> &y){ test2=y; } 

      No change here so far. 到目前为止,这里没有变化。

    2. C++11: The move semantics changes things a little. C ++ 11:move语义会稍微改变一点。 The above code can't utilize move semantics in the assignment, because it has constant lvalue reference and therefore can't move from it. 上面的代码不能在赋值中使用移动语义,因为它具有恒定的左值引用,因此不能从中移出。 You could add an rvalue reference overload, but it's actually easier to pass by value : 您可以添加右值引用重载,但实际上通过传递更容易:

       void settest(vector<number> x) { test=std::move(x); } // request move ^^^^^^^^^ void settest2(list<numb> y) { test2=std::move(y); } // request move ^^^^^^^^^ 

      C++11 automatically moves from temporaries, but function argument is not a temporary, so you have to move from it. C ++ 11自动从临时位置移开,但是函数参数不是临时的,因此您必须从临时位置移开。 This way if the source can be moved from, there are no copies, only two moves and if it can't, there is just one copy to the argument, all with a single overload. 这样,如果可以从中移出源,则没有副本,只有两步,如果不能,则只有一个副本到参数,所有都带有一个重载。

    3. Generic: All collections have a member template assign that allows setting them from two iterators. 通用:所有集合都有一个成员模板assign ,该成员模板允许从两个迭代器进行设置。 So you can write a template setter allowing to set it from any type of collection and only from part of collection: 因此,您可以编写一个模板设置器,以允许从任何类型的集合中进行设置,并且只能从部分集合中进行设置:

       template <typename IteratorT> void settest(IteratorT begin, IteratorT end) { test.assign(begin, end); } 

      The list version is the same except for setting different member. 列表版本相同,只是设置了不同的成员。 This is the C++ standard way of working with ranges, but it requires the caller to explicitly refer to the collection twice. 这是C ++处理范围的标准方法,但是它要求调用者显式地两次引用该集合。 For convenience the following can be added (C++03): 为了方便起见,可以添加以下内容(C ++ 03):

       template <typename CollectionT> void settest(CollectionT const &x) { test.assign(x.begin(), x.end()); } 

      In C++11 there are non-member functions std::begin and std::end functions that allow other classes to be made into ranges (notably accepting also plain old arrays), so there you should instead write: 在C ++ 11中,有非成员函数std::beginstd::end函数允许将其他类划分为范围(特别是也接受普通的旧数组),因此应该改写为:

       template <typename CollectionT> void settest(CollectionT const &x) { test.assign(std::begin(x), std::end(x)); } 

      If you don't have C++11, but have Boost , you may want to use boost::begin and boost::end from Boost.Range instead. 如果您没有C ++ 11,但是拥有Boost ,则可能要使用Boost.Range中的 boost::beginboost::end

      Note though that this does not allow move semantics, because you can only move from the same type and this assumes the types may be different. 请注意,尽管这不允许移动语义,因为您只能从同一类型移动,并且假定类型可能不同。 There is no harm in providing both the same-type moving overload and the generic overload. 同时提供相同类型的移动过载和通用过载都没有害处。

  3. Mutators: Have a look at mutating methods list and vector have (both have the same set, actually; the only difference is that vector can be directly indexed) and think which ones make sense for your class to provide. 变异器:看看变量方法列表和向量具有的变量方法(实际上,它们具有相同的集合;唯一的区别是,向量可以直接索引),并考虑为您的类提供哪些变量是有意义的。 If it makes sense to simply append elements to the list, it is much more efficient to provide access to the push_back and insert (possibly with fixed position) members than going through plain setter. 如果仅将元素追加到列表是有意义的,则提供对push_backinsert (可能具有固定位置)成员的访问比通过简单的setter更为有效。 Eg 例如

     void appendtest(number x) { test.push_back(x); } template <typename IteratorT> void appendtest(IteratorT begin, IteratorT end) { test.insert(test.end, begin, end); } 

    and similar for the other methods. 与其他方法类似。

There is no need to pass full Vector, you can just pass index and return value. 不需要传递完整的Vector,您可以传递索引并返回值。 For adding new value, pass value, and your set function will take care of adding in vector. 要添加新值,传递值和您的set函数,将要添加矢量。

    class abc{

private:
   vector<number> test;
   list<numb> test2; 

public:
   void settest(const vector<number> &x){
           test=x;

     }
   const vector<number>& gettest() const{

     return test;
   }

   void settest2(const list<numb> &y){
           test2=y;

     }
   const list<numb>& gettest2() const{

     return test2;
   }

}; };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM