Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C+中访问数组中的相邻元素+;?_C++_Arrays_Operator Overloading - Fatal编程技术网

C++ 如何在C+中访问数组中的相邻元素+;?

C++ 如何在C+中访问数组中的相邻元素+;?,c++,arrays,operator-overloading,C++,Arrays,Operator Overloading,我将实现一个函数,访问给定元素的相邻元素 这是一个简单的例子:在一维数组中,对距给定元素半径距离内的元素求和。C代码将是: int sum(int *elem_ptr, int radius) { int sum = 0; for (int *ptr = elem_ptr - radius; ptr <= elem_ptr + radius; ptr++) { sum += *ptr; } return sum; } int和(int*e

我将实现一个函数,访问给定元素的相邻元素

这是一个简单的例子:在一维数组中,对距给定元素半径距离内的元素求和。C代码将是:

int sum(int *elem_ptr, int radius) {
    int sum = 0;
    for (int *ptr = elem_ptr - radius; ptr <= elem_ptr + radius; ptr++) {
        sum += *ptr; 
    }
    return sum;
}
int和(int*elem\u ptr,int半径){
整数和=0;

对于(int*ptr=elem_ptr-radius;ptr由于数组是一个难题,我将首先使用
std::vector
提供一个解决方案

int sum_vector_neighbor(const std::vector<int>& values, 
                        unsigned int position,  // negative positions are weird. 
                        unsigned int radius)    // negative radii are weird.
{
   if (values.empty())
   {
     return 0;
   }
   const unsigned int size = values.length();
   unsigned int start_index = 0U;
   unsigned int end_index = size - 1;
   if (radius < position)
   {
     start_index = position - radius;
   }
   if ((position + radius) < size)
   {
     end_index = position + radius;
   }
   sum = 0;
   for (; start_index < end_index; ++start_index)
   {
      sum += values[start_index];
   }
   return sum;
}
int sum\u vector\u neighbor(const std::vector&values,
无符号int位置,//负位置很奇怪。
无符号整数半径)//负半径很奇怪。
{
if(values.empty())
{
返回0;
}
常量unsigned int size=values.length();
无符号整数起始索引=0U;
无符号int end_索引=大小-1;
if(半径<位置)
{
起始指数=位置-半径;
}
如果((位置+半径)<尺寸)
{
端部指数=位置+半径;
}
总和=0;
对于(;开始索引<结束索引;++开始索引)
{
总和+=值[开始索引];
}
回报金额;
}
上面的解决方案是使用索引而不是指针,因为索引比指针更容易比较

使用索引的习惯用法可以应用于数组。对于数组版本,您需要额外传递数组的容量,因为当数组转换为指向第一个元素的指针时,会删除“容量”属性


我不建议使用指针,因为必须从指针中减去半径值(这是一个有效的操作)但是,对于所有平台来说,对于小于或大于的不是一个支持的操作。在C++中使用< < /P> < P>,使用<强>迭代器< /St> > <强>容器是方法,而不是像代码示例中所示的那样执行指针运算。 迭代器也比原始索引更可取,因为它使代码与任何容器兼容(例如,集合或映射没有索引)

通过最近的STL,可用于操作迭代器的工具包变得非常通用:

  • std::prev(迭代器,n)
    向后移动迭代器n个列(朝向集合的开头)
  • std::next(迭代器,n)
    向前移动
  • std::distance(a,b)
    计算到迭代器之间的元素数,这对于检查是否超出范围非常有用
使用迭代器还有另一个很好的副作用:您将能够使用标准库中已有的许多算法中的一种,例如从

这是一个实现的开始,它不是完美的,但在大多数情况下都应该可以

#include <functional>
#include <numeric>

int sum(const std::vector<int>& v,
        const std::vector<int>::const_iterator elem,
        size_t radius)
{
    auto first_radius = (std::abs(std::distance(std::cbegin(v), elem)) < radius)
                        ? std::cbegin(v)
                        : std::prev(elem, radius);

    auto last_radius = (std::abs(std::distance(elem, std::cend(v))) < radius)
                       ? std::cend(v)
                       : std::prev(elem, radius);

    return std::accumulate(first_radius, last_radius, 0, std::plus<int>());
}

根据你对语言的熟悉程度,它可以是很多信息。值得一看的是我在这里使用的函数文档,因为这是真正的习惯用法(C++,请注意)。 作为结束语:您一定注意到我在我的示例中使用了

std::vector
。迭代器的好处是:我们可以使这个函数完全不依赖于容器

使用模板,它可以用于地图、数组、集合、自定义容器……你可以说出来。但这一次应该足够了……如果你感兴趣,请不要介意问我有关模板技巧的问题


玩得开心!

为什么不将数组的长度作为参数传入并在sum函数中执行边界检查?我建议使用
std::acculate
()相反,当您传递给该函数的所有代码都是“代码”INT*<代码>时,如何确定数组的任何内容?您没有关于数组开始或结束的信息。在C++中,可以通过使用<代码> STD::vector < /代码>来减少这些问题。不需要传递数组的指针或数组的容量。/代码>通过引用。更好的是,不要传递固定类型的容器和索引,而是在两个迭代器的类型上对该方法进行参数化。这样它将是通用的,并且只需要两个迭代器参数。
std::vector<int> x = { 0, 1, 0, 1, 0, 1};
const int a = sum(x, std::next(std::cbegin(x), 3), 15);
const int b = sum(x, std::prev(std::cend(x), 1), 2);