Thread: C++ 11: remove from array?

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    9

    C++ 11: remove from array?

    Hello everyone, I think I've read somewhere that C++ would allow me to use a 'remove' function to get rid of an element of the array?

    Am I right?

    What would be the syntax for something like that?

    Hopping to clarify more, what I would like to do is something like:

    int myarr = [1,2,3,4];
    myarr.remove(2);

    result in something like this: myarr[1,3,4]


    Thank you.
    Last edited by ricarod; 09-26-2016 at 09:08 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not sure what you mean exactly... the array container does not have erase capabilities. But if your container can erase, its member functions for that has a consistent interface.
    Code:
    vector<type> v;
    v.erase(v.begin() + 2); // erase item in position 2.
    v.erase(v.begin() + 2, v.end()); // erase items in the range of position 2 to the end.
    So you can erase a range of things or a single item in containers.

    Edit:
    I see. Wherever you heard this from, they were probably talking about the erase-remove idiom, and not everything (certainly not C arrays) can do it.
    Last edited by whiteflags; 09-26-2016 at 09:14 PM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just to clarify: You probably want to use vector as it is a dynamic array--its size can change at runtime. That way, you can add and remove elements from it, unlike static arrays (std::array, C-style arrays). Whiteflags has shown you how.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2016
    Posts
    9
    thank you @whiteflags

  5. #5
    Registered User
    Join Date
    Sep 2016
    Posts
    9
    @Elysia yeah, that's probably what I'm after. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. remove array element
    By Ash Mo Naiming in forum C Programming
    Replies: 6
    Last Post: 01-30-2013, 09:57 AM
  2. Remove elements from array
    By Xentor in forum C++ Programming
    Replies: 8
    Last Post: 09-24-2006, 07:47 PM
  3. trying to remove item from an array
    By rugger78 in forum C++ Programming
    Replies: 6
    Last Post: 10-15-2004, 10:26 AM
  4. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 04:53 AM
  5. How do I remove duplicate entries in an array?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-18-2002, 09:49 AM

Tags for this Thread