unable to apply std::set_intersection on different types of structs with a common field

前端 未结 3 613
南方客
南方客 2021-01-12 14:57

I am trying to use use std::set_intersection to find common elements between 2 completely different types of data structures that have a common binding \'na

3条回答
  •  我在风中等你
    2021-01-12 15:26

    set_intersection isn't magic, if you have sorted vectors it is very easy to roll your own, roughly like this:

    auto ta = aStructs.begin();
    auto tb = bStructs.begin();
    while( ta != aStructs.end() && tb != bStructs.end() ){
        if( ta->mCommonField < tb->mCommonField ){
            ++ta;
        } else if( tb->mCommonField < ta->mCommonField ){
            ++tb;
        } else {
            std::cout << "found common: " << ta->mCommonField << std::endl;
            ++ta;
            ++tb;
        }
    }
    

提交回复
热议问题