C++ 枚举作为C++;

C++ 枚举作为C++;,c++,map,hashtable,enumeration,C++,Map,Hashtable,Enumeration,下面的java方法返回哈希表的键作为枚举 Hashtable<String, Object> props = new Hastable<String, Object>(); // some code here public final Enumeration getPropertyURIs() { return props.keys(); } Hashtable props=newhastable(); //这里有一些代码 公共最终枚举getPropert

下面的java方法返回哈希表的键作为枚举

Hashtable<String, Object> props = new Hastable<String, Object>(); 

// some code here

public final Enumeration getPropertyURIs() {
    return props.keys();
}
Hashtable props=newhastable();
//这里有一些代码
公共最终枚举getPropertyURIs(){
返回props.keys();
}
我想把这个代码翻译成C++。p>
<> P> >更具体地说,如何实现C++中的相同函数,返回STD键的枚举::map?< /p> < P> A<代码> EnUM<代码> C++中只是常数集合。< /P> 你是说像这样的事吗

typedef std::unordered_map<std::string, boost::any> props_t;
props_t props;

std::vector<std::string> getPropertyURIs()
{
   std::vector<std::string> keys;
   for (props_t::const_iterator i = props.begin(); i != props.end(); ++i)
   {
      keys.push_back(i->first);
   }
   return keys;
}
typedef std::无序地图道具;
道具;
std::vector getPropertyURIs()
{
向量键;
for(props_t::const_迭代器i=props.begin();i!=props.end();++i)
{
按键。向后推(i->first);
}
返回键;
}

最接近的方法是返回迭代器。问题是,实际上需要两个迭代器来指定一个范围。解决此问题的一种方法是使用输出迭代器:

template<class output_iterator_type>
void getPropertyURIs(output_iterator_type out) {
    // loop copied from @dalle
    for (props_t::const_iterator i = keys.begin(); i != keys.end(); ++i)
    {
        *out = i->first;
        ++out;
    }
}

我不知道java,但是我怀疑C++中的枚举甚至不接近java中的枚举。C++中的代码>枚举< /Cords>只是列出一些常量的一种简便方法,给每个值一个唯一的值。在C++中,地图的键有固定的类型,只能说那种类型。(关键字类型当然可以是枚举。)可能是@ FMUX的复制:应用的解决方案,但问题是不同的。我不知道为什么C++ <代码>枚举< /COD>!Java
枚举
将进行讨论。java <代码> EnUM/C++ >与C++<代码> EnUM<代码>完全相同,但<代码>枚举<代码>完全不同,它仅在名称中包含3个相同的字母。在C++中,最接近于<代码>枚举< /C>的是迭代器。如果代码< GETPrimTyuriS> /Cux>将采用输出迭代器,则更喜欢它。它的类型是模板参数“输入”,并在其中写入键,而不是返回“代码>向量。这可能是OP认为他需要的,但这种东西可能只是C++中不需要的。如果您需要这些键,您只需直接在地图上重复……或调用<代码> FoeErh()< /COD>使用.@和安德烈卡隆的变体:从链接的页面中:这个函数对象是SGI扩展;它不是C++标准的一部分。@ BJO.RNBOLXEX:我知道。这就是为什么我说……这个函数对象是微不足道的。
std::vector<std::string> keys;
getPropertyURIs(std::back_inserter(keys));