我是否必须求助于循环,以比较C 中的两个2D阵列

Will I have to resort to for loops for comparing two 2d arrays in C++?

本文关键字:阵列 2D 两个 是否 求助于 循环 比较      更新时间:2023-10-16

我有两个3x3数组,其中填充了浮点数。我的目标是获取每个相应的单元格的差异,并将其与设定值进行比较。custSmiles只是帮助我确定所有单元格差异是否低于custSmiles

到目前为止,我有

double shopperChoice = 0.80;
int custSmiles = 0;
for(int a = 0; a < 4; a++) {
    for(int b = 0; b < 4; b++) {
        if((wheat[a][b] - nineGrain[a][b]) < shopperChoice) {
            custSmiles++;
        }
    }
}

Will I have to resort to for loops for comparing two 2d arrays in C++?

已经很晚了,我真的很无聊,我做到了,请按照您的方式接受,但是我觉得这有某种价值,如果我犯了任何错误是我不应该这样做,我只是在努力帮助,而且我当然不会有意地做错事。

我也意识到我选择了一些可怕的名字,但是您可以将它们更改为您想要的任何东西,我正在努力选择更好的名字,如果您发现任何有用的东西,请将其切成薄片并使它适合您,无论如何足够的杂乱无章。

main.cpp ;

#include <iostream>
#include "hide.h"
int main(){
    float array_A[3][3] = {
        {1, 1, 1},
        {1, 1, 1},
        {1, 1, 1}
    };  
    float array_B[3][3] = {
        {0.1, 0.2, 0.3},
        {0.4, 0.5, 0.6},
        {0.7, 0.8, 0.9}
    };
    hide::setChoice(0.35);
    hide A(array_A), B(array_B);
    std::cout<< "ncounter after A - B: "<< A - B << "n";
    hide::resetCounter();
}
    /* * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * the values returned by ( A - B ) are useless in this
     * context it would only make sense if you changed this
     * to fill/populate them with the proper values.
     *
     * that said this version of the class evaluates the
     * the same expression you did in the if statement within
     * the for loops (x[a][b] - y[a][b] < c) on each element
     * within the array/s.
     *
     * This effectively *hides* the for loops if you don't
     * look in the classes source files when you're thinking
     * "Geez i hate loops".
     *
     * lastly there is no bounds checking (or error) present,
     * you can index any value you could without a class
     * wrapped around the array should you decide to do that.
     *
     * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * (A - B) evaluates to s_counter
     *                  
     *  A (or) B.access( row, col ) treats like 2d
     *
     *  A (or) B.index( i ) treats like 1d
     *
     *  access() & index() are not used in the example
     *
     * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * hide::getCounter() also returns s_counter
     *
     * hide::resetCounter() sets s_counter to 0
     *
     */

hide.h

#ifndef HIDE_H
#define HIDE_H // include guard
// width and height constants need to be set accordingly
#define _WIDTH 3
#define _HEIGHT 3
class hide {
    private:
        static const int s_height;
        static const int s_width;
        static float s_choice; // a.k.a shopperChoice
        static int s_counter; // a.k.a custSmiles

        float (*m_array)[_WIDTH];
        /*
         * I'm using this as a pointer to
         * a multidimensional array, that is what
         * a 2d array really is under the hood,
         * i prefer to just use it that way and
         * create the abstractions myself..
         */
    public:
        static float setChoice(const float &choice);
        static int resetCounter();
        static int getCounter();
        hide(float (*ptr)[_WIDTH]);
        ~hide();
        float access(const int &row, const int &col);
        float index(const int &i);
        int operator-(hide &aHide);
};
#endif

hide.cpp

#include "hide.h"
// static initializations
int hide::s_counter = 0;
float hide::s_choice = 0;
const int hide::s_height = _HEIGHT;
const int hide::s_width = _WIDTH;
//------------------------------------
// Constructor
hide::hide(float (*ptr)[_WIDTH]){
    m_array = ptr;
}
// Destructor........
hide::~hide(){
    // default
}
// overloaded (-) minus operator to perform the
// desired operation, this could be changed to
// any operator you feel better suited..
int hide::operator-(hide &aHide){
    for(int i = 0; i<s_height*s_width; ++i){
        if( (*m_array)[i] - aHide.index(i) < s_choice ) {
            ++s_counter;
        }
    }
    return s_counter;
}
// static member functions
//----------------------------
// resets s_counter to 0
int hide::resetCounter(){
    s_counter = 0;
}
// sets choice to a specified value
float hide::setChoice(const float &choice){
    s_choice = choice;
}
// returns s_counters current value
int hide::getCounter(){
    return s_counter;
}
//----------------------------
// access m_array like a 2d array
float hide::access(const int &row, const int &col){
        return (*m_array) [row*s_width+col];
}
// access m_array like a 1d array
float hide::index(const int &i){
    return (*m_array) [i];
}

[原始答案]您可以使用一个指向数组数组的指针求解一个;

#include <iostream>
int main(){
    const unsigned height = 3, width = 3;
    float A[height][width] = {
        {1, 1, 1},
        {1, 1, 1},
        {1, 1, 1}
    };  
    float B[height][width] = {
        {0.1, 0.2, 0.3},
        {0.4, 0.5, 0.6},
        {0.7, 0.8, 0.9}
    };
    int counter = 0;
    float choice = 0.35;
    for(int i = 0; i < height*width; ++i){
        if( (*A)[i] - (*B)[i] < choice ) {
            ++counter;
        }
    }
    std::cout<< "counter: " << counter; 
}

输出;

counter: 3

假设您的代码描述了您要做的事情,也就是说,比较一对矩阵中每个相应单元格的差异,那么是的,您需要"访问"每个单元格,这需要某种形式的循环。因此,没有办法避免这种情况。

正如其他人所说的那样,如果您的矩阵为3x3,那么您应该拥有a < 3b < 3,或者您在循环中的矩阵外。

有可能您可以以不同的方式编写它,但是在这种情况下,我真的想不出任何制造的东西是"更好的"。

如果您的目标是确定所有单元格是否低于一定值,则如果该值不符合您的标准,则可以"提早停止"。这样的东西:

success = true;
for(int a = 0; a < 3 && success; a++)
{
   for(int b = 0; b < 3 && success; b++)
   {
      if((wheat[a][b] - nineGrain[a][b]) >= shopperChoice) {
         sucess = false;
      }
   }
}

然后,您可以做if (success),而不是if (custSmiles == 9)或其他任何东西,以确定它是"好的"。

在这种情况下可能几乎没有什么不同,但是对于经常"不匹配"的非常大的矩阵(假设不匹配是Venly分布式位置,如果始终是右下角,那么您可能想要)反向进行循环,或者一些]。

是。当涉及到阵列,向量,矩阵或类似的东西时,您想在每个单元格的每个(或子集)上进行相同的操作(例如,在一个子集上进行比较),则循环是选择的武器。

您是否选择for或while-loops(或其他不常见的构造)。

Sidenote:虽然重要的是可读性。但是幸运的是,如果您做正确的话,循环将支持这一点。我个人倾向于提取内部循环以分开功能。如果函数名称被很好地选择,这很有帮助。就像Joachim Pileborg已经说过的那样,封装是这里的关键。