How to remove an element from a dynamically allocated array of objects with operator -= without using std::vectors?

Stefan Stojkovski

Overloading the operator -=:

RentACar& operator-=(const Automobile& av)

In RentACar i have a dynamically allocated array of Automobile *a

So lets say that if any a has the same a.brand with av.brand then that a should be removed and the allocated memory freed for one object.

I thought sth. like if it doesnt find an object with the same brand it return *this, but if it does, it allocates space for a new automobile array of size of a-1, assigns objects of, skips the one with the same brand, but i dont know how to write it.

I don't know how to write with vectors and push back, anyway, i study for an exam and we never used vectors for whatever reason so i can't use them on the exam so i must do this even if it is a stupid implementation!

Thanks!

James Kanze

First, you have to find the object you want to remove: std::find_if is good for this, but if it is classwork, they may expect you to write your own implementation of a linear search. Once you've found the entry, you delete the pointer, and either set it to null (and ensure that all other logic works correctly when there are null pointers in the array), or shift all of the following entries down one. (std::copy could be used for the shift.) Of course, if you shift, you'll also have to keep track of where the valid entries in the array end.

There's not really any reason to create a new array when removing objects. Just keep track of the logical end of the array.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can you delete a array of pointers which points to a dynamically allocated objects

분류에서Dev

How to free dynamically allocated array in C in a for loop

분류에서Dev

how can i add and remove element dynamically from linear layout?

분류에서Dev

How can I remove duplicates in an array without using `uniq`?

분류에서Dev

How to remove whitespace in array element?

분류에서Dev

How to use $nin operator with objects in array

분류에서Dev

How to remove both instances of duplicated objects in an array

분류에서Dev

How to remove a simple specific key value pair from all objects inside an array

분류에서Dev

How to remove an array element in json-c?

분류에서Dev

How to remove an array element in json-c?

분류에서Dev

How to push an array into an array of objects using AngularJs

분류에서Dev

How find and remove DOM element from directive?

분류에서Dev

How to remove fastclick from a specific element?

분류에서Dev

Create Dynamically Allocated Array with Pointers to Structs C++

분류에서Dev

How to remove last node from linked list without using double pointer

분류에서Dev

std::find on vector of objects with overloaded == operator

분류에서Dev

Remove element from array then shift other elements over

분류에서Dev

How to dynamically remove all unwanted CSS and JS from page

분류에서Dev

How to remove and re-add specific objects by id into Array [jQuery]

분류에서Dev

how is Array object different from other objects

분류에서Dev

how to remove file details from multi array

분류에서Dev

How do I remove values from an array?

분류에서Dev

How to remove \0 from the array of strings

분류에서Dev

Create and remove HTML-Element dynamically

분류에서Dev

jQuery How to add/remove dynamically created elements from dynamically created divs?

분류에서Dev

How to combined array of objects one to another array using javascript

분류에서Dev

How to remove duplicate element based on combination of multiple attributes using xslt

분류에서Dev

How to remove an element from :class [Ljava.lang.String?

분류에서Dev

How to remove workspace switcher icon from launcher without disabling workspaces?

Related 관련 기사

  1. 1

    How can you delete a array of pointers which points to a dynamically allocated objects

  2. 2

    How to free dynamically allocated array in C in a for loop

  3. 3

    how can i add and remove element dynamically from linear layout?

  4. 4

    How can I remove duplicates in an array without using `uniq`?

  5. 5

    How to remove whitespace in array element?

  6. 6

    How to use $nin operator with objects in array

  7. 7

    How to remove both instances of duplicated objects in an array

  8. 8

    How to remove a simple specific key value pair from all objects inside an array

  9. 9

    How to remove an array element in json-c?

  10. 10

    How to remove an array element in json-c?

  11. 11

    How to push an array into an array of objects using AngularJs

  12. 12

    How find and remove DOM element from directive?

  13. 13

    How to remove fastclick from a specific element?

  14. 14

    Create Dynamically Allocated Array with Pointers to Structs C++

  15. 15

    How to remove last node from linked list without using double pointer

  16. 16

    std::find on vector of objects with overloaded == operator

  17. 17

    Remove element from array then shift other elements over

  18. 18

    How to dynamically remove all unwanted CSS and JS from page

  19. 19

    How to remove and re-add specific objects by id into Array [jQuery]

  20. 20

    how is Array object different from other objects

  21. 21

    how to remove file details from multi array

  22. 22

    How do I remove values from an array?

  23. 23

    How to remove \0 from the array of strings

  24. 24

    Create and remove HTML-Element dynamically

  25. 25

    jQuery How to add/remove dynamically created elements from dynamically created divs?

  26. 26

    How to combined array of objects one to another array using javascript

  27. 27

    How to remove duplicate element based on combination of multiple attributes using xslt

  28. 28

    How to remove an element from :class [Ljava.lang.String?

  29. 29

    How to remove workspace switcher icon from launcher without disabling workspaces?

뜨겁다태그

보관