...making Linux just a little more fun!
Jimmy O'Regan [joregan at gmail.com]
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.
René Pfeiffer [lynx at luchs.at]
On Aug 07, 2008 at 1735 +0100, Jimmy O'Regan appeared and said:
> My C++ sucks, so I need a second opinion
I fully understand. http://www.chunder.com/text/stroustrup.html
> 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: > [...] > I'm just wondering if there's some subtle nuance I'm missing.
I got used to c++ vectors a while ago and so far I had no nasty surprises. I think you can switch to vectors without running into trouble.
Best, René, who is waiting for C++0x.
Jimmy O'Regan [joregan at gmail.com]
2008/8/8 René Pfeiffer <[email protected]>:
> On Aug 07, 2008 at 1735 +0100, Jimmy O'Regan appeared and said: >> My C++ sucks, so I need a second opinion > > I fully understand. http://www.chunder.com/text/stroustrup.html > > >> 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: >> [...] >> I'm just wondering if there's some subtle nuance I'm missing. > > I got used to c++ vectors a while ago and so far I had no nasty > surprises. I think you can switch to vectors without running into > trouble.
Great
I think I should change the assignments to use push_back - from what I've read, it's more efficient.