我正在尝试使用带有 pop_back 的 while 循环从 C++ 向量中删除尾随零。如果向量的最后一个元素是 0,它应该弹出最后一个元素,直到它不再是 0。
很难理解这些错误的含义,如果有更明智的人可以帮助我破译这些错误,我将非常感激!
void Integer::unzero() {
while ((*this).bits_vector[((*this).bits_vector.size)] == 0) {
(*this).bits_vector.pop_back;
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
error C2679: binary '[': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
error C3867: 'std::vector<bool,std::allocator<_Ty>>::pop_back': non-standard syntax; use '&' to create a pointer to member
Run Code Online (Sandbox Code Playgroud)
使用std::find_if和std::vector::erase:
auto rit = std::find_if(bits_vector.rbegin(), bits_vector.rend(),
[](int v) { return v != 0; });
bits_vector.erase(rit.base(), end(bits_vector));
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你忘了括号:bits_vector.size()和bits_vector.pop_back()。
| 归档时间: |
|
| 查看次数: |
6112 次 |
| 最近记录: |