English 中文(简体)
动态阵列作为C++地图的关键
原标题:dynamic int * arrays as key for C++ map
  • 时间:2011-08-22 09:34:54
  •  标签:
  • c++

I have some dynamic int * arrays that I would like to use as keys for an unordered_map. I m a bit unclear on how I should declare the key type so it would actually be the value of the entire array.

此外,为了查找阵列的记忆,我是否使用<条码>。

例:

unordered_map<??, int> frequency;

while (some_condition) {

  int *my_array = new int[size];
  putSomeValuesToArray(my_array);
  frequency[my_array]++;
}

// to deallocate memory for the arrays in frequency?
最佳回答

您获得的标记是<代码>int*,作为<代码>std:map的关键,仍然能够按价值检索部件。 之所以如此,只是一个单独的<代码>int*,只是提供一个阵列的开端,你也需要最终来计算任何东西(相当于和更多的C类一样的长度)。 换言之,<代码>int*不构成一个阵列(动力或其他方式);在这种情况下,它成为起序号的保存人。

您可使用<代码>std:pair<int*, int*>,但只能用于对数据的非发表看法。 也就是说,如果你用<条码”对记忆进行人工管理:地图和设计;std:pair<int*, int*> int>,你用头痛填写。 一种可能性是使用智能标识:std:pair<std:unique_ptr<int[]> int*>。 但正如其他人所建议的那样,只是使用<代码> :vector。 因为它仍然符合处理<代码>int*的类似C接口。 加上“<代码>const :pair<std:unique_ptr<int> int*>,仍然使你能够播下记忆,因为记忆能够打破地图的次序,使你陷入困境。

使用<代码>int*>t*或std:unique_ptr<int[]>的最终打击是:你需要提供严格软弱的次序,即<代码>:要求,而std:vector<int> 随附适当的<代码>operator<。 另一方面,如果你在<条码>上填写,则你需要为两者提供一 has。 对于其价值而言,使用<代码>的简单校长: (same semantics as std:vector compar):

struct compare {
    typedef std::pair<std::unique_ptr<int[]>, int*> value_type;
    bool
    operator()(value_type const& lhs, value_type const& rhs) const
    {
        return std::lexicographical_compare(lhs.first.get(), lhs.second
                                           , rhs.first.get(), rhs.second);
    }
};

然后可以使用<代码>std:map<std:pair<std:unique_ptr<int[]> int*> int, compar>。

问题回答

Important: If you use a dynamically allocated object in a STL Container, then to deallocate the memory you need to walk the container and call delete (or delete[]) explicitly.

I would strongly suggest moving from int* to std::vector<int>, you would not have the issue of memory ownership any longer then.


为了宣布钥匙,通过模板参数的类型:

std::unordered_map<int*, Foo>
std::unordered_map<std::vector<int>, Foo>

当然,<代码>unordered_map 页: 1 页: 1

如果是,

int *p = new int[size];

and you want to make p as a key, then for you better choice will be to use std::set().

Also, you cannot simply clear() it; you may want to delete[] each element before, to avoid memory leak. If you don t want to individually delete[] it then, you can use shared_ptr (or other smart pointers) which will do the job for you.





相关问题
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?

热门标签