当前位置: 首页>>代码示例>>C++>>正文


C++ BitArray::toggle方法代码示例

本文整理汇总了C++中BitArray::toggle方法的典型用法代码示例。如果您正苦于以下问题:C++ BitArray::toggle方法的具体用法?C++ BitArray::toggle怎么用?C++ BitArray::toggle使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BitArray的用法示例。


在下文中一共展示了BitArray::toggle方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

// Test program
int main() {
    // Test exceptions
    BitArray<> b;
    throw_(b[0],logic_error);
    throw_(b.toggle(0),logic_error);
    const BitArray<> b1{b}; // Test copy constructor
    throw_(b1[0],logic_error);

    // Test empty Bitarray properties
    test_(b.size() == 0);
    test_(b.count() == 0);
    test_(b.capacity() == 0);
    test_(!b.any());

    // Validate construction and to_string()
    BitArray<> b2{5};
    test_(b2.size() == 5);
    for (size_t i = 0; i < 5; ++i) {
        test_(!b2[i]);
    }
    test_(b2.to_string() == "00000");

    // Test copy, assign, equality, and from_string
    BitArray<> b3{b2};
    test_(b2 == b3);
    test_(b != b2);
    test_(!b3[2]);
    b3[2] = 1;
    test_(b3[2]);
    test_(b2 != b3);
    test_(b2.to_string() == "00000");
    b = b2;
    test_(b.to_string() == "00000");

    // Test move operations
    BitArray<> b4{move(b3)};
    test_(b4[2]);
    BitArray<> b4b;
    b4b = move(b4);
    test_(b4b[2]);

    // Test bit ops
    BitArray<> x{"011010110"}; // Also tests string constructor
    test_(x.count() == 5);
    test_(x.any());
    test_((x << 6).to_string() == "110000000");
    test_((x >> 6).to_string() == "000000011");
    test_((x <<= 3).to_string() == "010110000");
    test_((x >>= 3).to_string() == "000010110");
    BitArray<> y{~x};
    nothrow_(x.toggle());
    test_(x == y);
    test_(x.to_string() == "111101001");

    b = BitArray<> {};
    test_(!b.any());
    b += 1;
    b += 0;
    b += 1;
    test_(b.to_string() == "101");
    test_(b.any());

    b2 = BitArray<> {"10101011"};
    test_(b2.count() == 5);
    b2.toggle();
    test_(b2.to_string() == "01010100");
    b2.erase(3);
    test_(b2.to_string() == "0100100");
    b2.erase(b2.size() - 1);
    test_(b2.to_string() == "010010");
    b2.erase(1,4);
    test_(b2.to_string() == "00");
    b2.insert(1,1);
    test_(b2.to_string() == "010");
    b2.insert(1,0);
    test_(b2.to_string() == "0010");
    b2 += b;
    test_(b2.to_string() == "0010101");
    b2.insert(3, b);
    test_(b2.to_string() == "0011010101");

    ostringstream os;
    os << "101" << 'a' << "0101";
    istringstream is{os.str()};
    b3 = BitArray<> {};
    is >> b3;
    test_(b3.to_string() == "101");
    is.get();
    is >> b3;
    test_(b3.to_string() == "0101");
    os.str("");
    os << 'a';
    istringstream is2{os.str()};
    is2 >> b3;
    test_(!is2);
    test_(b3.to_string() == "0101");

    BitArray<> b5{"11111111111111111111111111000000000000000000000000000011"};
    test_(b5.slice(23,10) == BitArray<>("1110000000"));
//.........这里部分代码省略.........
开发者ID:JonDegn,项目名称:BitArray,代码行数:101,代码来源:tbitarray.cpp


注:本文中的BitArray::toggle方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。