如何从STL容器实例获取元素类型? [英] How to get element type from STL container instance?

查看:535
本文介绍了如何从STL容器实例获取元素类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道value_type,key_type ...但它们操作类型,而不是实例。
我试过
的东西喜欢:

I know about value_type, key_type... but they operate on types, not on instances. I tried stuff like :

std::set<uint64_t> mySet;   

decltype (mySet)::value_type pos;

但它不工作。

:我使用VS 2010.

I use VS 2010.

EDIT2:这个代码的prupose是获取一个类型给它boost :: lexical_cast<>
有解决方法,启用?
我想要这样的东西:

the prupose of this code was to get a type to give it to boost::lexical_cast<> is there a workaround that enables that ? I want something like this:

   mySet.insert(boost::lexical_cast<decltype(mySet)::value_type>(*it));
  // it is a iterator in vector of strings

EDIT3:this works: p>

EDIT3 : this works:

mySet.insert(boost::lexical_cast<decltype(mySet)::value_type>(*it));


推荐答案

decltype(mySet): :value_type 是正确的。确保在编译器中启用了C ++ 11模式。

decltype (mySet)::value_type is correct. Make sure you have C++11 mode enabled in your compiler. If you have, then it's a compiler bug.

一个可能的解决方法是使用身份元功能:

A possible workaround involves using the identity metafunction:

template <typename T>
struct identity { typedef T type; };

identity<decltype(mySet)>::type::value_type pos;

这篇关于如何从STL容器实例获取元素类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