bitwise operations on vector

前端 未结 4 1605
日久生厌
日久生厌 2020-12-06 16:22

what\'s the best way to perform bitwise operations on vector?

as i understand, vector is a specialisation that uses

4条回答
  •  悲哀的现实
    2020-12-06 16:55

    I read both these answers, but just wanted a quick solution, and implemented something horrible.

    You can make the bitwise operators work on vector, but the code has to be specialized for the c++ standard library implementation or fall back to the slow form. Here's my operator| for GNU libstdc++-v3:

    std::vector operator|(std::vector A, const std::vector& B)
    {
        if (A.size() != B.size())
            throw std::invalid_argument("differently sized bitwise operands");
    
        std::vector::iterator itA = A.begin();
        std::vector::const_iterator itB = B.begin();
    
        // c++ implementation-specific
        while (itA < A.end())
            *(itA._M_p ++) |= *(itB._M_p ++); // word-at-a-time bitwise operation
    
        return A;
    }
    

    This is of course pretty bad. Somebody updates GCC, the new version stores things differently, and your code breaks for no apparent reason.

提交回复
热议问题