Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to remove an element in an array in c++?
Servers for Qt installer are currently down

How to remove an element in an array in c++?

Scheduled Pinned Locked Moved Unsolved C++ Gurus
13 Posts 7 Posters 2.9k Views 3 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.
  • I Offline
    I Offline
    ioanna
    wrote on 11 Mar 2020, 06:26 last edited by
    #1

    i have 10 elements in an array and i want to remove elements that is equal to zero

    int proc[] = {pr1,pr2,pr3,pr4,pr5,pr6,pr7,pr8,pr9,pr10};
    

    this is how i stored elements in an array.

    J 1 Reply Last reply 11 Mar 2020, 06:38
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 11 Mar 2020, 06:28 last edited by
      #2

      Any c++ relevance here? It's c++ basic...

      See for example std::remove_if()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      I 1 Reply Last reply 11 Mar 2020, 06:38
      5
      • C Christian Ehrlicher
        11 Mar 2020, 06:28

        Any c++ relevance here? It's c++ basic...

        See for example std::remove_if()

        I Offline
        I Offline
        ioanna
        wrote on 11 Mar 2020, 06:38 last edited by
        #3

        @Christian-Ehrlicher i am confuse sir, how do you apply that in an array to remove a value of 0. Sorry

        1 Reply Last reply
        0
        • I ioanna
          11 Mar 2020, 06:26

          i have 10 elements in an array and i want to remove elements that is equal to zero

          int proc[] = {pr1,pr2,pr3,pr4,pr5,pr6,pr7,pr8,pr9,pr10};
          

          this is how i stored elements in an array.

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 11 Mar 2020, 06:38 last edited by
          #4

          @ioanna may I suggest you use QVector instead, ? Internally it uses an array as well but it offers a great number of functions to use on it. removeAt and removeOne are part of it, and what you're looking for.

          Alternatively you can go full c:
          https://codeforwin.org/2015/07/c-program-to-delete-element-from-array.html

          or use the std classes @Christian-Ehrlicher pointed to


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          I T 2 Replies Last reply 11 Mar 2020, 06:48
          4
          • J J.Hilk
            11 Mar 2020, 06:38

            @ioanna may I suggest you use QVector instead, ? Internally it uses an array as well but it offers a great number of functions to use on it. removeAt and removeOne are part of it, and what you're looking for.

            Alternatively you can go full c:
            https://codeforwin.org/2015/07/c-program-to-delete-element-from-array.html

            or use the std classes @Christian-Ehrlicher pointed to

            I Offline
            I Offline
            ioanna
            wrote on 11 Mar 2020, 06:48 last edited by
            #5

            @J-Hilk Thank you! I will use QVector instead

            1 Reply Last reply
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 11 Mar 2020, 07:05 last edited by
              #6
              int a[] = {1, 2, 3, 4, 5};
              int arrayLenChange = std::end(a) - std::remove_if(std::begin(a), std::end(a), [](int v) { return v < 2; });
              qDebug() << arrayLenChange;
              for (int i = 0; i < 5 - arrayLenChange; ++i)
                  qDebug() << "a[" << i << "] =" << a[i];
              

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              4
              • G Offline
                G Offline
                Gerald.L.
                wrote on 12 Mar 2020, 07:56 last edited by Gerald.L. 3 Dec 2020, 07:56
                #7

                You should use std::vector or QVector instead of C array in CPP, then find suitable <algorithm> method/function to solve the problem.

                FYI, here is a quick reference:
                https://en.cppreference.com/w/cpp/algorithm

                1 Reply Last reply
                1
                • K Offline
                  K Offline
                  Kent-Dorfman
                  wrote on 13 Mar 2020, 22:54 last edited by
                  #8

                  The question itself if pretty simple, but to read between the lines I have to wonder whether the proc array is the correct structure for the use case in the first place. Certainly not enough information in the OP to know WHY it is necessary to remove elements from the array, or if a linked-list structure would be a better generic solution. If I were keeping a list of active processes that can go away at any time and I needed to reap them from the list I'd use a hash map instead of an array.

                  If you stick to the original question as-is then the most optimal way of removing an unkown number of elements from the array involves a linear scan and copy to a new array.

                  1 Reply Last reply
                  0
                  • C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 14 Mar 2020, 18:25 last edited by
                    #9

                    @Kent-Dorfman said in How to remove an element in an array in c++?:

                    If you stick to the original question as-is then the most optimal way of removing an unkown number of elements from the array involves a linear scan and copy to a new array.

                    No, it depends on a lot of variables!

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      Kent-Dorfman
                      wrote on 14 Mar 2020, 18:30 last edited by Kent-Dorfman
                      #10

                      @Christian-Ehrlicher said in How to remove an element in an array in c++?:

                      No, it depends on a lot of variables!

                      In what world is that not the fastest general solution, given the conditions as defined? If you have a linear array with randomized values in it and need to find and remove all the elements that match a specific value then there is absolutely NO QUICKER way than a linear scan and copy to a new array. Anything else infers predictions about the ordeing of the data that we cannot make with the given information.

                      Edit: if the order of elements in the result is not important then the shrink can be done in-place by overwriting elements to be removed with elements from the end of the array, and adjusting the reported array size accordingly...but I challenge anyone to find me a more efficient general algorithms. My testing shows that compared to std:remove_if, shrink-by-linear-copy is 3 times as fast, and shrink-by-element-replacement is 15 times as fast.

                      1 Reply Last reply
                      0
                      • C Online
                        C Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 16 Mar 2020, 17:58 last edited by
                        #11

                        When the element is movable and big enough I'm pretty sure your results will be much different.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        kshegunovK 1 Reply Last reply 16 Mar 2020, 19:53
                        0
                        • C Christian Ehrlicher
                          16 Mar 2020, 17:58

                          When the element is movable and big enough I'm pretty sure your results will be much different.

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on 16 Mar 2020, 19:53 last edited by kshegunov
                          #12

                          @Christian-Ehrlicher said in How to remove an element in an array in c++?:

                          When the element is movable and big enough I'm pretty sure your results will be much different.

                          If it's big enough (whether it's movable is irrelevant), then the standard swap in-place on each removal is going to perform the worst. Move semantics can't magically prevent a copy, it's just a hint to the compiler where it can elide the copy, but you can't cheaply reorder memory regardless of the compiler knowing or not knowing if the element is movable. Just sayin'.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          0
                          • J J.Hilk
                            11 Mar 2020, 06:38

                            @ioanna may I suggest you use QVector instead, ? Internally it uses an array as well but it offers a great number of functions to use on it. removeAt and removeOne are part of it, and what you're looking for.

                            Alternatively you can go full c:
                            https://codeforwin.org/2015/07/c-program-to-delete-element-from-array.html

                            or use the std classes @Christian-Ehrlicher pointed to

                            T Offline
                            T Offline
                            Tutorial Gateway
                            Banned
                            wrote on 19 Sept 2020, 04:53 last edited by
                            #13
                            This post is deleted!
                            1 Reply Last reply
                            -1

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved