Friday, April 29, 2011

How to add element to C++ array?

i want to assign an int into an array, but the problem is i dont know what is the index now.

int[] arr = new int[15];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;

that codes work because i know what index i am assigning to,

but what if i dont know the index..

in php i can just do this arr[]=22; this will automatically add 22 to the next empty index of array.

but in c++ i cant do that, it gives me compiler error.

so what do you guys suggest ?

From stackoverflow
  • Arrays in C++ cannot change size at runtime. For that purpose, you should use vector<int> instead.

    vector<int> arr;
    // arr.size() will be the number of elements in the vector at the moment.

    As mentioned in the comments, vector is defined in vector header and std namespace. To use it, you should:

    #include <vector>

    and also, either use std::vector in your code or add

    using std::vector;


    using namespace std;

    after the #include <vector> line.

    Jon Cage : +1 -> I agree, vectors are by far the simplest way to do this. Don't forget you need: #include
    Bastien LĂ©onard : Also, use std::vector or add using std::vector before the instantiation.
  • Use a vector:

    #include <vector>
    void foo() {
        std::vector <int> v;
        v.push_back( 1 );       // equivalent to v[0] = 1
  • There is no way to do what you say in C++ with plain arrays. The C++ solution for that is by using the STL library that gives you the std::vector.

    You can use a vector in this way:

    std::vector< int > arr;
  • int arr[] = new int[15];

    The variable arr holds a memory address. At the memory address, there are 15 consecutive ints in a row. They can be referenced with index 0 to 14 inclusive.

    In php i can just do this arr[]=22; this will automatically add 22 to the next empty index of array.

    There is no concept of 'next' when dealing with arrays.
    One important thing that I think you are missing is that as soon as the array is created, all elements of the array already exist. They are uninitialized, but they all do exist already. So you aren't 'filling' the elements of the array as you go, they are already filled, just with uninitialized values. There is no way to test for an uninitialized element in an array.

    It sounds like you want to use a data structure such as a queue or stack or vector.

  • I fully agree with the vector way when implementing a dynamic array. However, bear in mind that STL provides you with a host of containers that cater to different runtime requirements.You should choose one with care. E.g: For fast insertion at back you have the choice between a vector and a deque.

    And I almost forgot, with great power comes great responsibility :-) Since vectors are flexible in size, they often reallocate automagically to adjust for adding elements.So beware about iterator invalidation (yes, it applies as well to pointers). However, as long as you are using operator[] for accessing the individual elements you are safe.

    Hao Wooi Lim : +1 for the automagically reference :)
  • I may be missing the point of your question here, and if so I apologise. But, if you're not going to be deleting any items only adding them, why not simply assign a variable to the next empty slot? Everytime you add a new value to the array, increment the value to point to the next one.

    In C++ a better solution is to use the standard library type std::list< type > which also allows the array to grow dynamically e.g.

    #include <list>
    std::list<int> arr; 
    for (int i = 0; i < 10; i++) 
        // add new value from 0 to 9 to next slot
    // add arbitrary value to the next free slot
  • If you are writing in C++ -- it is a way better to use data structures from standard library such as vector.

    C-style arrays are very error-prone, and should be avoided whenever possible.

  • everybody kindly answering my question with STL. i am also planning to delete the content of that array after it has been used. and put the new value to that empty or whatever next slot available. to keep it simple, i was thinking to keep in first in first out or first in last out. i dont know.

    ok i m going to try to apply vector to it.

    jilles de wit : Perhaps you could accept an answer then?
    fengshaun : for FIFO you can use std::queue and for LIFO you can use std::stack


Post a Comment