【发布时间】:2010-03-04 04:49:55
【问题描述】:
我有一个 std::map 并且我正在使用迭代器来查找某个键值对。找到它后,我无法从迭代器中获取键值对的位置。通过做另一个 find 我可以得到它,但我想要解决这个问题。
//mycode is this
std::map<std::string,myclass*> mymap;
size_t myfind(const std::string &s)
{
std::map<std:string,myclass*>::iterator i=mymap.find(s);
if((i==mymap.end())||((*i).second==0))
{
std::cout<<"some error\n";
}
else
{
//here i need to return the size_t value of the iterator i
}
}
注意:将 size_t 编辑为键值对的位置
【问题讨论】:
-
“来自迭代器的 size_t 值”是什么意思? size_t 是 C 中的一种类型,它不是迭代器具有的值。请详细说明
-
迭代器的
size_t值是什么意思? -
size_t的价值是什么?您的地图使用std::string作为键和myclass *作为值。我在任何地方都没有看到任何size_t... -
您是否尝试将偏移量返回到找到的位置。该地图具有双向迭代器类型,您不能对它们进行指针类型算术运算。 sgi.com/tech/stl/BidirectionalIterator.html
-
@Sriram:我担心你想要那个。