-2

I have vector of struct partationPartItemTag_t type

typedef struct partationPartItemTag
{
    std::string partition;
    std::string partGroupId;
    std::string mathTuid;
    std::string partNumber;
    tag_t itemTag;
}partationPartItemTag_t;

I want to find element from vector having value for partition == 'a' and partGroupId == 'b'

I have written following code and its working fine with C++11 But now i want to modify this code as I do not have c++11 support

partationPartItemTag_t mi={'a', 'b'};
vector<partationPartItemTag_t> searchlist;
searchlist.push_back(mi);
vector<partationPartItemTag_t>::iterator flag = std::search(partationPartItemTagVec.begin(),partationPartItemTagVec.end(),searchlist.begin(),searchlist.end(),[](partationPartItemTag_t &x, partationPartItemTag_t &y){
    return ((x.partition.compare(y.partition) == 0) && (x.partGroupId.compare(y.partGroupId) == 0));  
});

Please help me to modify this code i saw few posts for function pointer but I am not able to convert this using function pointer Note: i want to chek value of 2 member in struct and I can not use lambda function or c++11

Thanks in advance.

user2991556
  • 115
  • 1
  • 10
  • 1
    possible duplicate of [Search for a struct item in a vector by member data](http://stackoverflow.com/questions/14225932/search-for-a-struct-item-in-a-vector-by-member-data) – Cory Kramer Jun 18 '15 at 11:53
  • If you cannot use C++11, then you cannot use the lambda function that you're using as the last argument in `std::search`. In that case, you have to make a functor `class`/`struct` with `operator()` defined, like in the above link, specifically [this answer](http://stackoverflow.com/a/14226007/2296458). – Cory Kramer Jun 18 '15 at 11:54
  • Also it's not perfectly clear from your question, but I think you want to use [`std::find_if`](http://en.cppreference.com/w/cpp/algorithm/find) instead of `std::search` – Cory Kramer Jun 18 '15 at 11:55
  • can i use find_if if i want to check 2 members in struct? if yes can you please help me doing this – user2991556 Jun 18 '15 at 11:58
  • You have a vector of struct, and you are looking for a specific struct that has some particular values for it's member variables? Then yes, you want `std::find_if` – Cory Kramer Jun 18 '15 at 11:59
  • yes you are right but I can not use lambda function now... so want to modify this code can you please suggect how to that using find_if without using lambda function i mean c++11 – user2991556 Jun 18 '15 at 12:02
  • Read my 2nd comment, I linked to [this answer](http://stackoverflow.com/a/14226007/2296458) by @RemyLebeau. They specifically gave an answer for before C++11. – Cory Kramer Jun 18 '15 at 12:04
  • possible duplicate of [How to use std::find/std::find\_if with a vector of custom class objects?](http://stackoverflow.com/questions/6939129/how-to-use-stdfind-stdfind-if-with-a-vector-of-custom-class-objects) – Blacktempel Jun 18 '15 at 12:06

2 Answers2

0

If you need to convert from C++11 to C++098 better option is to replace a lambda with a functor.

Define a functor compare as follows and call it in your algorithm in place of lambda.

struct  compare 
{
        bool operator(partationPartItemTag_t x, partationPartItemTag_t y) const
        {
            return ((x.partition.compare(y.partition) == 0) && 
             (x.partGroupId.compare(y.partGroupId) == 0) );        
        }
}
std::search(partationPartItemTagVec.begin(),
            partationPartItemTagVec.end(),
            searchlist.begin(),searchlist.end(),
compare(partationPartItemTag_t &x, partationPartItemTag_t &y) );
Steephen
  • 14,645
  • 7
  • 40
  • 47
0

A lambda is just a convenience functor that was introduced so that you can write these algorithms simpler.

But it's just a convenience. If you can't use them in this case, it's not the end of the world. After all, we couldn't for many many years. The solution is to just go back to using a functor:

struct SearchFor
{
    /* ctor omitted for brevity */
    std::string partition, partGroupId;

    bool operator()(const partationPartItemTag_t& elem) const 
    {
        return elem.partition == partition && 
               elem.partGroupId == partGroupId;
    }
};

And then use it in-line:

vector<partationPartItemTag_t>::iterator flag = 
    std::find_if(partationPartItemTagVec.begin(),
                 partationPartItemTagVec.end(),
                 SearchFor("a", "b"));
Barry
  • 286,269
  • 29
  • 621
  • 977