Value_type容器模板参数

Value_type of a container template parameter

本文关键字:参数 type Value      更新时间:2023-10-16

在他今年的主题演讲《走向原生的本质C++》(转到40:30)中,Bjarne Stroustrup给出了以下代码示例:

template<typename C, typename V>
vector<Value_type<C>*> find_all(C& cont, V v) 
{
    vector<Value_type<C>*> res; 
    for (auto& x : cont) 
        if (x == v) 
            res.push_back(&x)
    return res;
}

此函数用于查找容器中某个值的所有匹配项并返回指向找到的元素的指针。视频中的示例:

string m{"Mary had a little lamb"}; 
for (const auto p: find_all(m,'a')) // p is a char*
    if (*p != 'a')
        cerr << "string bug!n"; 

我的问题是关于Value_Type<C>*.标准中有这样的东西吗图书馆?我找了找,没有找到。如果它不在 std 中,如何实现?

我不知道标准中是这样,但实现起来并不难:

    template <class C>
    struct value_type
    {
       typedef typename C::value_type type;
    };
    template <class T, int N>
    struct value_type<T[N]>
    {
       typedef T type;
    };
    template <class T>
    struct value_type<T*>
    {
      typedef T type;
    };

现在,您可以使用typename value_type<C>::type访问容器包含的类型。 如果您有自己的容器想要使用,但它没有value_type typedef(无论出于何种原因您无法更改它),那么您也可以简单地将此结构专用于该容器。

要避免typename ...::type您可以执行以下操作:

    template <class C>
    using Value_Type = typedef value_type<C>::type;

现在你只需在任何地方使用Value_Type<C>

编辑
正如 stefan 在 soon's 的回答中所建议的那样,您可以使用 std::begin更轻松地执行此操作,这没关系,因为您使用/创建的任何容器都希望能够调用 std::beginstd::end

    template <class C>
    using Value_Type = typename std::remove_reference<
        decltype(*std::begin(std::declval<
            typename std::add_lvalue_reference<C>::type>()))>::type;

这要简洁得多,尽管阅读起来有点密集。 它仍然比第一个选项更好,这将需要更少的自定义容器类型的样板代码。

Value_type<C>只是C::value_type的typedef。据我所知,标准库中没有这样的 typedef,但您可以自己定义它:

template <class T>
using Value_type = typename T::value_type;
template<typename C, typename V>
std::vector<Value_type<C>*> find_all(C& cont, V v)
{
    std::vector<Value_type<C>*> res;
    for (auto& x : cont)
        if (x == v)
            res.push_back(&x);
    return res;
}
int main()
{
    std::vector<int> v{1, 2, 3, 3, 5};
    for(const auto x: find_all(v, 3))
    {
        std::cout << *x << std::endl;
    }
}

但是,正如@stefan所建议的,这仅适用于标准容器。您可以使用std::begin函数(也是为数组定义的)来检索底层类型,它是如何在@GuyGreer的答案中实现的