简体   繁体   English

定义类的向量的成员变量的引用

[英]Define a reference of the member variable of a vector of a class

This code explains an example of what I want to do: 这段代码说明了我想做的一个例子:

class Object
{
public:
    int data;

    Object() : data(0) {}
    Object(int data) : data(data) {}
};

int dataSum(vector<int>& dataReference)
{
    int sum = 0;
    for(int i = 0; i < dataReference.size(); i++)
    {
         sum += dataReference[i];
    }
    return sum;
}

int main()
{
    vector<Object> myObject(5);
    int mySum = dataSum(myObject.data); // this line does not work

    return 0;
}

It's obvius that this can be done easier making dataSum a member function of Object, but this is not what I am asking. 使dataSum成为Object的成员函数可以轻松实现,但这不是我要的。 How can I just reference myObject.data? 如何仅引用myObject.data? Or at least doing the same with a pointer if it cannot be done with a reference. 或者,如果不能通过引用完成,至少要对指针执行相同的操作。

Edit: Some people have told me that the question is not clear. 编辑:有人告诉我这个问题不清楚。 What I want to do is reference every integer of myObject[i].data putting them in a vector<int> reference. 我想做的是引用myObject[i].data每个整数,并将它们放入vector<int>引用中。 Sorry also If I have put some grammatical error, English is not my native language. 也很抱歉,如果我在语法上有误,英语不是我的母语。

Do you want this? 你想要这个吗?

int dataSum(const vector<Object>& dataReference)
{
  int sum = 0;
  for (int i = 0; i < dataReference.size(); i++)
  {
    sum += dataReference[i].data;
  }
  return sum;
}

int main()
{
  vector<Object> myObject{ 1,2,3,4 };
  int mySum = dataSum(myObject); // mySum will be 1+2+3+4 (=10) here.

  return 0;
}

or better: 或更好:

int dataSum(const vector<Object>& dataReference)
{
  int sum = 0;
  for (const auto & object : dataReference)
  {
    sum += object.data;
  }
  return sum;
}

Why can't you use std::accumulate ? 为什么不能使用std::accumulate

vector<Object> myObject(5) { 1, 2, 3, 4, 5 };
int mySum = std::accumulate(myObject.begin(), myObject.end(), 0, 
            [](int init, const Object& obj)
            {
                return init + obj.data;
            }); // mySum = 15

This removes the need for the function in the first place and achieves what you want with a simple function call! 首先,无需使用该函数,并通过简单的函数调用即可实现所需的功能!

If you wanted to do the same thing with a std::vector<int> , then you could just call the same function without a binary predicate. 如果要使用std::vector<int>进行相同的操作,则可以只使用相同的函数而无需二进制谓词。

You are attempting to pass std::vector<Object>& into a method whose signature requires std::vector<int>& . 您正在尝试将std::vector<Object>&传递到签名需要std::vector<int>& If you want to handle a vector of Object types then that's what you need to implement. 如果要处理Object类型的向量,则需要实现。 If you want to use the method you have defined for dataSum then you need to use it like this. 如果要使用为dataSum定义的方法,则需要这样使用。

std::vector<int> numVec = {1, 2, 3, 4, 5};
Object obj;
int sum = obj.dataSum(numVec);

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

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