What answer would you venture? Does it work? Why, or why not?Originally Posted by fclopez
What answer would you venture? Does it work? Why, or why not?Originally Posted by fclopez
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 } } }
How would you create an array of arrays of integers?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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; } } }
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....
then access the elements count_at_thresholdCode: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); } } }
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" }
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:
you might want:Code:vec_th.at(ipos).at(ithresh).at(ichan);
Code:vec_th.at(ithresh).at(ichan).at(ipos);
Thanks kmdv!
Following your suggestion
gives meCode:vec_th.at(ithresh).at(ichan).at(ipos);
So I modify a bit my code toCode:terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check
still gives meCode: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]; } }
Thanks in advance for the help!Code:terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check
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:
I would consider using assertions + operator[] http://www.cplusplus.com/reference/cassert/assert/.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` } }
Last edited by kmdv; 01-05-2013 at 06:42 AM.
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.
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.
Last edited by kmdv; 01-06-2013 at 11:28 AM.
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.
If you can't get boolean logic your career in programming is short lived.
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.
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.
Last edited by kmdv; 01-07-2013 at 08:37 AM.