C++关于带有remove_if的指针/引用

发布于 2024-12-27 19:07:01 字数 775 浏览 5 评论 0原文

我正在尝试使用remove_if 删除向量中的元素以进行过滤。问题是当我编译编码时,没有错误,但是当我尝试使用过滤器函数时,弹出错误说我无法取消引用迭代器。我不知道出了什么问题,希望你们能帮忙找出问题所在。 这是我的部分代码

bool filter_C (Teacher &t) 
{ 
return (t.getCat() != compare); //compare is a static string
}
void filterTeacherCategory(vector<Teacher> &t)
{
    vector<Teacher>::iterator i;
    Teacher *ptr;
    i = remove_if(t.begin(), t.end(), filter_C);
    ptr = &(*i);
    for (i = t.begin(); i != t.end(); ++i)
    {
        ptr->getName();
        cout << "\t";
        ptr->getGender();
        cout << "\t";
        ptr->getPhone();
        cout << "\t";
        ptr->getCategory();
        cout << "\t\t";
        ptr->getLocation();
        cout << "\n";
     }
}

I'm trying to use remove_if to remove elements in my vector to do filtering. The problem is when I compile the coding, there were no error but when I try to use the filter function, error popped out saying I can't dereference an iterator. I have no idea what is wrong and hope you guys can help spot the problem.
Here's partial of my codes

bool filter_C (Teacher &t) 
{ 
return (t.getCat() != compare); //compare is a static string
}
void filterTeacherCategory(vector<Teacher> &t)
{
    vector<Teacher>::iterator i;
    Teacher *ptr;
    i = remove_if(t.begin(), t.end(), filter_C);
    ptr = &(*i);
    for (i = t.begin(); i != t.end(); ++i)
    {
        ptr->getName();
        cout << "\t";
        ptr->getGender();
        cout << "\t";
        ptr->getPhone();
        cout << "\t";
        ptr->getCategory();
        cout << "\t\t";
        ptr->getLocation();
        cout << "\n";
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江心雾 2025-01-03 19:07:01

要实际删除元素,您需要执行类似

t.erase(std::remove_if(...),t.end());

remove_if 的操作,只为您提供删除元素的范围(新结束)。在您的代码中,您的 ptr 正是新的结尾(即,超过最后一个有效元素)。

To actually erase elements, you need to do something like

t.erase(std::remove_if(...),t.end());

remove_if only provides you with a range (new end) with elements removed. And in your code your ptr is exactly the new end (that is, one past the last valid element).

嗳卜坏 2025-01-03 19:07:01

remove_if 返回向量的新末尾。所以你应该像这样迭代

vector<Teacher>::iterator i;
vector<Teacher>::iterator newenditer = remove_if(..);


for (i = t.begin(); i != newenditer ; ++i)
{
       Teacher& tchr= *i;
       cout << tchr.getName() << "\n";
       cout << tchr.getPhone() << "\n";

}

来自 remove_if 文档

将 pred 应用于范围 [first,last) 中的元素,并从结果范围中删除那些不返回 false 的元素。结果范围由函数返回的第一个元素和迭代器之间的元素组成,该迭代器指向范围的新末尾。

保留未删除元素的相对顺序,而超出新范围末尾的元素仍然有效,尽管具有未指定的值。

一般来说,在remove_if之后删除剩余元素是个好主意

vector<Teacher>::iterator i;
vector<Teacher>::iterator newenditer = remove_if(..);
t.erase( newenditer , t.end() );

现在t.begin()和t.end()之间的所有内容都是有效且良好的,所以你可以这样做

 for (i = t.begin(); i != t.end() ; ++i)
    {
    }

remove_if returns the new end of the vector. so you should be iterating like so

vector<Teacher>::iterator i;
vector<Teacher>::iterator newenditer = remove_if(..);


for (i = t.begin(); i != newenditer ; ++i)
{
       Teacher& tchr= *i;
       cout << tchr.getName() << "\n";
       cout << tchr.getPhone() << "\n";

}

From remove_if documenation

Applies pred to the elements in the range [first,last), and removes those for which it does not return false from the resulting range. The resulting range consists of the elements between first and the iterator returned by the function, which points to the new end of the range.

The relative order of the elements not removed is preserved, while the elements past the new end of range are still valid, although with unspecified values.

In general it is a good idea to erase the remaining elements after a remove_if

vector<Teacher>::iterator i;
vector<Teacher>::iterator newenditer = remove_if(..);
t.erase( newenditer , t.end() );

Now everything between t.begin() and t.end() is all valid and good so you can do

 for (i = t.begin(); i != t.end() ; ++i)
    {
    }
叹沉浮 2025-01-03 19:07:01

此行在

ptr = &(*i);

序列的过滤部分结束后取消引用元素。如果您的过滤器根本不匹配任何元素,因此没有删除任何内容,那么您将尝试将迭代器取消引用到向量末尾的迭代器,这将给出您报告的错误。即使不是,i 指向的元素的内容也不太有帮助。

根本不清楚你想要的 ptr 是什么,但我很确定它不是这个。

This line

ptr = &(*i);

is dereferencing the element after the end of the filtered part of the sequence. If your filter didn't match any elements at all, so nothing was removed, then you're trying to dereference an iterator to one past the end of the vector, which will give the error you report. And even if not the contents of the element pointed to by i are not likely to be very helpful.

It's not at all clear what you want ptr to be, but I'm pretty sure it's not this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文