0

For e.g. if we have an array in python arr = [1,3,4]. We can delete element in the array by just using arr.remove(element) ot arr.pop() and list will mutate and it's length will change and that element won't be there. Is there a way to do this is C ot C++?. If yes the how to do that?

7
  • Do you mean a [] array, or a STL array<>? Commented Jan 16, 2013 at 10:06
  • i mean just the standart list in python like so a = ['spam', 'eggs', 100, 1234]. I don't have a clue what's a STL array<> is. Commented Jan 16, 2013 at 10:12
  • ... C/C++ doesn't have a "standard list", and it certainly doesn't have Python's. Commented Jan 16, 2013 at 10:12
  • I just need to know how to do what I asked) can you help with that? Commented Jan 16, 2013 at 10:14
  • Not unless you can answer a very simple question. Commented Jan 16, 2013 at 10:16

4 Answers 4

5

I guess you're looking for std::vector (or other containers in the standard library).

Sign up to request clarification or add additional context in comments.

7 Comments

but how I implement such a method without the standart library? is there a way to do this in C?
@IgorZimenko You asked "C or C++" (and tagged just C++), so I answered for C++. In C, you'll have to implement this yourself. If you want to do that, just try it and if you hit concrete problems, ask new question(s) here on SO (tagged C). If you don't know where to start, reading a book on data structures might be in order.
@Angew: std::deque would be the closest approximation
@IgorZimenko How to implement without the standard library? You pretty much have to reimplement most of what std::vector does. (It's not really as difficult as it sounds, if you ignore the allocator business.)
@Abhijit I don't see where std::deque would be any more appropriate than std::vector. Python lists provide both random access ([]) and removal/insertion at arbitrary places. Not to mention things like l[3::3] = 3 * [ 0 ], to set every third element to 0.
|
1

Arrays in C are just pointers and they don't contains an information about their length. So, C-programmer should keep not only a pointer but it's length too. Here's a snippet to erase a particular value from "C-array"

/// \return a new length
int removeFromArray( int * array, int arrayLength, int elementToRemove ) {
    int i;
    for ( i = 0; i < arrayLength; i++ ) {
        if ( array[ i ] == elementToRemove ) {
            memmove( & array[ i ], & array[ i + 1 ], arrayLength - i - 1 );
            return arrayLength - 1;
        }
    }
    return arrayLength;
}

Comments

0

In C++ either you can use std::list or std::vector based on your mileage

If you need to implement in C, you need to write your double-link list implementation or if you wan't to emulate std::vector, you can do so by using memcpy and array indexing

4 Comments

Or std::array or std::deque.
@juanchopanza: I believe the closest stl data-structure to Python List would be std::deque
I am not completely sure, I haven't used python for a while. But there is a collections.deque data structure, which makes me wonder...
This post seems to suggest that the underlying structure is an array.
0

As I understand, you don't want or able to use standard library (BTW, why?).

You can look into the code of the vector template in one of STL implementations and see, how it's implemented.

The basic algorithm is simple. If you're deleting something from the middle of the array, you must shrink it after. If you're inserting into the middle - you must expand it before. Sometimes it involves memory reallocation and moving you array, by memcpy for example.

Or you can implement double-linked list as mentioned. But it won't behave like array.

1 Comment

No, I want to use standart library. But I'm very intrested in the algorithm that I can implement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.