C++ 我可以向数组的所有成员添加值吗

C++ 我可以向数组的所有成员添加值吗,c++,stl,C++,Stl,STL中是否有一种算法可以同时向数组的所有成员添加相同的值 例如: KnightMoves moveKnight(int currentPossition_x, int currentPossition_y) { array<int , 8> possibleMoves_x = { -2 , -2 , -1 , -1 , 1 , 1 , 2 , 2 }; array<int , 8> possibleMoves_y = { -1 , 1 , -2 ,

STL中是否有一种算法可以同时向数组的所有成员添加相同的值

例如:

KnightMoves moveKnight(int currentPossition_x, int currentPossition_y)
{
    array<int , 8> possibleMoves_x = { -2 , -2 , -1 , -1 ,  1 , 1 , 2 ,  2 };
    array<int , 8> possibleMoves_y = { -1 ,  1 , -2 ,  2 , -2 , 2 , -1 , 1 };

    for (auto it = possibleMoves_x.begin(); it != possibleMoves_x.end(); it++)
    {
        array <int, 8> newTempKnightPoss_x = currentPossition_x + possibleMoves_x;

        array <int, 8> newTempKnightPoss_y = currentPossition_y + possibleMoves_x;
    }

}
KnightMoveKnight(int-CurrentPosition\u x,int-CurrentPosition\u y)
{
数组可能移动_x={-2,-2,-1,-1,1,1,2,2};
数组可能移动_y={-1,1,-2,2,-2,2,-1,1};
for(auto it=possibleMoves\u x.begin();it!=possibleMoves\u x.end();it++)
{
数组newtempnightposs\ux=当前position\ux+possibleMoves\ux;
数组newTempKnightPoss_y=当前Position_y+可能移动_x;
}
}
我可以这样做,但我希望有更好的解决办法

KnightMoves moveKnight(int currentPossition_x, int currentPossition_y)
{
   array<int , 8> possibleMoves_x = { -2 , -2 , -1 , -1 ,  1 , 1 , 2 ,  2 };
   array<int , 8> possibleMoves_y = { -1 ,  1 , -2 ,  2 , -2 , 2 , -1 , 1 };

   for (auto it = possibleMoves_x.begin(); it != possibleMoves_x.end(); it++)
   {
       *it = *it  +currentPossition_x;

   }
   for (auto it = possibleMoves_y.begin(); it != possibleMoves_y.end(); it++)
   {
       *it = *it + currentPossition_y;

   }
}
KnightMoveKnight(int-CurrentPosition\u x,int-CurrentPosition\u y)
{
数组可能移动_x={-2,-2,-1,-1,1,1,2,2};
数组可能移动_y={-1,1,-2,2,-2,2,-1,1};
for(auto it=possibleMoves\u x.begin();it!=possibleMoves\u x.end();it++)
{
*it=*it+currentPosition\ux;
}
for(auto it=possibleMoves_y.begin();it!=possibleMoves_y.end();it++)
{
*it=*it+CurrentPosition\u y;
}
}

axpepted结果是2个数组,每个元素是元素加一个常量值

如果您有C++11,则可以使用循环:

KnightMoveKnight(int-CurrentPosition\u x,int-CurrentPosition\u y){
数组可能移动_x={-2,-2,-1,-1,1,1,2,2};
数组可能移动_y={-1,1,-2,2,-2,2,-1,1};
对于(auto&i:possibleMoves_x){i+=currentposition_x;}
对于(auto&i:possibleMoves_y){i+=currentposition_y;}
}
在C++11之前,您可以使用:

结构加法器{ 加法器(int-val):v{val}{} void运算符()(int&n){n+=v;} INTV; }; 奈特移动奈特(int-currentPosition\u x,int-currentPosition\u y){ 数组可能移动_x={-2,-2,-1,-1,1,1,2,2}; 数组可能移动_y={-1,1,-2,2,-2,2,-1,1}; std::for_each(可能移动x.begin(),可能移动x.end(), 加法器(CurrentPosition_x)); std::for_each(可能移动_y.begin(),可能移动_y.end(), 加法器(CurrentPosition_x)); }
根据您想要做的一切,可以使用合适且简单的(或函子对象)?for range使它变得很简单:
for(auto&e:possibleMoves_x){e+=currentposition_x;}
“在C++11之前”不能使用lambdas。
KnightMoves moveKnight(int currentPossition_x, int currentPossition_y){
    array<int , 8> possibleMoves_x = { -2 , -2 , -1 , -1 ,  1 , 1 , 2 ,  2 };
    array<int , 8> possibleMoves_y = { -1 ,  1 , -2 ,  2 , -2 , 2 , -1 , 1 };

    for(auto& i : possibleMoves_x){ i += currentPossition_x; }
    for(auto& i : possibleMoves_y){ i += currentPossition_y; }
}
struct adder{
    adder(int val): v{val}{}
    void operator()(int& n) { n += v; }
    int v;
};

KnightMoves moveKnight(int currentPossition_x, int currentPossition_y){
    array<int , 8> possibleMoves_x = { -2 , -2 , -1 , -1 ,  1 , 1 , 2 ,  2 };
    array<int , 8> possibleMoves_y = { -1 ,  1 , -2 ,  2 , -2 , 2 , -1 , 1 };

    std::for_each(possibleMoves_x.begin(), possibleMoves_x.end(), 
                  adder(currentPossition_x));
    std::for_each(possibleMoves_y.begin(), possibleMoves_y.end(),
                  adder(currentPossition_x));
}