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 8307899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:38:30+00:00 2026-06-08T18:38:30+00:00

I need a data structure in which each element has a specific index but

  • 0

I need a data structure in which each element has a specific index but can also be retrieved using a key.

I need that data structure for model-view Programming in Qt.
On the one hand, the View asks for an element in a specific row.
On the other hand, the model wants to insert and modify elements with a given key.
Both operations should run in O(1).

Here is an example of what I want:

The View sees the following:

list[0]: "Alice", aged 22
list[1]: "Carol", aged 15
list[2]: "Bob", aged 23

The Model sees:

hash["Alice"]: "Alice", aged 22
hash["Bob"]: "Bob", aged 23
hash["Carol"]: "Carol", aged 15

My idea was the following: I have a QList<Value> and a QHash<Key, Value*>.
The hash points to the place in the list, where the corresponding element is stored.
This is the code to insert/edit values:

if (hash.contains(key))
    *hash[key] = value;
else
{
    int i = list.size();
    list << value;
    hash[key] = &list[i];
}

The problem is that this code does not always work.
Sometimes it works as expected, but it happens that the data structure is not consistent any more.
I suspect, it is because QList moves it’s content through memory because it allocates new space or something like that.

Operations which are important (should run in expected O(1)):

  • Insert key/value pair (appends the value to the end of the list)
  • Look up and modify a value using a key
  • Look up and modify a value using an index

Other operations which have to be possible, but don’t have to be possible in constant run time:

  • Delete an element by index
  • Delete an element by key
  • Insert in the middle of the array
  • Swap elements in the array / sort array
  • Get the index of a key

My two questions are:

  • Is there any data structure which does what I want?
  • If there is not, how could I fix this or is there a better approach?
  • 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-06-08T18:38:31+00:00Added an answer on June 8, 2026 at 6:38 pm

    Approach 1: Instead of the pointer, you can store the list index in the hash. Then you have one more indirection (from the hash, you get the index, then you retrieve from the list), but it is still O(1). The difference in speed should not be too much.

    Approach 2: Both the list and the hash operate with pointers. Then they will stay valid. However, deleting based on index or key will become O(n), as you have to find the object manually in the non-corresponding container.

    I also wonder how you want to solve the issue of deletion by index or insertion in the middle anyway. In both cases, the hash will point to wrong entries (both in your approach and Approach 1). Here you would be forced to go with Approach 2.

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

Sidebar

Related Questions

I need to have data structure, each element in which is accessible by pair
I need a data structure which can handle the following: date_from (datetime) date_to (datetime)
I need a data structure that holds unique values (like a set), but also
I need a data structure with the following properties: Each element of the structure
I need a data structure which will support the following operations in a performant
I am in need of a Javascript data structure which will allow me to
I am planning to work in TRIE data structure for which I need a
I am beginner in C++, I need to know which data structure to store
I need a data structure that acts like a SortedDictionary<int, double> but is sorted
I need to design a data structure that supports the following operations: search element

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.