# Create vector of floats in a for-loop

Show 80 post(s) from this thread on one page
Page 3 of 4 First 1234 Last
• 01-03-2013
laserlight
Quote:

Originally Posted by fclopez
How does one write an array of array of vectors.

What answer would you venture? Does it work? Why, or why not?
• 01-03-2013
fclopez
Code:

``` int iMax=50; int jMax = 20; const int kMax = 10; int x =5; int y; vector<int> someVector[kMax]; // my problem is here....I want to create an array of array of vectors. How does one declare it??? for (int i =0; i<iMax; i++ {   for(int j = 0; j<jMax; j++) {       for(int k = 0; k <kMax; k++) {         y = k * j * i * x;         //then do push_back(y) here       }   } }```
• 01-03-2013
laserlight
How would you create an array of arrays of integers?
• 01-03-2013
fclopez
Code:

``` int iMax=50;  int jMax = 20;  const int kMax = 10; int x =5; int y; vector<vector<vector<int>>> someVector(kMax); //no complains here  for (int i =0; i<iMax; i++) {     for(int j = 0; j<jMax; j++) {            for(int k = 0; k <kMax; k++) {                  y = k * j * i * x;                  someVector[i][j].push_back(y); //but it crashes here;            }     }  }```
• 01-03-2013
kmdv
For any i, someVector[i] has 0 elements, which means that someVector[i][j] does not exist.
And the most outer one should be initialised to iMax, not kMax.

Code:

```const int iMax = 50; const int jMax = 20; const int kMax = 10; int x = 5; int y; vector<vector<vector<int>>> someVector(iMax, vector<vector<int>>(jMax)); for (int i =0; i<iMax; i++) {     for(int j = 0; j<jMax; j++) {            for(int k = 0; k <kMax; k++) {                  y = k * j * i * x;                  someVector[i][j].push_back(y);        }     }  }```
• 01-04-2013
fclopez
Quote:

Originally Posted by kmdv
For any i, someVector[i] has 0 elements, which means that someVector[i][j] does not exist.
And the most outer one should be initialised to iMax, not kMax.

Code:

```const int iMax = 50; const int jMax = 20; const int kMax = 10; int x = 5; int y; vector<vector<vector<int>>> someVector(iMax, vector<vector<int>>(jMax)); for (int i =0; i<iMax; i++) {     for(int j = 0; j<jMax; j++) {            for(int k = 0; k <kMax; k++) {                  y = k * j * i * x;                  someVector[i][j].push_back(y);        }     }  }```

Thanks!
I am using my actual code. Say I would like to access what I just pushed back....

Code:

```for(int ithresh=th_begin; ithresh<th_end+th_interval; ithresh = ithresh+th_interval) {         for(int ichan=chMin; ichan<chMax+1; ichan++) {             for(int ipos=0; ipos<npos; ipos++) {                 sprintf(fname,"%s%d%s%s%s", fpath, ipos,file_pref, specifier,ext);                        TH2F *h2_dummy=createScan(fname, th_begin, th_end, th_interval, nchans);                 TH1F *h1_ichan = getCh(h2_dummy, ichan);                 count_at_threshold = (int) h1_ichan->GetBinContent(h1_ichan->FindBin(ithresh));                            delete h1_ichan;                 delete h2_dummy;                 vec_th.at(ithresh).at(ichan).push_back(count_at_threshold);                            }                    }     }```
then access the elements count_at_threshold

Code:

```int value; int ithresh = 20; int ipos = 0; for(int ichan=chMin; ichan<chMax+1; ichan++) {  value = vec_th.at(ipos).at(ithresh).at(ichan); //problem here. How does one access the elements "count_at_threshold" }```
• 01-04-2013
kmdv
If you want to access what you have just pushed back, you can call `some_vector.back()`. But in the case of your second loop, instead of:
Code:

`vec_th.at(ipos).at(ithresh).at(ichan);`
you might want:
Code:

`vec_th.at(ithresh).at(ichan).at(ipos);`
• 01-05-2013
fclopez
Quote:

Originally Posted by kmdv
If you want to access what you have just pushed back, you can call `some_vector.back()`. But in the case of your second loop, instead of:
Code:

`vec_th.at(ipos).at(ithresh).at(ichan);`
you might want:
Code:

`vec_th.at(ithresh).at(ichan).at(ipos);`

Thanks kmdv!
Code:

`vec_th.at(ithresh).at(ichan).at(ipos);`
gives me
Code:

```  terminate called after throwing an instance of 'std::out_of_range'   what():  vector::_M_range_check```
So I modify a bit my code to
Code:

