从指针向量中查找并删除元素?
vector<unsigned int> x;
vector<unsigned int>::iterator itr;
unsigned int varF;
...
....
// find and delete an element from a vector.
itr = std::find(x.begin(), x.end(), varF); // <algorithm>
if (itr != x.end())
x.erase(itr);
//or
x.erase(std::remove(x.begin(), x.end(), varF), x.end());
我想将此向量转换为指针向量
vector<unsigned int*> x;
如何将上述功能转换为指针向量?
vector<unsigned int> x;
vector<unsigned int>::iterator itr;
unsigned int varF;
...
....
// find and delete an element from a vector.
itr = std::find(x.begin(), x.end(), varF); // <algorithm>
if (itr != x.end())
x.erase(itr);
//or
x.erase(std::remove(x.begin(), x.end(), varF), x.end());
I want to convert this vector to a vector of pointers
vector<unsigned int*> x;
How I can convert the above functionality for a vector of pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
find_if
而不是find
,或使用remove_if
而不是remove
,以使用自定义谓词:如果您有 C ++11,您可以使用 lambda 代替显式谓词:
如果您想在需要取消引用和比较的其他类似情况下重用谓词,则可以将谓词转换为模板。如果是这种情况,模板比每次都输入 lambda 更优雅。
Use
find_if
instead offind
, orremove_if
instead ofremove
, to employ a custom predicate:If you have C++11, you can use a lambda instead of the explicit predicate:
The predicate could be turned into a template if you want to reuse it for other similar situations where you need to dereference-and-compare. If this is the case, a template is more elegant than typing out the lambdas each time.