Thread: Create vector of floats in a for-loop

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Registered User
    Join Date
    Dec 2012
    Posts
    12
    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
          }
       }
    }

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How would you create an array of arrays of integers?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Registered User
    Join Date
    Dec 2012
    Posts
    12
    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;       
           }
        }
     }

  5. #35
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    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);  
           }
        }
     }

  6. #36
    Registered User
    Join Date
    Dec 2012
    Posts
    12
    Quote Originally Posted by kmdv View Post
    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"
    
    }

  7. #37
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    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);

  8. #38
    Registered User
    Join Date
    Dec 2012
    Posts
    12
    Quote Originally Posted by kmdv View Post
    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!
    Following your suggestion
    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!

  9. #39
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    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/.
    Last edited by kmdv; 01-05-2013 at 06:42 AM.

  10. #40
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kmdv View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #41
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elysia View Post
    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.
    Last edited by kmdv; 01-06-2013 at 11:28 AM.

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #43
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you can't get boolean logic your career in programming is short lived.

  14. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh, the boolean part is easy. The rest isn't.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #45
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elysia View Post
    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 View Post
    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.
    Last edited by kmdv; 01-07-2013 at 08:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you create an array/vector of resizable containers?
    By LyTning94 in forum C++ Programming
    Replies: 9
    Last Post: 03-07-2012, 12:50 PM
  2. Faster squaring for a vector of floats
    By onako in forum C++ Programming
    Replies: 6
    Last Post: 09-04-2010, 10:00 AM
  3. can we create instance of a vector?
    By chintugavali in forum C++ Programming
    Replies: 6
    Last Post: 12-20-2007, 12:32 PM
  4. Custom Vector template: Want to create erase/clear function
    By Bird Killer in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:37 AM
  5. Call to new to create objects that go into a vector
    By matth in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2006, 02:59 PM