-1

I am currently using an std::set<uintptr_t> list to store unique pointers in it. This list gets bigger with the time and when this list reaches x amount of entries it should delete the first entry, the other entries should move down one and make room for a new entry.

For example:

if (list.count >= 20)
{
    list.remove[0]; //remove the first entry from the list?
}

I know this code doesn't work but just for the logic so you know what I mean. Also the first entry would be empty then, would it be possible to move all other entries one down so the [0] entry isn't empty anymore?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • [`std::set::erase`](https://en.cppreference.com/w/cpp/container/set/erase) – 463035818_is_not_an_ai Dec 10 '21 at 10:04
  • 2
    Why are you using `uintptr_t` for pointers? Pointers to what? Why aren't you using actual pointers instead of an *integer* type (`uintptr_t` is an integer type, large enough to also hold pointer values)? That has a definite bad smell about it. – Some programmer dude Dec 10 '21 at 10:07
  • @Someprogrammerdude uintptr_t is an unsigned integer type that is capable of storing a data pointer. Which typically means that it's the same size as a pointer which is perfect for what I am doing. Regarding this question it doesn't matter what type it is. – Baseult Business Dec 10 '21 at 10:16
  • a set is not an array, hence it is not quite clear what you mean with "should move down one and make room for a new entry." – 463035818_is_not_an_ai Dec 10 '21 at 10:22
  • also "the first entry would be empty then", no the first entry would be removed, and the second element is the new first – 463035818_is_not_an_ai Dec 10 '21 at 10:23
  • frankly your question seems to be driven by some wrong assumptions. I suggest you to write the code and see what actually happens. – 463035818_is_not_an_ai Dec 10 '21 at 10:24
  • https://stackoverflow.com/questions/215557/how-do-i-implement-a-circular-list-ring-buffer-in-c Is it what you want to do ? – LittlePanic404 Dec 10 '21 at 10:37
  • If you have a pointer, use a pointer. That makes the code clear, readable, understandable and maintainable. It also doesn't sidestep type-checking which is an important part of making sure that the code builds as expected and does what it's supposed to do. There must be a reason you picked `uintptr_t`, what is that reason? What problem did that solve for you? Perhaps it's not the only solution? Perhaps there are better and more "C++-ish" solutions that can work *with* the type-system rather than site-stepping it? If I saw that as your teacher or mentor or colleague I would fail it. – Some programmer dude Dec 10 '21 at 12:25
  • And I'm sorry for nagging about it, but it's really an important issue. Good code leads to good programs. Shortcuts and being "smart" usually leads to the opposite. – Some programmer dude Dec 10 '21 at 12:26

1 Answers1

0

You can get the iterator of your first element in your list with list.begin()

if (list.count >= 20)
{
    list.erase(list.begin()); //remove the first entry from the list
}
cic
  • 106
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 10 '21 at 11:13
  • this won't compile. `[]` -> `()` – 463035818_is_not_an_ai Dec 10 '21 at 11:29
  • edit in code list.erase(list.begin()) thx @463035818_is_not_a_number – cic Dec 10 '21 at 11:32