0

I mean not keep pointer on array

std::vector<int*> vector;

but

std::vector<int[]> vector;

The problem is to keep such array in hash_map in order to compare not pointers when Insert there but when I try like this

std::hash_map<std::vector<BYTE>,std::string> xxx

I've got an error.

14
  • 1
    I have a hard time understanding your question, could you please rephrase it? Also note that a vector<int[]> is exactly the same thing as a vector<int*>. Did you maybe mean a vector<int[N]> (where N is a compile-time constant)? Commented Jan 9, 2012 at 22:51
  • 1
    The hash map in C++ is called unordered_map, and this works fine for me: std::unordered_map<std::vector<unsigned char>, std::string> Commented Jan 9, 2012 at 22:52
  • 1
    std::unordered_map<std::array<unsigned char, N>, std::string> is a possibility too, since the array is presumably statically-sized. Commented Jan 9, 2012 at 22:53
  • vector<int[3]> v; v.push_back({1, 2, 3}); This even makes GCC segfault :) Commented Jan 9, 2012 at 22:55
  • even with unordered_map I've got an error cannot convert from 'const std::vector<_Ty>' to 'size_t' Commented Jan 9, 2012 at 23:00

1 Answer 1

2

You cant do std::vector<int[]> vector. You have to specify the size of the array for it to compile like this std::vector<int[5]> vector.

However, this is a bad idea because you can`t assign arrays to other arrays, etc. and you will get all kinds of errors when you try to use vector.

Instead, use vector<vector<int>> vector or in C++11 use vector<std::array<int, 5>> vector.

Also, I don't know what implementation of hash_map you are using so I dont know if the above solutions will work in your case. (Also, C++11 has unordered_map, so that might be preferable)

Sign up to request clarification or add additional context in comments.

3 Comments

What's C++11x? I know C++0x and C++11 :)
so put my vector into another vector?
@Артём: Yes, that might work, but I don't know if the key for hash_map can be a container or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.