[TAG] Vectors vs. dynamic arrays

Jimmy O'Regan joregan at gmail.com
Thu Aug 7 20:35:21 MSD 2008


My C++ sucks, so I need a second opinion :)

We have a user who's using some oddball compiler that doesn't support
C99 type arrays, so I'm wondering if it's ok to replace this code:

``
  double classes_ocurrences[M]; //M = Number of ambiguity classes
  double classes_pair_ocurrences[M][M];
  double tags_estimate[N]; //N = Number of tags (states)
  double tags_pair_estimate[N][N];
''

with this:

``
  vector <double> classes_ocurrences(M); //M = Number of ambiguity classes
  vector <vector <double> > classes_pair_ocurrences(M, vector<double>(M));
  vector <double> tags_estimate(N); //N = Number of tags (states)
  vector <vector <double> > tags_pair_estimate(N, vector<double>(N));
''

As far as I know, it's functionally equivalent, and this test:

``
#include <vector>
#include <iostream>

using namespace std;

int main ()
{
        int N = 2;
        int a[N][N];
        vector < vector <int> > b (N, vector<int>(N));


        a[0][0] = 1;
        a[0][1] = 2;
        a[1][0] = 3;
        a[1][1] = 4;

        b[0][0] = 1;
        b[0][1] = 2;
        b[1][0] = 3;
        b[1][1] = 4;

        cout << "a: " << a[0][0] << " b: " << b[0][0] << endl;
        cout << "a: " << a[0][1] << " b: " << b[0][1] << endl;
        cout << "a: " << a[1][0] << " b: " << b[1][0] << endl;
        cout << "a: " << a[1][1] << " b: " << b[1][1] << endl;

        return 0;
}
''

gives:

``
a: 1 b: 1
a: 2 b: 2
a: 3 b: 3
a: 4 b: 4
''

as expected. I'm just wondering if there's some subtle nuance I'm missing.




More information about the TAG mailing list