我有两个整数数组[为简单起见,调用一个A和一个B]和一个给定的整数值。我想用数组做两件事:
对不起,如果这不是很清楚,我会尽力解释。无论如何,我对数组很新,并且不太熟悉如何与它们进行交互(根据你在程序中包含的内容,似乎还有很多方法可以实现)。对此有哪些好的参考?
我想使用if循环来搜索数组中的给定整数' A'如果这是真的,那么从B中提取相应的值基于A。中数组元素的位置。
我目前有这样的事情:
#include <iostream>
#include <time.h> // time
#include <stdlib.h> // srand, rand
using namespace std;
int main ()
{
srand(time(NULL)); // Initiate seed
int pointPos = rand() % 6 + 1; // Generate random point
int velPoint[6] = {1, 2, 3, 4, 5, 6}; // velocity-point array
int accel[6] = {14, 17, 23, 32, 41, 59}; //
if (pointPos...) //This is where I want to check velPoint array to see if the random point is within the array
// Here is where I want to pull the element from accel that is in the same position as the matching element in velPoint
}
答案 0 :(得分:0)
实际上就像你说的那样,虽然技术上是一个&#34; if loop&#34;不存在。
const int num = 6;
int velPoint[num] = {1, 2, 3, 4, 5, 6}; // velocity-point array
int accel[num] = {14, 17, 23, 32, 41, 59}; //
srand(time(NULL)); // Initiate seed
int pointPos = rand() % 6 + 1; // Generate random point
int covalue;
for( int i = 0; i < num; i++ ){ // loop through all the elements
if( velPoint[i] == pointPos ){
covalue = accel[i];
break;
}
}
你也可以使用内置函数std :: find() 这里有一个很好的例子:std::find()
您可能会考虑切换到向量或更新数据结构,可能会使用对或您自己的结构对数据进行分组。
答案 1 :(得分:0)
对于一个简单易懂的测试,我们可以尝试这样的事情:
// This function takes an array of N elements of type T, and a value of type T.
// (In your case, it will take parameters "const int (&arr)[6]" and "const int& val".)
// Array and value are passed by const reference, as this function doesn't modify either.
template<typename T, size_t N>
size_t checkForValue(const T (&arr)[N], const T& val)
{
// Iterate over array.
for (size_t i = 0; i < N; ++i)
{
// Compare element to desired value.
if (arr[i] == val) { return i; }
}
// If value isn't found in array, return the array's size to indicate this.
return N;
}
可以像这样使用:
using namespace std;
int main ()
{
srand(time(NULL)); // Initiate seed
int pointPos = rand() % 6 + 1; // Generate random point
int velPoint[6] = {1, 2, 3, 4, 5, 6}; // velocity-point array
int accel[6] = {14, 17, 23, 32, 41, 59}; //
size_t accelIndex = checkForValue(velPoint, pointPos);
int accelValue = 0;
// If pointPos was found in velPoint, look up element at same index in accel.
// To determine if it was found, we check if the returned value is equal to the size of
// the array. If it is, then the value wasn't found, and we don't access accel; if it
// isn't, the value was found, and we have a valid index.
if (accelIndex != (sizeof(velPoint)/sizeof(velPoint[0])))
{
accelValue = accel[accelIndex];
}
// ...
}
请注意,函数checkForValue()
与std::find()
中的库函数<algorithm>
非常相似;虽然最好使用std::find()
而不是编写自己的,我提供了这个函数来说明在学习使用数组时对你有用的一些事情:
for
循环遍历数组。T arr[N]
,并将其作为函数参数(例如func(arr)
)传递,则该函数将数组作为T* arr
,指向第一个元素。通过引用传递可以防止这种情况,并使用模板确定T
和N
而不是将它们硬编码到签名中允许函数接受任何数组。] sizeof(array) / sizeof(array[0])
确定C数组的大小。这将获得数组使用的字节数,然后将其除以每个元素使用的字节数,以获得元素数。如果您更愿意使用std::find()
,请尝试以下操作:
#include <cstdlib>
#include <ctime>
#include <algorithm> // For std::find.
#include <iterator> // For std::distance.
using namespace std;
int main ()
{
srand(time(NULL)); // Initiate seed
int pointPos = rand() % 6 + 1; // Generate random point
int velPoint[6] = {1, 2, 3, 4, 5, 6}; // velocity-point array
int accel[6] = {14, 17, 23, 32, 41, 59}; //
// Obtain pointer to element containing pointPos, then use pointer arithmetic to
// convert it to an index.
// Included to show what happens behind the scenes. Use std::distance instead.
// size_t accelIndex = std::find(std::begin(velPoint), std::end(velPoint), pointPos) -
// std::begin(velPoint);
// Obtain pointer to element containing pointPos with std::find(), then convert to
// index of that element with std::distance().
// If pointPos isn't found, this will be equal to the array's size.
size_t accelIndex = std::distance(std::begin(velPoint),
std::find(std::begin(velPoint),
std::end(velPoint),
pointPos));
int accelValue = 0;
// If pointPos was found in velPoint, look up related element in accel.
// If std::find() is unable to find pointPos, it will return an iterator to the end of
// the passed range; due to std::distance (or our pointer arithmetic, if you use it
// instead), this will be converted into a value equal to the size of the array, as
// with checkForValue() above.
if (accelIndex != (sizeof(velPoint) / sizeof(velPoint[0])))
{
accelValue = accel[accelIndex];
}
// ...
}
这也说明了如何:
std::begin(array)
获取数组第一个元素的迭代器;这是元素0
。对于类型为T
的C数组,此迭代器将指向T
或T*
。std::end(array)
获取数组的last-the-end元素的迭代器;对于大小为N
的数组,这是元素N
。对于类型为T
的C数组,此迭代器将指向 - T
或T*
。 [注意,这实际上不是数组的成员,但是数组的已分配存储结束后的下一个内存地址。例如,对于6元素数组,如果有第7个元素,它就是第7个元素。] std::distance()
将指向数组元素的指针转换为指向元素的索引号。这比指针算法更受欢迎,因为它适用于任何类型的范围,而不仅仅是C数组。