Like Tree11Likes

Create vector of floats in a for-loop

This is a discussion on Create vector of floats in a for-loop within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by fclopez How does one write an array of array of vectors. What answer would you venture? Does ...

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,781
    How would you create an array of arrays of integers?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    682
    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);  
           }
        }
     }
    I never put signature, but I decided to make an exception.

  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
    682
    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);
    I never put signature, but I decided to make an exception.

  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
    682
    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 05:42 AM.
    I never put signature, but I decided to make an exception.

  10. #40
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    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
    682
    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 10:28 AM.
    I never put signature, but I decided to make an exception.

  12. #42
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    If you can't get boolean logic your career in programming is short lived.

  14. #44
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    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
    682
    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 07:37 AM.
    I never put signature, but I decided to make an exception.

Page 3 of 4 FirstFirst 1234 LastLast
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, 11:50 AM
  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, 11:32 AM
  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, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21