bitset::operator[] == false/true 或 bitset::test?

发布于 2024-11-30 10:23:13 字数 1323 浏览 0 评论 0原文

使用 bitset::operator[] 是否等同于使用 bitset::test 或者是否有一些底层优化?

也就是说,这两个循环等价吗?

使用 bitset::operator[]:

static const int UP = 0;
static const int DOWN = 1;

for(int i = 1; i < KEY_MAX; ++i) {
    if(_handler && (_prevKey[i] == UP && _curKey[i] == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyDownEvent(i));
    }
    if(_handler && (_prevKey[i] == DOWN && _curKey[i] == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyPressEvent(i));
    }
    if(_handler && (_prevKey[i] == DOWN && _curKey[i] == UP)) {
        _handler->EnqueueEvent(new KeyboardKeyUpEvent(i));
    }
}

使用 bitset::test():

static const bool UP = false;
static const bool  DOWN = true;

for(int i = 1; i < KEY_MAX; ++i) {
    if(_handler && (_prevKey.test(i) == UP && _curKey.test(i) == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyDownEvent(i));
    }
    if(_handler && (_prevKey.test(i) == DOWN && _curKey.test(i) == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyPressEvent(i));
    }
    if(_handler && (_prevKey.test(i) == DOWN && _curKey.test(i) == UP)) {
        _handler->EnqueueEvent(new KeyboardKeyUpEvent(i));
    }
}

Is using bitset::operator[] equivalent to using bitset::test or is there some underlying optimization?

That is, are these two loops equivalent?

Using bitset::operator[]:

static const int UP = 0;
static const int DOWN = 1;

for(int i = 1; i < KEY_MAX; ++i) {
    if(_handler && (_prevKey[i] == UP && _curKey[i] == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyDownEvent(i));
    }
    if(_handler && (_prevKey[i] == DOWN && _curKey[i] == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyPressEvent(i));
    }
    if(_handler && (_prevKey[i] == DOWN && _curKey[i] == UP)) {
        _handler->EnqueueEvent(new KeyboardKeyUpEvent(i));
    }
}

Using bitset::test():

static const bool UP = false;
static const bool  DOWN = true;

for(int i = 1; i < KEY_MAX; ++i) {
    if(_handler && (_prevKey.test(i) == UP && _curKey.test(i) == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyDownEvent(i));
    }
    if(_handler && (_prevKey.test(i) == DOWN && _curKey.test(i) == DOWN)) {
        _handler->EnqueueEvent(new KeyboardKeyPressEvent(i));
    }
    if(_handler && (_prevKey.test(i) == DOWN && _curKey.test(i) == UP)) {
        _handler->EnqueueEvent(new KeyboardKeyUpEvent(i));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天冷不及心凉 2024-12-07 10:23:13

来自 C++03 标准,§23.3.5.2/39-41:

bool test(size_t pos) const;

要求: pos 有效
抛出: out_of_range 如果 pos 不对应于有效的位位置。
返回: 如果 *thispos 位置的位值为 1,则为 true

§23.3.5.2/46-48:

bool 运算符[](size_t pos) const;

要求: pos 有效。
抛出:什么都没有。
返回: test(pos)

§23.3.5.2/49-51:

bitset::引用运算符[](size_t pos);

要求: pos 有效。
抛出:什么都没有。
返回: bitset::reference 类型的对象,使得 (*this)[pos] == this-
test(pos)
,并且 (*this)[pos] = val 相当于 this->set(pos, val)。< /p>

因此,当对象是 const 时,它们返回相同的值,除了当 pos 无效时 test 抛出 out_of_rangeoperator[] 则不会抛出任何异常。当对象不是 const时,操作符返回一个代理对象,允许人们改变对象的数据。

From the C++03 standard, §23.3.5.2/39-41:

bool test(size_t pos) const;

Requires: pos is valid
Throws: out_of_range if pos does not correspond to a valid bit position.
Returns: true if the bit at position pos in *this has the value one.

§23.3.5.2/46-48:

bool operator[](size_t pos) const;

Requires: pos is valid.
Throws: nothing.
Returns: test(pos).

§23.3.5.2/49-51:

bitset<N>::reference operator[](size_t pos);

Requires: pos is valid.
Throws: nothing.
Returns: An object of type bitset<N>::reference such that (*this)[pos] == this-
test(pos)
, and such that (*this)[pos] = val is equivalent to this->set(pos, val).

So when the object is const, they return the same value, excepting that when pos is invalid test throws out_of_range while operator[] throws nothing. When the object is not const, the operator returns a proxy object allowing one to mutate the object's data.

撩发小公举 2024-12-07 10:23:13

与访问运算符 ([]) 不同,测试函数在检索位值之前对位置执行范围检查。如果该位置不是有效的位位置,则抛出 out_of_range 。

您可以在以下位置找到参考:

http://www.cplusplus.com/reference/stl/bitset

Unlike access operator ([]), The test function performs a range check on position before retrieveing the bit value. out_of_range is thrown if the position is not a valid bit position.

You can find references on:

http://www.cplusplus.com/reference/stl/bitset

呢古 2024-12-07 10:23:13

我会这样优化它:

int nPrevKey, nCurKey;

for(int i = 1; i < KEY_MAX; ++i) 
{
    if(_handler)
    {
        nPrevKey = _prevKey[i];
        nCurKey = _curKey[i];

        if(nPrevKey == UP && nCurKey  == DOWN)
        {
            _handler->EnqueueEvent(new KeyboardKeyDownEvent(i));
        }
        if(nPrevKey  == DOWN && nCurKey  == DOWN)
        {
            _handler->EnqueueEvent(new KeyboardKeyPressEvent(i));
        }
        if(nPrevKey  == DOWN && nCurKey  == UP)
        {
            _handler->EnqueueEvent(new KeyboardKeyUpEvent(i));
        }
    }
}

其他实现也类似。

I would optimize it this way:

int nPrevKey, nCurKey;

for(int i = 1; i < KEY_MAX; ++i) 
{
    if(_handler)
    {
        nPrevKey = _prevKey[i];
        nCurKey = _curKey[i];

        if(nPrevKey == UP && nCurKey  == DOWN)
        {
            _handler->EnqueueEvent(new KeyboardKeyDownEvent(i));
        }
        if(nPrevKey  == DOWN && nCurKey  == DOWN)
        {
            _handler->EnqueueEvent(new KeyboardKeyPressEvent(i));
        }
        if(nPrevKey  == DOWN && nCurKey  == UP)
        {
            _handler->EnqueueEvent(new KeyboardKeyUpEvent(i));
        }
    }
}

And similarly the other implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文