Check if element is in the list (contains)

后端 未结 7 2045
南笙
南笙 2021-01-31 14:38

I\'ve got a list of elements, say, integers and I want to check if my variable (another integer) is one of the elements from the list. In python I\'d do:

my_list         


        
7条回答
  •  半阙折子戏
    2021-01-31 15:12

    Declare additional helper function like this:

    template 
    bool vectorContains(const vector& v, I& t)
    {
        bool found = (std::find(v.begin(), v.end(), t) != v.end());
        return found;
    }
    

    And use it like this:

    void Project::AddPlatform(const char* platform)
    {
        if (!vectorContains(platforms, platform))
            platforms.push_back(platform);
    }
    

    Snapshot of example can be found here:

    https://github.com/tapika/cppscriptcore/blob/b7f3d62747494a52a440482e841ffb016a3fc56e/SolutionProjectModel/Project.cpp#L13

提交回复
热议问题