0

I need to keep an array of pointers in c++. I have a Set which keeps objects of my class. Then I need to iterate over the set and keep the pointer of each item in array. array is 2D by the way. so What I need in summary is : pointers[1][4] = PinterToAnItem; the reason is I need to delete the item later, So I want to keep the pointer and directly go to that and delete it, instead of iterate again and find and then delete. But I have an error in assigning the array. I dont know how this idea will work, but so far it gives me error. Please let me know if it is not clear

the error is : " a value of type CONST Movie cannot be assigned to type of Movie"

struct Movie
        {
            int movieId;
            int shopId;
            int priceId;
    
        };
    
    Movie *pointers[100][100];
    
    set<Movie> setMovie;
    
    void main()
    {
    
    //reading and initialize the set with movies
    // for example movie1 = {0,10,3} so I want to keep in my array a pointer to this object as:
    // pointer [0][10] = ObjectPointer
    // I have error in loop body. Also (*id), id and &id does not work. 
                for (auto id = setMovie.begin(); id != setMovie.end(); id++)
                    pointers[(*id).movieId][(*id).shopId] = &(*id);
    
    }

2- by the way, do you guys think it is a fine Idea? can I delete pointer[0][10] which points to an object in my set? so by this way I don't need to iterate again through the set and delete (0,10). I have a pointer to (0,10) and I erase it from the set. Thanks

EDITED

The answer from @Jeffry is right but it does not work for rest of my problem which is erasing the specified item. i intended to keep a pointer to an item and then erase it directly. But we know set.erase(ITERATOR) or (actual_value). so pointer here does not work for me. I had to change array of iterators, then it works. So I completed here maybe works for some later.

    set<Movie> ::iterator pointers[100][100];
    for (auto id = setMovie.begin(); id != setMovie.end(); id++)
                    pointers[(*id).movieId][(*id).shopId] = id; // I changed here as well
    setMovie[1].erase(pointers[1][0]); // works well 
// pointers[1][0] is an iterator to an item in set with movieid=1,shopid=0
2
  • Why do you need to manually delete the contents of pointers at all? set<Movie> will call all the Movie destructors when it gets destroyed, when the program ends. Commented Jul 26, 2021 at 3:25
  • 1
    Anyway, set has the requirement that its members are constant, to guarantee that once inserted it won't have to change the order later. The compiler is telling you that because &(*id) is a const Movie*, you can't assign it to a Movie*. If you absolutely insist on doing it this way--which, to be clear, you shouldn't, because it's a ton of effort and confusion that only seems to exist to allow you to try to double-delete things--you could change Movie *pointers[100][100]; to const Movie *pointers[100][100]; Commented Jul 26, 2021 at 3:28

1 Answer 1

2
pointers[(*id).movieId][(*id).shopId] = &(*id);

tries to store in pointers a pointer to a Movie, pointing into the setMovie.

Now consider what happens, after you do this. What if you did:

pointers[42][43]->priceId = 44;

That would possibly invalidate the set (you could have twice the same Movie). That is why the set doesn't let you do it.

One way around is:

const Movie *pointers[100][100];

Then, you could store the pointer because you wouldn't legally be allowed to modify movies, and mess up the ordering. I'm not sure that makes it a good idea, but it would solve your immediate problem.

For 2, no, it would not be a good idea to call delete on a pointer pointing to a movie stored in your set. You did not call new on this object, so you should not delete it. If you try, you'll get a crash immediately, or much later.

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

2 Comments

Thanks but let me explain a bit. the problem solved by adding const. But There is erase function on set. So we can delete an item. maybe I need to use Heap, priority_queue. but I need to delete an item and maybe again add it. So i dont want to iterate every time to find it and delete it. that's my main trick and idea. if it works programmatically, it would be enough for me. But again I have to delete movie(0,10) and again add, add delete multiple times. I use Set or any LOG(N) data structure to for some searching functions which is not related to this problem but I have to. thanks again
Ok, that clarifies it, but don't use the delete word unless you mean delete. It has a very specific meaning in C++. If you plan on calling set.erase with a reference to the object you want to delete it might work. Still not saying it's a nice design, but I'm not saying it will crash, if you meant erase instead of delete

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.