2017-05-19 124 views
-3

我要添加兩個bolean載體如何在C執行布爾運算++

vector<bool> v1= {0,0,1} 
vector<bool> v2= {1,0,1} 
vector<bool> resultedVector = v1+v2 

答案應該是:

resultedVector = {1,1,0}; 

有誰知道,如何在c++/c++11辦? 我想每次給布爾向量增加1.而只是想使用二元運算。或者可以創建給定數量的變量的布爾真值表。

+0

你想附加向量? –

+7

你看過std :: bitset:http://en.cppreference.com/w/cpp/utility/bitset? – marcinj

+0

我剛加了一個問題,我想要執行加法。結果矢量的大小相同。 – TonyParker

回答

0

在C++進行二進制加法,可以使用這裏所描述的功能: Adding binary numbers in C++

我從該鏈接實現的功能,以適應您的要求是這樣的:

std::vector<bool> add(const std::vector<bool>& a, const std::vector<bool>& b) 
{ 
     bool c; 
     std::vector<bool> result; 
     for(int i = 0; i < a.size() ; i++){ 
       result.push_back(false); 
       result[i] = ((a[i]^b[i])^c); // c is carry 
       c = ((a[i] & b[i]) | (a[i] & c)) | (b[i] & c); 
     } 
     return result; 
} 

該函數有兩個bools向量(並假定它們的大小相同)並返回它們的結果向量。顯然這個函數不處理溢出或不同大小的數字。如果您需要這些功能,您可以自行修改它。另外,你似乎在討論一個bool向量的重載操作符,你可以通過檢查操作符重載來實現,但是這個邏輯將允許你添加存儲在向量中的兩個布爾值。

+0

與嵌入式添加相比,該方法會非常慢。 – Slava

+0

是的,顯然最好的方法是使用像int這樣的現有類型,並通過整數加法執行布爾運算,但是我回答了這個問題,假設我們需要使用向量。 –

0

這裏是你如何使用狀態函子:

struct BitAdder { 
    bool carry_ = 0x0; // Range is [0, 1]. 

    // Only accepts single bit values for a and b. 
    bool operator()(bool a, bool b) { 
     assert(a == (a & 0x1) && b == (b & 0x1)); 
     char sum = a + b + carry_; 
     carry_ = (sum & 0x2) >> 1; // Keep in range. 
     return sum & 0x1; 
    } 
}; 

// Code is more straightforward when bits are stored in reverse. 
std::vector<bool> v = {0, 1, 1, 1, 0}; // To be interpreted as: 1110 (14). 
std::vector<bool> w = {1, 0, 1, 1, 0}; // To be interpreted as: 1101 (13). 
std::vector<bool> result = {0, 0, 0, 0, 0}; //  Will become: 11011 (27). 

assert(v.size() <= w.size()); // v and w can be iterated over together. 
assert(v.size() <= result.size()); // There is enough space to store the bits. 
assert(v[v.size() - 1] + w[v.size() - 1] < 2); // No overflow can happen. 
std::transform(v.cbegin(), v.cend(), w.cbegin(), result.begin(), BitAdder()); 

std::cout << "want: 11011, got: "; 
std::copy(result.crbegin(), result.crend(), std::ostream_iterator<bool>(std::cout)); 
std::cout << '\n'; 

Live Demo

+0

如果操作數大小不同,該怎麼辦? 'a + b + carry_'不是布爾算術。 – ZDF

0

我不知道,我明白你的問題。因爲這看起來像功課和問題的要點似乎是運營商超載,這裏有一個想法,而不是完整的答案:

#include <vector> 

std::vector<bool> operator+(const std::vector<bool>& a, const std::vector<bool>& b) 
{ 
    std::vector<bool> r; 
    // your code goes here 
    return r; 
} 

int main() 
{ 
    std::vector<bool> a, b, c; 
    c = a + b; 
    return 0; 
} 

編輯 - 一天後

這裏是你的增值解決方案問題(demo):

#include <iostream> 
#include <vector> 

// preinc - no grow on overflow 
std::vector<bool>& operator++(std::vector<bool>& v) 
{ 
    for (auto e : v) 
    if (e = !e) 
     break; 
    return v; 
} 

// postinc - no grow on overflow 
std::vector<bool> operator++(std::vector<bool>& v, int) 
{ 
    auto t { v }; 
    operator++(v); 
    return t; 
} 

// insert 
std::ostream& operator<<(std::ostream& os, const std::vector<bool> v) 
{ 
    for (std::vector<bool>::const_reverse_iterator ci = v.rbegin(); ci != v.rend(); ++ci) 
    os << *ci ? '1' : '0'; 
    return os; 
} 

int main() 
{ 
    std::vector<bool> b {0,0,0,0}; 
    for (int i = 0; i < 16; ++i) 
    { 
    std::cout << b << std::endl; 
    ++b; 
    } 

    return 0; 
}