English 中文(简体)
你们将如何使导师成为 has子的关键?
原标题:How would you make an iterator as a key of hash_map?

How would you make an iterator as a key of hash_map?
How would you define it under gcc, Microsoft c++?

E.g.

    vector<string>::iterator i;
    hash_map<vector<string>::iterator, int> h;

or

    list<string>::iterator i;
    hash_map<list<string>::iterator, int> h;

THis给出了一个错误,因为探测器没有被预先界定为扼杀和其他类型......

缩略语

最佳回答

如果你知道你做了些什么(例如,如果机器人从一个不可改装的集装箱中取走),你可以尝试利用以下事实:<代码>&*it。 每一要素应具有独特性:

typedef std::string my_container;
typedef my_container::const_iterator my_iterator;

struct IteratorHasher
{
  std::size_t operator()(const my_iterator & it) const
  {
    return hasher(&*it);
  }
private:
  std::hash<const char *> hasher;
};

使用:

int main()
{
   std::unordered_map<my_iterator, int, IteratorHasher> mymap;

   std::string hello("hello");
   mymap[hello.begin()] = 3;
}
问题回答

储存病媒探测器,将其用作相关集装箱的关键,这不是一个好的想法,因为病媒探测器不稳定,即它们在<条码>上失效,<条码>,<>remove,resize,push_back an so on (见ator-> 无效>。

指数在这方面更为安全:

hash_map<size_t, int> h;

您可以简单地将指数转换成一个炉子:

size_t index = ...
std::vector<std::string> vec(...);
std::vector<std::string>::iterator i = vec.begin() + index;

并重新编制索引:

index = i - vec.begin();

FWIW:

以这种方式使用探测器是NOT强力。 罐头在集装箱上的某些业务上被废弃。 你的过去——地图钥匙将从那时起失效。

我建议使用

hash_map<string, int> h;

vector<string*> i;
hash_map<string*, int> h;

或even

vector<shared_ptr<string> > i;
hash_map<shared_ptr<string>, int> h;




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签