Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在结构的向量中查找_C++_Algorithm_Find - Fatal编程技术网

C++ 在结构的向量中查找

C++ 在结构的向量中查找,c++,algorithm,find,C++,Algorithm,Find,我有一个结构和两个像这样的向量 struct CodeColor { int index; string color; }; std::vector<CodeColor> secret_code; std::vector<CodeColor> my_code; struct CodeColor { 整数指数; 字符串颜色; }; std::向量密码; std::vector my_代码; 我需要在secret\u code中的my\u code中搜索每个项目。

我有一个结构和两个像这样的向量

struct CodeColor
{
 int index;
 string color;
};


std::vector<CodeColor> secret_code;
std::vector<CodeColor> my_code;
struct CodeColor
{
整数指数;
字符串颜色;
};
std::向量密码;
std::vector my_代码;
我需要在
secret\u code
中的
my\u code
中搜索每个项目。我需要得到的是,对于
my_code

  • secret\u code
    中是否有与
    索引
    颜色
    匹配的项目
  • 如果没有,是否有仅与颜色匹配的项目
  • 不是以上两种中的任何一种

  • 实际上,我可以通过两个for循环来实现这一点,但我不喜欢这样做(考虑到时间复杂性)。我正在尝试使用
    查找if
    或任何其他方法。有什么建议吗

    您可以使用这种方法:

    #include <algorithm>
    #include <set>
    
    set<CodeColor> intersect;
    set_intersection(secret_code.begin(),secret_code.end(),my_code.begin(),my_code.end(), // sorted!
                      std::inserter(intersect,intersect.begin()));
    
    #包括
    #包括
    设置相交;
    设置交集(secret\u code.begin()、secret\u code.end()、my\u code.begin()、my\u code.end()、//排序!
    插入器(intersect,intersect.begin());
    
    在这种方式下,

    template< class InputIt1, class InputIt2, class OutputIt >
    OutputIt set_intersection( InputIt1 first1, InputIt1 last1,
                               InputIt2 first2, InputIt2 last2,
                               OutputIt d_first );
    
    template
    输出集\u交叉点(输入第一个1,输入最后一个1,
    InputIt2 first2,InputIt2 last2,
    先输出d_);
    
    构造从d_first开始的排序范围,该范围由排序范围[first1,last1]和[first2,last2]中的元素组成。第一个版本要求使用运算符对两个输入范围进行排序<
    因此,您需要为
    CodeColor
    重载
    。您可以编写一个类似于此演示示例中的函数的函数。矢量密码只需查看一次

    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    
    struct CodeColor
    {
        int index;
        std::string color;
    };
    
    enum class SEARCH_RESULT { NO_MATCH, PARTIAL_MATCH, EXACT_MATCH };
    
    SEARCH_RESULT find( const std::vector<CodeColor> &v, const CodeColor &value )
    {
        SEARCH_RESULT result = SEARCH_RESULT::NO_MATCH;
    
        auto it = std::find_if( v.begin(), v.end(),
                                [&value]( const CodeColor &c ) 
                                {
                                    return ( c.color == value.color );
                                } );
    
        if ( it != v.end() )
        {
            result = SEARCH_RESULT::PARTIAL_MATCH;
            it = std::find_if( it, v.end(),
                               [&value]( const CodeColor &c ) 
                               {
                                return ( c.index == value.index &&
                                         c.color == value.color );
                               } );
            if ( it != v.end() ) result = SEARCH_RESULT::EXACT_MATCH;
        }
    
        return result;
    }
    
    int main() 
    {
        std::vector<CodeColor> secret_code = 
        { 
            { 1, "Red" }, { 2, "Green" }, { 3, "Blue" }
        };
    
        std::vector<CodeColor> my_code =
        {
            { 2, "Green" }, { 1, "Blue" }
        };
    
        for ( const CodeColor &c : my_code )
        {
            std::cout << static_cast<int>( find( secret_code, c ) ) << ' ';
        }
        std::cout << std::endl;
    
        return 0;
    }
    

    您可以重写该函数,使其返回一对枚举数和相应的迭代器。

    是否允许在向量中重复CODECOLOR?是的,在那里重复color@Karoly Horvath:那么怎么办?索引是否真的与向量元素索引分离?很好的解决方案!这不需要一些运算符吗
    CodeColor
    上的重载?@Debasish Jana,你能解释一下吗?运算符<应该重载,以便CodeColorOP仍然必须区分2.和3,即部分匹配颜色和不匹配。@Jarod42在这种情况下,需要找到两组不同的集合_交集,一组是基于颜色和索引a的比较nd在第二次比较中,基于其他条件,自定义比较函数作为附加的最后参数提供,但查找_如果是O(N),它就是O(N^2),OP试图避免这种情况。@Will Briggs我从未说过是O(N^2)还是O(N)。我演示了如何在一个循环中确定是否存在匹配。
    2 1