Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Delete Elements in arrays

Delete Elements in arrays

Scheduled Pinned Locked Moved C / C++ / MFC
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KARFER
    wrote on last edited by
    #1

    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) -*-*-*-*-*-*-*-*-*

    C M C K 4 Replies Last reply
    0
    • K 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) -*-*-*-*-*-*-*-*-*

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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]

      1 Reply Last reply
      0
      • K 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) -*-*-*-*-*-*-*-*-*

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • K 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) -*-*-*-*-*-*-*-*-*

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          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.

          T 1 Reply Last reply
          0
          • C CPallini

            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.

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            think STL buddy, think STL ! :-D


            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            C 1 Reply Last reply
            0
            • T toxcct

              think STL buddy, think STL ! :-D


              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              Otherwise I'll keep getting tons of 1.0!!!! :-D Honestly, I rarely suggest STL 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 the STL 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 a C++ 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 stupid 1.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.

              T 1 Reply Last reply
              0
              • C CPallini

                Otherwise I'll keep getting tons of 1.0!!!! :-D Honestly, I rarely suggest STL 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 the STL 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 a C++ 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 stupid 1.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.

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                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]

                C 1 Reply Last reply
                0
                • T toxcct

                  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]

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  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! :-D

                  toxcct 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 a 2.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.

                  T 1 Reply Last reply
                  0
                  • K 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) -*-*-*-*-*-*-*-*-*

                    K Offline
                    K Offline
                    KARFER
                    wrote on last edited by
                    #9

                    Thank you for all things

                    -*-*-*-*-*-*-*-*-* To Be Or Not To Be (KARFER) -*-*-*-*-*-*-*-*-*

                    C 1 Reply Last reply
                    0
                    • K KARFER

                      Thank you for all things

                      -*-*-*-*-*-*-*-*-* To Be Or Not To Be (KARFER) -*-*-*-*-*-*-*-*-*

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #10

                      KARFER wrote:

                      To Be Or Not To Be (KARFER)

                      Are you sure that is yours? :-D

                      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.

                      1 Reply Last reply
                      0
                      • C CPallini

                        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! :-D

                        toxcct 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 a 2.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.

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #11

                        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]

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups