Delete Elements in arrays
-
Hi Please How I can delete element from the struct that contain arrays This is My Code
struct Member { char name[50]; char address[30]; int number; }; struct Member member[100]; And i have this Function for delete elements But how i can complete it void delete_member () { int search; cout <<"Enter Your Number of member:"; cin >> search; for (int i=0;i<100;i++) { if (member[i].number == search) { /* what can i write here to complete the code */ } }
-*-*-*-*-*-*-*-*-* To Be Or Not To Be (KARFER) -*-*-*-*-*-*-*-*-*
-
Hi Please How I can delete element from the struct that contain arrays This is My Code
struct Member { char name[50]; char address[30]; int number; }; struct Member member[100]; And i have this Function for delete elements But how i can complete it void delete_member () { int search; cout <<"Enter Your Number of member:"; cin >> search; for (int i=0;i<100;i++) { if (member[i].number == search) { /* what can i write here to complete the code */ } }
-*-*-*-*-*-*-*-*-* To Be Or Not To Be (KARFER) -*-*-*-*-*-*-*-*-*
You can't remove elements in such an array. And even worse, your array will be initialized with garbage structures (except if you provide a default constructor to your structure). I suggest you take a look at a std::list, this will do what you are looking for (but you'll probably need some time to learn how to use it, look for some tutorials).
Cédric Moonen Software developer
Charting control [v1.2] -
Hi Please How I can delete element from the struct that contain arrays This is My Code
struct Member { char name[50]; char address[30]; int number; }; struct Member member[100]; And i have this Function for delete elements But how i can complete it void delete_member () { int search; cout <<"Enter Your Number of member:"; cin >> search; for (int i=0;i<100;i++) { if (member[i].number == search) { /* what can i write here to complete the code */ } }
-*-*-*-*-*-*-*-*-* To Be Or Not To Be (KARFER) -*-*-*-*-*-*-*-*-*
You cannot remove an element from a "hard-coded" array like that, you will need to either mark/tag the element as "removed" or copy the elements that you want to keep to a new array and remove the old one. but that sucks. If you need to add and remove elements to an "array", I suggest you have a look at ode>std::list (not
std::vector
).
Maximilien Lincourt Your Head A Splode - Strong Bad
-
Hi Please How I can delete element from the struct that contain arrays This is My Code
struct Member { char name[50]; char address[30]; int number; }; struct Member member[100]; And i have this Function for delete elements But how i can complete it void delete_member () { int search; cout <<"Enter Your Number of member:"; cin >> search; for (int i=0;i<100;i++) { if (member[i].number == search) { /* what can i write here to complete the code */ } }
-*-*-*-*-*-*-*-*-* To Be Or Not To Be (KARFER) -*-*-*-*-*-*-*-*-*
Since the array is allocated onto the stack you cannot delete (i.e. remove) items. However you can erase the info wherein, for instance:
...
for (int i=0;i<100;i++)
{
if (member[i].number == search)
{
member[i].number = -1; // Using -1 to mark 'deleted' items
memset( member[i].name, 0, 50); // erase name
memset( member[i].address, 0, 30); // erase address
break; // only if you're sure numbers are unique
}
}
...You can also use an old (soft removing) trick: swap the found item content with the (currently) last one and decrease the element count. BTW: don't use explicitely numbers (eg.
50
, ...) in code, use meaningful symbolic consts instead (e.g.NAME_SIZE
, ...). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Since the array is allocated onto the stack you cannot delete (i.e. remove) items. However you can erase the info wherein, for instance:
...
for (int i=0;i<100;i++)
{
if (member[i].number == search)
{
member[i].number = -1; // Using -1 to mark 'deleted' items
memset( member[i].name, 0, 50); // erase name
memset( member[i].address, 0, 30); // erase address
break; // only if you're sure numbers are unique
}
}
...You can also use an old (soft removing) trick: swap the found item content with the (currently) last one and decrease the element count. BTW: don't use explicitely numbers (eg.
50
, ...) in code, use meaningful symbolic consts instead (e.g.NAME_SIZE
, ...). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Otherwise I'll keep getting tons of
1.0
!!!! :-D Honestly, I rarely suggestSTL
usage to newbies.STL
is very good (in fact, state of the art if I have to compare it with, say MFC counterparts) but IMHO a lot of people find too hard theSTL
learning step and I'm afraid to suggest a rather advanced topic to a newbie (even if the benefits are worthy the effort). [added] Furthermore, I think aC++
programmer must be able to do some memory management before switching to container classes. If one just don't like the argument there's a pletora of managed languages... BTW Who cares about stupid1.0
of stupid guys ? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Otherwise I'll keep getting tons of
1.0
!!!! :-D Honestly, I rarely suggestSTL
usage to newbies.STL
is very good (in fact, state of the art if I have to compare it with, say MFC counterparts) but IMHO a lot of people find too hard theSTL
learning step and I'm afraid to suggest a rather advanced topic to a newbie (even if the benefits are worthy the effort). [added] Furthermore, I think aC++
programmer must be able to do some memory management before switching to container classes. If one just don't like the argument there's a pletora of managed languages... BTW Who cares about stupid1.0
of stupid guys ? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
is it better like that ? lol actually, i prefer confuse even newbies a bit with STL, rather than push then in dynalically allocating memory, then leading toward memory leaks... pushing people into STL will make then make the effort of learning it... since then, they will never look around anymore :) so, please, edit your post and slide a little STL note in it ;)
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
is it better like that ? lol actually, i prefer confuse even newbies a bit with STL, rather than push then in dynalically allocating memory, then leading toward memory leaks... pushing people into STL will make then make the effort of learning it... since then, they will never look around anymore :) so, please, edit your post and slide a little STL note in it ;)
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
toxcct wrote:
actually, i prefer confuse even newbies a bit with STL, rather than push then in dynalically allocating memory, then leading toward memory leaks...
If they shouldn't face memory allocation then is better a
C#
migration. On the other hand, if they want the metal then metal to them! :-Dtoxcct wrote:
pushing people into STL will make then make the effort of learning it... since then, they will never look around anymore
STL has the grat advantage of being rigorous. It turns out to be a (learning) disvantage for not-so-rigorous people :laugh:.
toxcct wrote:
o, please, edit your post and slide a little STL note in it
You know I'll never do that. :) BTW: who has changed the
1.0
vote(in fact was a2.0
) to my post (i.e. only the original poster has the power to?) :confused: :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hi Please How I can delete element from the struct that contain arrays This is My Code
struct Member { char name[50]; char address[30]; int number; }; struct Member member[100]; And i have this Function for delete elements But how i can complete it void delete_member () { int search; cout <<"Enter Your Number of member:"; cin >> search; for (int i=0;i<100;i++) { if (member[i].number == search) { /* what can i write here to complete the code */ } }
-*-*-*-*-*-*-*-*-* To Be Or Not To Be (KARFER) -*-*-*-*-*-*-*-*-*
-
-
toxcct wrote:
actually, i prefer confuse even newbies a bit with STL, rather than push then in dynalically allocating memory, then leading toward memory leaks...
If they shouldn't face memory allocation then is better a
C#
migration. On the other hand, if they want the metal then metal to them! :-Dtoxcct wrote:
pushing people into STL will make then make the effort of learning it... since then, they will never look around anymore
STL has the grat advantage of being rigorous. It turns out to be a (learning) disvantage for not-so-rigorous people :laugh:.
toxcct wrote:
o, please, edit your post and slide a little STL note in it
You know I'll never do that. :) BTW: who has changed the
1.0
vote(in fact was a2.0
) to my post (i.e. only the original poster has the power to?) :confused: :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
CPallini wrote:
BTW: who has changed the 1.0 vote(in fact was a 2.0) to my post (i.e. only the original poster has the power to?)
you prefer i go back to 2.0 ? lol ok, you got me... was me :doh: but hey, at least, the voting system has that better than the previous one, that if we want to change a vote, we still can...
CPallini wrote:
You know I'll never do that
why ?! :confused:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]