bitset::operator[] == false/true 或 bitset::test?
使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 C++03 标准,§23.3.5.2/39-41:
§23.3.5.2/46-48:
§23.3.5.2/49-51:
因此,当对象是 const 时,它们返回相同的值,除了当 pos 无效时
test
抛出out_of_range
而operator[]
则不会抛出任何异常。当对象不是const
时,操作符返回一个代理对象,允许人们改变对象的数据。From the C++03 standard, §23.3.5.2/39-41:
§23.3.5.2/46-48:
§23.3.5.2/49-51:
So when the object is
const
, they return the same value, excepting that whenpos
is invalidtest
throwsout_of_range
whileoperator[]
throws nothing. When the object is notconst
, the operator returns a proxy object allowing one to mutate the object's data.与访问运算符 ([]) 不同,测试函数在检索位值之前对位置执行范围检查。如果该位置不是有效的位位置,则抛出 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
我会这样优化它:
其他实现也类似。
I would optimize it this way:
And similarly the other implementation.