如何使用向量和整数作为类的构造函数

发布于 2024-12-09 16:53:37 字数 334 浏览 0 评论 0原文

如果我有一个 foo 类,那么

class foo{
   vector <int> vec;
   int someint;

   public:
   foo(number n): someint(n){}


}

我将如何为 class foovector 编写构造函数?此外,我可以使用:

int get_someint() const{

  return someint;
}

返回一个 int,但是向量呢?

If I had a class foo then

class foo{
   vector <int> vec;
   int someint;

   public:
   foo(number n): someint(n){}


}

How would I write a constructor for the vector, for class foo? Moreoever, I can use:

int get_someint() const{

  return someint;
}

To return an int, but what about vectors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞳孔里扚悲伤 2024-12-16 16:53:37

当使用复杂的数据类型时,通常最好使用引用或常量引用,如下所示:

class A
{
public:
    A()  {}  // default construct of A, v_ is empty

    A(const std::vector<int>& source)
      : v_(source)  // construct A, given a vector that is 
                    // copied into v_
    {
    }

    // returns an immutable reference to v_
    const std::vector<int>& get_v() const
    {
       return v_;
    }

    // returns a mutable reference to v_
    std::vector<int>& get_v()
    {
        return v_;
    }
private:
    std::vector<int> v_;
};

示例用法:

A a_no_vec;  // A has an empty vector v_

std::vector<int> src;

src.push_back(16);
src.push_back(19);

A a_with_vec(src);  // A has a vector that is a copy of src

When working with complex data types, its usually best to work with references, or const-references like so:

class A
{
public:
    A()  {}  // default construct of A, v_ is empty

    A(const std::vector<int>& source)
      : v_(source)  // construct A, given a vector that is 
                    // copied into v_
    {
    }

    // returns an immutable reference to v_
    const std::vector<int>& get_v() const
    {
       return v_;
    }

    // returns a mutable reference to v_
    std::vector<int>& get_v()
    {
        return v_;
    }
private:
    std::vector<int> v_;
};

Example usage:

A a_no_vec;  // A has an empty vector v_

std::vector<int> src;

src.push_back(16);
src.push_back(19);

A a_with_vec(src);  // A has a vector that is a copy of src
七禾 2024-12-16 16:53:37

你也可以这样做。出于所有目的,请将 vector 视为普通变量(例如 int),因此您可以编写:

class foo{
   vector <int> vec;
   int someint;

   public:
   foo(number n, vector<int> v): someint(n), vec(v){}
}

它将被内部和外部复制,并且这可能意味着大量的内存副本。这就是为什么对于如此大的对象,使用按引用传递(通过指针或引用)。例如,要返回向量:

vector<int> const& the_vector()
{ return inner_vector; } // no copy

,构造函数也将是 foo(number n, vectorconst& v)。另外,如果在内部您不必存储向量的副本,则可以使用该向量的指针或引用作为成员,而不是向量副本本身,即类是:(

class foo{
   vector <int>& vec;
   int someint;

   public:
   foo(int n, vector<int>& v): someint(n), vec(v){}
}

注意引用) 。与指针相同。

You can do it just the same. For all purposes, consider vector<int> as a normal variable (like an int, for example), so you can write:

class foo{
   vector <int> vec;
   int someint;

   public:
   foo(number n, vector<int> v): someint(n), vec(v){}
}

It will be copied inside and out, and that may mean a lot of memory copies. This is why for such large objects, pass by reference (either by pointer or reference) is used. For example, to return the vector:

vector<int> const& the_vector()
{ return inner_vector; } // no copy

and the constructor would be also foo(number n, vector<int> const& v). Also, if internally you don't have to store a copy of the vector, you can use a pointer or reference to that vector as a member, instead of the vector copy itself, that is, the class being:

class foo{
   vector <int>& vec;
   int someint;

   public:
   foo(int n, vector<int>& v): someint(n), vec(v){}
}

(note the references). The same with pointers.

习惯成性 2024-12-16 16:53:37

您不需要为向量编写构造函数。 Vector 已经有了它的构造函数。您也可以在返回 int 时简单地返回一个向量,例如

const std::vector<int> & getVec()
{
   return vec;
}

You don't need to write a constructor for the vector. Vector already has it's constructors. Also you can simply return a vector as you return your int e.g.

const std::vector<int> & getVec()
{
   return vec;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文