dynamic arrays in cpp(and comparison with matlab)

hello,

my problem is that i have a code in matlab and i want to rewrite it in cpp

in the matlab code i have dynamic arrays
with that i mean that too many times rows or columns of the table are deleted or a new one is inserted

but in cpp we know that the dimensions of an array are constant

what do you suggest me to use??
vector or lists or do it with array??
Dynamic arrays are called vector in C++.
See http://www.cplusplus.com/reference/stl/vector/
What is this about? Could I see the matlab code?
vectors and why not lists??

an example is the following in matlab

for i=1:M
for k=1:A(i)
Mem_in(size(Mem_in,1)+1,:)=zeros(1,M+3); %here a new row is created
Mem_in(size(Mem_in,1),1)=max(Mem_in(:,1))+1;
Mem_in(size(Mem_in,1),2)=i;
Mem_in(size(Mem_in,1),3)=1;
Mem_in(size(Mem_in,1),4:M+3)=binornd(1,P(i,:));
end
end




other questions are
can i add a column in a vector table?
in matlab that can be done without writing much code
only with that
A(:,N+1)=1;

and generally what are the advadances of using vector and not list?
Last edited on
In general, vectors provide fast access to the data, while lists can be useful if you want to insert/delete elements very often. I assume that most of the time your array/table will remain at a certain size, therefore, as Athar said, vector is the way to go. Of course, depending on your problem, you could write a class that fits your needs better than any standard container.

I've written something for you to play with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <vector>
using namespace std;

ostream & print(const vector<int> & v);
ostream & print(const vector<vector<int> > & v);

int main()
{
    vector<int> array;

    array.resize(3);
    array[0]=6; array[1]=2; array[2]=5;

    cout << "your array is: ";
    print(array) << endl;

    cout << "\nlet's add some elements..." << endl;
    array.push_back(1); array.push_back(-1);

    cout << "\nyour array now is: ";
    print(array) << endl;

    vector<vector<int> > table;

    //two rows
    table.resize(2);

    //three columns (three elements per row)
    for (int i=0; i<2; i++) table[i].resize(3);

    table[0][0]=4; table[0][1]=3;
    table[1][0]=2; table[1][2]=5;

    cout << "\nyour table is:\n\n";
    print(table) << endl;

    cout << "let's add a column...\n";
    table[0].resize(4); //+1 element to the 1st row
    table[1].resize(4); //+1 element to the 2nd row
    table[0][3]=9;

    cout << "\nyour table now is:\n\n";
    print(table) << endl;

    cout << "hit enter to quit...";
    cin.get();
    return 0;
}

ostream & print(const vector<int> & v)
{
    for (int i=0; i<v.size(); i++)
        cout << v[i] << ' ';

    return cout;
}

ostream & print(const vector<vector<int> > & v)
{
    for (int i=0; i<v.size(); i++)
    {
        for (int j=0; j<v[i].size(); j++)
            cout << v[i][j] << ' ';

        cout << endl;
    }

    return cout;
}
Last edited on
ok thank you very much my friend!!
it was very helpfull
another question is
can i add a new vector as a new row in a two dimensional vector??
or i have to add each individual element?

for example..

int main()
{
vector<vector<int>> I;
vector<int> row;

for (int i=0;i<=5;i++)
{
row[i]=i;
}
I.push_back(row);
print(I);
}
Yes, you can add a whole vector as a new row.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//...
int main()
{
    //don't forget
    //the space here \/
    vector<vector<int> > I;
    vector<int> row;

    //don't forget to set
    //your vector's size
    row.resize(5);

    for (int i=0;i<5;i++) //i<5
        row[i]=i;

    I.push_back(row);
    print(I);

    cout << "hit enter to quit...";
    cin.get();
    return 0;
}
//... 

Unfortunately, you can only do that for one dimension (either rows or columns). See what you need more (adding new rows or new columns?) and set up your container to meet that need.
Last edited on

You do not need to call resize() when using push_back(); it will automatically extend the vector if necessary by reallocating it. You only need to call resize if you want to make the vector smaller, or if you want to add default constructed elements to the end.
 
void resize ( size_type sz, T c = T() );

You can give it an initial value to set the elements to, but if you leave it out, it just uses the default constructor. So in the above example, you do have to resize row if you want to go through and set the values using the index operator. Resize will go through and default initialize all elements of the vector though, and then you go through a second time and reset the values. It is better to call reserve to have the vector reserve sufficient space, but not do any initialization, and then go through and push_back the values. Even if you reserve space, it is an error to access anything outside of size, so if you have a vector with 5 elements, and you call reserve(10), you can still only access the 5 elements. To access more, you have to push_back, insert, or call resize.

Also, when you push_back a vector onto another vector, you are passing it by const reference, so unless the compiler can optimize it out, you are going to copy the entire vector, probably unnecessarily. You can push_back an empty vector, and then add the values to it to avoid the possibility of this.
1
2
I.push_back(vector<int>());
I[I.size()-1].push_back(5);  // push 5 onto the last vector of integers in I. 


On last thing: vector is a general purpose container. You may want to look at valarray for a container specifically meant to hold arithmetic types. Note that functions with the same name in valarray and vector do not necessarily do the same thing. Resize for example, will always wipe out any existing content for a valarray.

http://www.cplusplus.com/reference/std/valarray/valarray/

For multiple dimensions, you would probably want to use vector<valarray<double> > for example.
oik thank you very much!!
Topic archived. No new replies allowed.