```int value; for(int ichan=chMin; ichan<chMax+1; ichan++) {     for (int ipos=0; ipos<npos; ipos++) {             value = vec_th.at(ithresh-th).at(ichan).at(ipos);            } } //or even int value; for(int ichan=chMin; ichan<chMax+1; ichan++) {     for (int ipos=0; ipos<npos; ipos++) {             value = vec_th[ithresh][ichan][ipos];            } }```
still gives me
Code:

```terminate called after throwing an instance of 'std::out_of_range'   what():  vector::_M_range_check```
Thanks in advance for the help!
• 01-05-2013
kmdv
What about using a debugger? You access a non-existent vector element, that's all.
There are some possibilities which you have to check on your own:

Code:

``` assert(ithresh - th < vec_th.size()); int value; for(int ichan=chMin; ichan<chMax+1; ichan++) // It may be: `ichan < chMax`, but it depends on chMax {     assert(ichan < vec_th[ithresh-th].size());     for (int ipos=0; ipos<npos; ipos++)     {         assert(ipos < vec_th[ithresh-th][ichan].size());         value = vec_th[ithresh-th][ichan][ipos]; // Ensure that `ithresh >= th`     } }```
I would consider using assertions + operator[] http://www.cplusplus.com/reference/cassert/assert/.
• 01-06-2013
Elysia
Quote:

Originally Posted by kmdv
I would consider using assertions + operator[] assert - C++ Reference.

Unless there is a reason why one must not use exceptions, I don't see why you have to make life harder for yourself.

Btw, what are these functions createScan and getCh which returns stuff you must delete? Is there a reason you must delete the returned result? Can you rewrite them so that you don't have to call delete? If so, you should do so.
• 01-06-2013
kmdv
Quote:

Originally Posted by Elysia
Unless there is a reason why one must not use exceptions, I don't see why you have to make life harder for yourself.

I think it is pretty obvious that in his case, an attempt to access vector out of its range is a pure programming error.

Every language has different ways and tools for solving different problems, and C++ is not one of those like Java where obvious programming errors are reported via exceptions.

Whether it is harder... it depends; messages generated by assertions usually carry a little more information than std::out_of_range.
• 01-06-2013
Elysia
I agree that it's a programming error, but what I disagree on is that you have to teach newbies:
You know, you could make an out-of-bounds access here, therefore you must create an assert that checks that you're not doing an OOB access.
And newbies go: "WHAAAAAT? Why won't I just get an error? Why do I have to create some magic statement just because I'm accessing an array?"
Yes, asserts are very nice and yes, I agree, they should be used to protect against programming errors, but in this case, IMHO, it's just making things worse. We should not have to teach newbies what OOB access is. It should just "work" out of the box. Leave OOB to security courses.
Also, getting asserts right are more complicated than using at and relying on the exceptions to report the error. Most testing facilities tend to fail tests if an unexpected exception is thrown (which is a good thing™) and debuggers typically have the power to break on an unexpected exception.
I think most newbies would get the assert wrong, anyway, making it pointless.
• 01-06-2013
whiteflags
If you can't get boolean logic your career in programming is short lived.
• 01-06-2013
Elysia
Oh, the boolean part is easy. The rest isn't.
• 01-07-2013
kmdv
Quote:

Originally Posted by Elysia
Yes, asserts are very nice and yes, I agree, they should be used to protect against programming errors, but in this case, IMHO, it's just making things worse.

If your point is that in this particular exercise for this particular newbie, the .at() member function is a better choice than assertions, then I agree - all his exception handling is probably one handler printing the error and exiting the program, so it's perfectly fine to leave it for another exercise. However, I don't agree if this is a part of his (even first) application he wants to deploy.

Quote:

Originally Posted by Elysia
Also, getting asserts right are more complicated than using at and relying on the exceptions to report the error. Most testing facilities tend to fail tests if an unexpected exception is thrown (which is a good thing™) and debuggers typically have the power to break on an unexpected exception.
I think most newbies would get the assert wrong, anyway, making it pointless.

o_O

Since when are assertions "harder to get right" than handling exceptions?
Please show me at least one newbie who is able to properly code a member function which modifies two STL containers at a time and provides strong exception guarantee. Newbies are taught that exception handling is a great way for error reporting. As a result they think of it as a brilliant mechanism, making assumptions that their functions have strong exception guarantee (without knowing this term), leaving everything without proper handling, and then continuing execution of their program to "retry something".

The problem in reporting programming errors via exceptions is that you can accidentally catch such exceptions, and continue execution. You catch "std::out_of_range" and you think "WTF, what a stupid user entered a bad index!?".
Assertions cause immediate termination and will never change your exception guarantees.
Show 80 post(s) from this thread on one page
Page 3 of 4 First 1234 Last