2

In the 1st code snippet below, I am trying to remove an element from a vector within a member function based on a static condition function fed into std::remove_if function. My problem here is that the input parameter uuid in removeVipAddress method cannot accessed within the condition function. What do you think I should do here to enable removal of an item from the vector based on the input parameter named uuid? Thanks. NOte: This is a follow up problem explained previously in Removing an item from an std:: vector

SNIPPET 1 (CODE)

void removeVipAddress(std::string &uuid)
{
          struct RemoveCond
          {
            static bool condition(const VipAddressEntity & o)
            {
              return o.getUUID() == uuid;
            }
          };

          std::vector<VipAddressEntity>::iterator last =
            std::remove_if(
                    mVipAddressList.begin(),
                    mVipAddressList.end(),
                    RemoveCond::condition);

          mVipAddressList.erase(last, mVipAddressList.end());

}

SNIPPET 2 (COMPILATION OUTPUT)

 $ g++ -g -c -std=c++11 -Wall Entity.hpp
 Entity.hpp: In static member function ‘static bool ECLBCP::VipAddressSet::removeVipAddress(std::string&)::RemoveCond::condition(const   ECLBCP::VipAddressEntity&)’:
 Entity.hpp:203:32: error: use of parameter from containing function
 Entity.hpp:197:7: error:   ‘std::string& uuid’ declared here
Community
  • 1
  • 1
F. Aydemir
  • 2,665
  • 5
  • 40
  • 60

2 Answers2

2

If you're using C++11 this could be done with a lambda:

auto last = std::remove_if(
     mVipAddressList.begin(),
     mVipAddressList.end(),
     [uuid]( const VipAddressEntity& o ){
          return o.getUUID() == uuid;
     });

The last parameter on that function call declares a lambda, which is an anonymous inline function. The [uuid] bit tells it to include uuid in the scope of the lambda.

There's a tutorial on lambdas here

Alternatively you probably want to provide a constructor & member function to your RemoveCond predicate (and implement it using operator() rather than a function named condition).

Something like this:

struct RemoveCond
{
    RemoveCond( const std::string& uuid ) :
    m_uuid( uuid )
    {
    }

    bool operator()(const VipAddressEntity & o)
    {
        return o.getUUID() == m_uuid;
    }

    const std::string& m_uuid;
};

std::remove_if( 
     mVipAddressList.begin(),
     mVipAddressList.end(),
     RemoveCond( uuid );
     );
obmarg
  • 9,369
  • 36
  • 59
1

If you don't have C++11 lambdas, you could express your RemoveCond as a functor:

struct RemoveCond
{
  RemoveCond(const std::string uuid) : uuid_(uuid) {}
  bool operator()(const VipAddressEntity & o) const
  {
          return o.getUUID() == uuid_;
  }
  const std::string& uuid_;
};

then pass an instance to std::remove_if:

std::remove_if(mVipAddressList.begin(),
               mVipAddressList.end(),
               RemoveCond(uuid));

BTW you removeVipAddress function should take a const reference:

void removeVipAddress(const std::string &uuid)
juanchopanza
  • 223,364
  • 34
  • 402
  • 480