std::find on a vector of object pointers

前端 未结 3 1587
旧时难觅i
旧时难觅i 2021-01-24 14:48

I have a class A with a member which is a vector of object pointers of another class B

class A
{
    std::vector m_member_A
<         


        
3条回答
  •  粉色の甜心
    2021-01-24 15:34

    If you want to compare values instead of comparing pointers, you might want to use std::find_if instead:

    bool IsFoo (B* _item) {
        bool result = false;
    
        if ( _item != nullptr && _item->value == 1 ) //Whatever is your criteria
            result = true; 
    
        return result;
    }
    
    std::vector m_member_A;
    B* instance = std::find_if (m_member_A.begin(), m_member_A.end(), IsFoo);
    

提交回复
热议问题