小编mag*_*gu_的帖子

将vector <bool>转换为int

我有一个vector的bool,我想在复制int较大尺寸的容器.有没有快速的方法来做到这一点?

澄清一下,有没有更聪明的方法来实现这一目标?

#include <vector>
#include <cstdint>
#include <iostream>
#include <climits>
#include <cassert>


inline size_t bool2size_t(std::vector<bool> in) {
    assert(sizeof(size_t)*CHAR_BIT >= in.size());
    size_t out(0);

    for (size_t vecPos = 0; vecPos < in.size(); vecPos++) {
        if (in[vecPos]) {
            out += 1 << vecPos;
        }
    }

    return out;
} 

int main () {
    std::vector<bool> A(10,0);
    A[2] = A[4] = 1;

    size_t B = bool2size_t(A);

    std::cout << (1 << 2) + (1 << 4) << std::endl;
    std::cout << B …
Run Code Online (Sandbox Code Playgroud)

c++ memcpy

3
推荐指数
1
解决办法
5036
查看次数

在python中使用装饰器跳过单元测试

我正在写一些unittest,发现一个相当奇怪的行为,几乎把我烧死了。

以下测试:

import unittest

class Test(unittest.TestCase):
    @unittest.skip('Not ready yet')
    def test_A(self):
        self.assertTrue(False)

    @unittest.skip
    def test_B(self):
        self.assertTrue(False)

    def test_C(self):
        self.assertTrue(False)

if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

结果是:

test_A (__main__.Test) ... skipped 'Not ready yet'
test_B (__main__.Test) ... ok
test_C (__main__.Test) ... FAIL

======================================================================
FAIL: test_C (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./test.py", line 13, in test_C
    self.assertTrue(False)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 3 tests in 0.000s
Run Code Online (Sandbox Code Playgroud)

使用装饰器unittest.skipempty 确实会跳过测试,但随后将其报告为通过。因此,这个跳过的测试很容易在第二天被遗忘并永远停留在跳过状态。这个跳过背后的原因是什么,但报告通过行为?

如果重要:

  • Python:3.4.3 | 蟒蛇 2.3.0(64 位) …

python unit-testing python-3.x

3
推荐指数
1
解决办法
1997
查看次数

如何从dict调用__missing__

我想从dict派生并覆盖缺少的方法.我想做一些东西然后仍然调用它的超级功能.像这样的东西:

class Foo(dict):
    def __missing__(self, key):
        print('missing {}'.format(key))
        return super().__missing__(key)
Run Code Online (Sandbox Code Playgroud)

然而,这产生:

AttributeError: 'super' object has no attribute '__missing__'
Run Code Online (Sandbox Code Playgroud)

当然,我仍然可以使用以下方法制作工作程序:

raise KeyError(key)
Run Code Online (Sandbox Code Playgroud)

但我更愿意称之为超级功能.因为代码可以(或者可能在将来的python版本中)执行,super().__missing__而不是引发KeyError.

python oop dictionary python-3.x

3
推荐指数
1
解决办法
1217
查看次数

gcc是否将uint8_t转换为int以获取单个值?

如果我编译以下程序:

#include <vector>
#include <cstdint>
#include <stdio.h>

int main() {
    constexpr std::size_t N = 10;
    uint8_t int8Value = 42;

    std::vector<int> IntVector(N, 0);
    for (int & ele:IntVector) {
        ele += int8Value;
    }

    std::vector<uint8_t> Int8Vector(N, 0);
    for (uint8_t & ele:Int8Vector) {
        ele += int8Value;
    }

    for (std::size_t i = 0; i < N; i++) {
        printf("%i %i\n",IntVector[i],Int8Vector[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

与g++ test.cpp -o test -std=c++11 -Wconversion上gcc 4.9它吐出来的是如下因素警告:

test.cpp: In function ‘int main()’:
test.cpp:16:7: warning: conversion to ‘uint8_t {aka unsigned …
Run Code Online (Sandbox Code Playgroud)

c++ gcc type-conversion implicit-conversion c++11

1
推荐指数
1
解决办法
3595
查看次数