源码
/opt/homebrew/Cellar/llvm/16.0.4/include/c++/v1/__iterator/wrap_iter.h
// clang llvm
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator++()
_NOEXCEPT {
_LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
"Attempted to increment a non-incrementable iterator");
++__i_;
return *this;
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
operator++(int) _NOEXCEPT {
__wrap_iter __tmp(*this);
++(*this);
return __tmp;
}
可以看出就是跟 int 版本的 i++, ++i 定义一模一样. 前置的效率高一些
对于 gcc 来说, 是这样的:
_GLIBCXX20_CONSTEXPR
__normal_iterator&
operator++() _GLIBCXX_NOEXCEPT
{
++_M_current;
return *this;
}
_GLIBCXX20_CONSTEXPR
__normal_iterator
operator++(int) _GLIBCXX_NOEXCEPT
{ return __normal_iterator(_M_current++); }
目测没什么区别
汇编
采用 gcc13 -g:
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char* argv[]) {
vector<int> v{1, 2, 3};
auto it = v.begin();
// cout << *(++it) << endl;
cout << *(it++) << endl;
return 0;
}