简体   繁体   English

c ++从非朋友外部实用程序函数中循环一个类私有unique_ptr数组,而不使用c风格的for循环

[英]c++ loop through a class private unique_ptr array from non-friend external utility function without using c-style for loop

I've got a class with:我有一堂课:

class vector_class {
private:
    std::unique_ptr<int[]> my_vector;
    int size_;

public:
    auto at(int) const -> int; // returns value at subscript
    auto at(int) -> int&;
    auto size() -> int; // returns vector size
}

I've been asked to construct an external function that takes 2 of these objects and returns the inner_product.我被要求构建一个外部函数,该函数采用其中 2 个对象并返回 inner_product。 Problem is that I have the following restrictions:问题是我有以下限制:

  1. Can't add any public functions.不能添加任何公共功能。
  2. Shouldn't use friendship不应该使用友谊
  3. Shouldn't use c-style for loops (ie should use algorithms).不应该使用 c 风格的循环(即应该使用算法)。
  4. Can't use any stl containers.不能使用任何 stl 容器。

I can't use for_each because I don't have an iterator (since the vector is private non-friend).我不能使用 for_each 因为我没有迭代器(因为向量是私有的非朋友)。 I can't do for (i = 0; i < a.size(); ++i) ... (since no basic for loop).我不能 for (i = 0; i < a.size(); ++i) ... (因为没有基本的 for 循环)。

I guess I could write a custom iterator class but this is a lot of work and wondering if I've missed an easier solution... Any suggestions?我想我可以编写一个自定义迭代器类,但这是很多工作并且想知道我是否错过了一个更简单的解决方案......有什么建议吗?

Iterators were designed to emulate much of the behavior of pointers, so much that all pointers can be used as iterators.迭代器旨在模拟指针的大部分行为,以至于所有指针都可以用作迭代器。

So if you have an array, and the number of elements in the array, you can use a pointer to the first element of the array as the begin iterator.因此,如果您有一个数组,以及数组中的元素数量,您可以使用指向数组第一个元素的指针作为begin迭代器。 And a pointer to one element beyond the last element (ie begin + size ) as the end iterator.以及指向最后一个元素之外的一个元素的指针(即begin + size )作为end迭代器。 Then you can use these "iterators" with any normal standard function expecting an iterator pair.然后,您可以将这些“迭代器”与任何需要迭代器对的普通标准函数一起使用。

For your specific case, &my_vector[0] as begin, and &my_vector[size_] as end.对于您的具体情况, &my_vector[0]作为开始, &my_vector[size_]作为结束。

Now, due to the limitations and requirements of your exercise, the simplest solution is probably to get these pointers through the public at() function.现在,由于您的练习的限制和要求,最简单的解决方案可能是通过公共at()函数获取这些指针。 Or if the class has an overloaded operator[] you can use that instead:或者,如果该类有一个重载的operator[]你可以使用它来代替:

inner_product(&i[0], &i[i.size()], &j[0]);

Because there are two overloads of the at function (and probably of the [] operator if it exists) we might need to specifically ask for the overload that returns a reference, if the compiler doesn't select the correct overloads to begin with.因为at函数有两个重载(如果存在的话,可能还有[]运算符的重载),如果编译器没有选择正确的重载开始,我们可能需要专门要求返回引用的重载。

This can be done by defining our own temporary reference variables:这可以通过定义我们自己的临时引用变量来完成:

auto const& first_begin  = i[0];
auto const& first_end    = i[i.size()];
auto const& second_begin = j[0];

auto result = inner_product(&first_begin, &first_end, &second_begin, 0);

Or, since it seems that the at function have a proper reference-returning overload, you can use that instead of [] :或者,由于at函数似乎具有适当的返回引用重载,因此您可以使用它来代替[]

auto result = inner_product(&i.at(0), &i.at(i.size()), &j.at(0), 0);

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

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