Question:
c++ how to move elements of an array to the left once there is an empty spot?
anonymous
2013-02-05 14:15:33 UTC
in c++

A[5] has elements {a,b,c,d,e}

A[2] becomes empty, what is the the best way to move everything to the right so that they fill up the array partially leaving the empty spaces to the far left. I know how to do it with logic and loops, I was wondering if there are any commands that would do it for me?
Three answers:
godfatherofsoul
2013-02-05 14:50:33 UTC
Create a for loop from the empty index to array length - 2. At each iteration, set the current array value to the next array value. You subtract 2 because 1 is for array indices starting at 0 and 1 is for stopping at the 2nd to last place in the array:



array[i]=array[i+1];



You'll also ahve to figure out what to use for the last element since that's where the empty spot will be.
anonymous
2016-12-05 00:17:18 UTC
once you declare char str[43ec517d68b6edd315b3edc9a11367b43ec517d68b6edd315b3edc9a11367b] what you're extremely doing is allocating a block of 43ec517d68b6edd315b3edc9a11367b43ec517d68b6edd315b3edc9a11367b contiguous (sequential) characters, then putting str to point to that block. So, that is mostly a foul concept to later assign str to NULL or 0. As others have stated, you do no longer extremely desire to reinitialize, in basic terms reuse an analogous array and it will overwrite. even although, in case you're utilising the "array" as a string you may characteristic a '0' to the top after analyzing each line, so it's going to print out and artwork with string purposes fantastic. in case you genuinely need to initialize the array to three fee, the quickest way is with memset(): memset(str, 0, 43ec517d68b6edd315b3edc9a11367b43ec517d68b6edd315b3edc9a11367b); this might fill the memory block with zeroes (the 2nd argument). BTW, in case you're utilising C++ you in all probability desire to be taught on the subject of the STL (everyday template library) and use the two a vectora43ec517d68b6edd315b3edc9a11367b7deb43ec517d68b6edd315b3edc9a11367bc5f539e6bda3443ec517d68b6edd315b3edc9a11367b29c43ec517d68b6edd315b3edc9a11367bef23643ec517d68b6edd315b3edc9a11367b or string merchandise. that is lots less difficult and safer than utilising C-form arrays. to illustrate: string str = ""; str += 'a'; // append character 'a' to the top of the string printf("%sn", str.c_str()); // output with printf() cout << str << endl; // output with c++ form iostreams str.clean(); // clean the string.
David
2013-02-05 15:56:56 UTC
Consider using the following:



    #include

    #include

    #include



    template

    void remove(std::vector& v, const T val) {

        v.erase(std::remove(v.begin(),

               std::find(v.begin(), v.end(), val), val));

    }



    int main() {

        std::vector v{1, 2, 3, 4, 5, 6};



        remove(v, 3);



        for (auto &a : v) std::cout << a << " "; // 1, 2, 4, 5, 6

    }



Demo -- http://liveworkspace.org/code/CTLv2$4


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...