Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想检查一个向量,看看它是否至少有一个元素。哪个更快?
if (vec.size()) { ... }
或者
if (vec) { ... }
有没有更好的解决方案?
使用 just vector::empty,因为它专门用于此特定任务。
vector::empty
std::vector<int> myvector; for (int i=1;i<=10;i++) myvector.push_back(i); while (!myvector.empty()) { sum += myvector.back(); myvector.pop_back(); }