STL 设置相等运算符是否首先检查大小?
当我使用 == 或 != 运算符来比较两个集合时,该运算符实际上是否首先比较两个集合的大小?我想知道是否需要先手动比较两个尺寸以提高效率,或者是否实际上会降低效率。我知道相等和不等运算符会检查大小,我只是不知道它是否会首先这样做。
bool checkEqualTo( const set<int> & set1, const set<int> & set2 )
{
// Should I include comparison of sizes first?
if ( set1.size() != set2.size() )
{
return false;
}
if ( set1 != set2 )
{
return false;
}
return true;
}
When I'm using the == or != operator to compare two sets, does that operator actually compare the size of the two sets first? I'm wondering if I need to manually compare the two sizes first to make it more efficient, or if I would actually be making it less efficient. I know the equality and inequality operators will check size, I just don't know if it will do so first.
bool checkEqualTo( const set<int> & set1, const set<int> & set2 )
{
// Should I include comparison of sizes first?
if ( set1.size() != set2.size() )
{
return false;
}
if ( set1 != set2 )
{
return false;
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是检查的第一件事 - 根据 C++11 标准,§23.2.1 表 96(容器要求):
Yes, that's the first thing that's checked — from the C++11 standard, §23.2.1 table 96 (Container requirements):