Prateek Oraon Prateek Oraon - 3 years ago 114
C++ Question

Applying or(||) operator on two vectors containing only 0 and 1

I have two vectors:
A = {1,0,1,1},
B = {0,1,1,1}.
How can I apply or operator on these two so that i could get a vector:
C = {0,0,1,1} .

Answer Source

You can just loop through them element-wise using indices

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> A = {1, 0, 1, 1};
    std::vector<int> B = {0, 1, 1, 1};

    std::vector<int> C(A.size());
    for (std::size_t i = 0; i < A.size(); ++i)
    {
        C[i] = A[i] && B[i];
    }

    for (auto value : C)
    {
        std::cout << value << " ";
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download