Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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++ 如何在std::数组中读取/写入或迭代特定范围的元素?_C++_Arrays - Fatal编程技术网

C++ 如何在std::数组中读取/写入或迭代特定范围的元素?

C++ 如何在std::数组中读取/写入或迭代特定范围的元素?,c++,arrays,C++,Arrays,我想在更大的内存中读/写一些特定范围的内存,基本上我想这样做: std::array<int, 1024> big_array; unsigned short int offset = 128; template<std::size_t SIZE> void ReadArray(std::array<int, size>& big_array_with_address_offset) { // ... some code in here }

我想在更大的内存中读/写一些特定范围的内存,基本上我想这样做:

std::array<int, 1024> big_array;
unsigned short int offset = 128;

template<std::size_t SIZE>
void ReadArray(std::array<int, size>&  big_array_with_address_offset)
{
  // ... some code in here 
}

ReadArray(big_array + offset); //--> This doesn't work
注意1:我能够在函数参数中使用较小的
std::array
的唯一方法是使用
template
,否则,由于大小不兼容,会出现编译错误


注2:我不想使用
std::vector
执行此操作,因为我需要静态内存而不是动态内存。

将迭代器发送到数组的开头,而不是发送到数组中


FindMinElement函数接收到的是指针,而不是数组。在迭代器中发送是最接近的等价项。

偏移量
元素处的数组地址传递给
findmineelement
函数。有关如何操作,请参见下面的示例:

#include<iostream>
#include<array>
#include<algorithm>
using namespace std;

std::array<int, 1024> big_array;
unsigned short int offset = 128;

int FindMinElement(int *array)
{
    // Min element from range: 128 to 384
    return *std::min_element(array, array+256);
}

int main(void)
{  
    int counter = 0;
    for(auto & i : big_array) //populates the array from 0 to 1023
    {
        i = counter++; 
    }       
    cout << "Min element in the range 128 to 384 is: ";
    cout << FindMinElement(big_array.begin() + 128) << endl;
}

我终于明白了这一点,这就是我所要达到的目的,我的意思是使用迭代器和现代C++的原始指针,P.W.的答案接近于我想要实现的:

short int FindMinElement(std::array<short int, 1>::iterator it);
{
    // Min element from range: 128 to 384
    return *std::min_element(it, it + 128);
}

int main()
{
    std::array<int, 1024> big_array{{0}}; 
    unsigned short int offset = 128;
    short int minval = 0;

    // Asuming big_array is populated in some other place before calling FindMin..()
    minval = FindMinElement(std::begin(big_array) + offset);
    return 0;
}
short int findmineelement(std::array::iterator it);
{
//最小元素范围:128到384
return*std::minu元素(it,it+128);
}
int main()
{
数组大数组{{0};
无符号短整数偏移=128;
短int minval=0;
//Asuming big_数组在调用FindMin之前在其他地方填充()
minval=findmineelement(标准::开始(大数组)+偏移量);
返回0;
}
我希望能够在不使用原始指针的情况下操作数据,然后解决方案是使用迭代器作为参数,而不是指针


这也解决了NOTE1中提到的编译错误,我在将较大的std::array引用传递给较小的std::array函数参数时遇到了编译错误。它们允许您定义要从中复制的范围。它们还与
std::copy
一起使用,因此您可以使用它将元素从一个数组复制到另一个数组。这几乎是对你以前的问题的一种欺骗:(C++ 20)可能会帮助,否则传递迭代器而不是容器。如果你想通过容器,那么也要传递偏移量。嗨,谢谢你的回答,但是我是这样做的,我提到我不使用这个方法,因为它使用原始指针,我想用更现代的C++方法。与使用迭代器类似,在这种情况下,可以使用
std::array
begin()
迭代器。我已经更新了答案来反映这一点。嗨,我知道它正在接收一个指针,这就是我在解释中所说的,我能够使用原始指针实现预期的行为,但我希望像第一个原型那样使用数组和迭代器
Min element in the range 128 to 384 is: 128
short int FindMinElement(std::array<short int, 1>::iterator it);
{
    // Min element from range: 128 to 384
    return *std::min_element(it, it + 128);
}

int main()
{
    std::array<int, 1024> big_array{{0}}; 
    unsigned short int offset = 128;
    short int minval = 0;

    // Asuming big_array is populated in some other place before calling FindMin..()
    minval = FindMinElement(std::begin(big_array) + offset);
    return 0;
}