c++ - 在 std::any 的 std::vector 中找到一个元素

标签 c++ c++17 stdany

我想检查一个元素是否存在于 vector 中。我知道下面的代码会检查它。

#include <algorithm>

if ( std::find(vector.begin(), vector.end(), item) != vector.end() )
   std::cout << "found";
else
   std::cout << "not found";

但我有任何类型的 vector 。即 std::vector<std::any> 我正在像这样将元素插入 vector 。

std::vector<std::any> temp;
temp.emplace_back(std::string("A"));
temp.emplace_back(10);
temp.emplace_back(3.14f);

所以我需要确定 vector 中是否存在字符串“A”。 std::可以在这里找到帮助吗?

到目前为止,我正在使用下面的代码来执行此操作

bool isItemPresentInAnyVector(std::vector<std::any> items, std::any item)
{
    for (const auto& it : items)
    {
        if (it.type() == typeid(std::string) && item.type() == typeid(std::string))
        {
            std::string strVecItem = std::any_cast<std::string>(it);
            std::string strItem = std::any_cast<std::string>(item);

            if (strVecItem.compare(strItem) == 0)
                return true;
        }
        else if (it.type() == typeid(int) && item.type() == typeid(int))
        {
            int iVecItem = std::any_cast<int>(it);
            int iItem = std::any_cast<int>(item);

            if (iVecItem == iItem)
                return true;
        }
        else if (it.type() == typeid(float) && item.type() == typeid(float))
        {
            float fVecItem = std::any_cast<float>(it);
            float fItem = std::any_cast<float>(item);

            if (fVecItem == fItem)
                return true;
        }
    }

    return false;
}

最佳答案

我想这应该很好用:

#include <vector>
#include <string>
#include <any>
#include <algorithm>
#include <iostream>

int main(){
    std::vector<std::any> temp;
    temp.emplace_back(std::string("A"));
    temp.emplace_back(10);
    temp.emplace_back(3.14f);

    int i = 10;//you can use any type for i variable and it should work fine
    //std::string i = "A"; 
    auto found = std::find_if(temp.begin(), temp.end(), [i](const auto &a){
        return typeid(i) == a.type() && std::any_cast<decltype(i)>(a) == i;
    } );

    std::cout << std::any_cast<decltype(i)>(*found);
}

或者使代码更通用和可重用:

#include <vector>
#include <string>
#include <any>
#include <algorithm>
#include <iostream>


auto any_compare = [](const auto &i){
    return [i] (const auto &val){
        return typeid(i) == val.type() && std::any_cast<decltype(i)>(val) == i;
    };
};

int main(){
    std::vector<std::any> temp;
    temp.emplace_back(std::string("A"));
    temp.emplace_back(10);
    temp.emplace_back(3.14f);

    //int i = 10;
    std::string i = "A";
    auto found = std::find_if(temp.begin(), temp.end(), any_compare(i));

    std::cout << std::any_cast<decltype(i)>(*found);
}

Live demo

重要说明:由于 std::any 类型的标准要求,这保证仅在单个翻译单元内工作(例如,相同的类型不需要在不同的翻译中具有相同的类型标识符单位)

关于c++ - 在 std::any 的 std::vector 中找到一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55057937/

相关文章:

c++ - OpenCV calibrateCamera - 断言失败 (nimages > 0 && nimages == (int)imagePoints1.total()

c++ - 在不发送任何内容的情况下获取函数的返回值

c++ - 通过 std::optional 标准化,我们可以停止在新代码中使用 nullptr 并弃用它吗?

c++ - `std::filesystem::path::operator/(/*args*/)` 没有按预期工作

c++ - 如何根据枚举参数返回不同的类型

c++ - 在模板类中编写的 lambda 函数不支持 restrict(...) 吗?

c++ - 对象正在初始化为不需要的值

c++ - 如何在需要旧式 unsigned char 的地方使用新的 std::byte 类型?

c++ - std::any_cast 不需要原始对象的类型

c++ - std::any 用于仅移动模板,其中 copy-ctor 内的 static_assert 等于编译错误,但为什么呢?