Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6358175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:19:03+00:00 2026-05-24T23:19:03+00:00

I have some dynamic int * arrays that I would like to use as

  • 0

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.

Also, to deallocate the memory for the arrays, do I use map.clear()?

Example:

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:19:04+00:00Added an answer on May 24, 2026 at 11:19 pm

    You won’t be able to stick int* as key inside a std::map and still be able to retrieve elements ‘by value’. The reason for that is a single int* only provides the start of an array, you need the end too to compute anything (equivalently and more C-like, the length). In other words, int* does not an array (dynamic or otherwise) make; in this context it’s an iterator to the start of a sequence.

    You can use std::pair<int*, int*> but it should only be used for non-owning ‘views’ of data. That is, if you manually manage memory with a std::map<std::pair<int*, int*>, int> you’ll end up with a headache. One possibility is using a smart-pointer: std::pair<std::unique_ptr<int[]>, int*>. But as others have recommended, just use std::vector as it is still compatible with C-like interfaces that deal with int*. Plus a const std::pair<std::unique_ptr<int>, int*> still allows you to scribble the memory, which can mess up the ordering of the map and ends you up in trouble.

    A final blow to using int* or std::unique_ptr<int[]> is that you would need to provide the strict weak ordering that std::map requires, whereas std::vector<int> comes with an appropriate operator<. On the other hand, you need to provide a hash for both if you settle instead on std::unordered_map. For what it’s worth, a simple functor that uses std::lexicographical_compare (same semantics as std::vector comparison):

    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);
        }
    };
    

    Then you can use std::map<std::pair<std::unique_ptr<int[]>, int*>, int, compare>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was helping a friend with some C++ homework. I warned said friend that
I have a class that contains a dynamically allocated array, say class A {
What are some of the technical differences between memory that is allocated with the
I've just started combining my knowledge of C++ classes and dynamic arrays. I was
I am a C++ programmer and recently joined a new company that uses a
I'm doing some work with stats, and want to refactor the following method :
I am implementing a templated sparse_vector class. It's like a vector, but it only
I have two algorithms written in C++. As far as I know, it is
This is my first post, so please be gentle. I've been playing around with
I'm trying to walk through a bunch of items, each item has an array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.