Should I use an algorithm or manual code in this case?

Well, they tell me that it would be better. I need | = elements of one vector with another. That is, I want

void orTogether(vector<char>& v1, const vector<char>& v2)
{
    typedef vector<char>::iterator iter;
    for (iter i = v1.begin(), iter j = v2.begin() ; i != v1.end(); ++i, ++j)
        *i |= *j;
}

I cannot use for_each due to the need to process 2 collections. I guess I could do something like

struct BitWiseOr
{
    char operator()(const char& a, const char& b) {return a | b;}
};

void orTogether2(vector<char>& v1, const vector<char>& v2)
{
    transform(v1.begin(), v1.end(), v2.begin(), 
        v1.begin(), BitwiseOr());
}

Is this a more efficient solution, even if the top is in place, but the bottom is the destination? This is right in the middle of the processing loop, and I need the fastest code.

Edit: Added (obvious?) Code for BitwiseOr. In addition, I get a lot of comments about unrelated things like checking v2 lengths and changing names. This is just an example, the real code is more complex.

, . orTogether2 , orTogether, . , Together2 4 MSVC9. , , , - , . .

+3
5

, , OR- . , , - - .

, . , , . , C++, raw C.

+12

+5

, , MyBitwiseOrObject. ?

transform , ( , ). - Boost Lambda.

+2

, STL,

  • ( )

, . STL .

+1

? , , ? , , .

However, I would use a transform. Let a well-tested library do a heavy lift. Keep your code simple and clean. You may not know exactly what is going on inside each of them, but you can trust the library developers to do a good job.

0
source

Source: https://habr.com/ru/post/1705922/


All Articles