0

lets say i have a structure like this:

struct customer{
    int c1;
    int c2;
    int c3;
};
customer phone[10];

phone[0].c1 = 1;
phone[0].c2 = 1;
phone[0].c3 = 1;

phone[1].c1 = 2;
phone[1].c2 = 2;
phone[1].c3 = 2;

so my question is how to remove phone[1] from the struct array?

thanks in advance.

1 Answer 1

2

The best you can do is to overwrite it.

This is a built-in array. So the other elements are not initialized. In the same way, once you write something to one of the elements it stays there until the array goes out of scope or you write something else there.

It would be best to do the same with a std::vector:

#include <iostream>
#include <vector>

int main()
{
    struct customer {
        int c1;
        int c2;
        int c3;
    };
    customer phone[10];

    phone[0].c1 = 1;
    phone[0].c2 = 1;
    phone[0].c3 = 1;

    phone[1].c1 = 2;
    phone[1].c2 = 2;
    phone[1].c3 = 2;

    std::vector<customer> phonev{{1,1,1},{2,2,2}};
    phonev.erase(phonev.begin()+1);

    return 0;
}

Then you can erase an element.

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

Comments

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.