Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to Get Length Of Int* Array
Forum Updated to NodeBB v4.3 + New Features

How to Get Length Of Int* Array

Scheduled Pinned Locked Moved Solved C++ Gurus
26 Posts 8 Posters 5.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Ketan__Patel__0011K Ketan__Patel__0011

    @J-Hilk

    I was Try this But Application is Still Crashing

    	std::vector<std::vector<std::vector<int>>> Temp;
    
    	Temp.at(0).at(0).push_back(10);
    	Temp.at(0).at(0).push_back(20);
    	Temp.at(0).at(0).push_back(30);
    	Temp.at(0).at(0).push_back(40);
    
    	cout << Temp.at(0).at(0).at(0);
    	cout << Temp.at(0).at(0).at(1);
    	cout << Temp.at(0).at(0).at(2);
    	cout << Temp.at(0).at(0).at(3);
    
    
    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by J.Hilk
    #12

    @Ketan__Patel__0011

        std::vector<std::vector<std::vector<int>>> Temp;
    
        int targetSize = 4;
        Temp.resize(targetSize,std::vector<std::vector<int> >(targetSize,std::vector<int>(targetSize)));
    
        Temp.at(0).at(0).at(0) = 10;
        Temp.at(0).at(0).at(1) = 20;
        Temp.at(0).at(0).at(2) = 30;
        Temp.at(0).at(0).at(3) = 40;
    

    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    1 Reply Last reply
    3
    • Ketan__Patel__0011K Ketan__Patel__0011

      @J-Hilk

      I was Try this But Application is Still Crashing

      	std::vector<std::vector<std::vector<int>>> Temp;
      
      	Temp.at(0).at(0).push_back(10);
      	Temp.at(0).at(0).push_back(20);
      	Temp.at(0).at(0).push_back(30);
      	Temp.at(0).at(0).push_back(40);
      
      	cout << Temp.at(0).at(0).at(0);
      	cout << Temp.at(0).at(0).at(1);
      	cout << Temp.at(0).at(0).at(2);
      	cout << Temp.at(0).at(0).at(3);
      
      
      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #13

      @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

      was Try this But Application is Still Crashing

      	std::vector<std::vector<std::vector<int>>> Temp;
      
      	Temp.at(0).at(0).push_back(10);
      

      Yes of course, the 2 first vector are not defined!

      You could read this article to learn how to work with multidimensionalvectors => https://mklimenko.github.io/english/2019/08/17/multidimensional-vector-allocation/

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      2
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #14

        Maybe something like this helps:
        https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdvector/

        It also refers to std::array, which might be an alternative if you really only need fixed-sized arrays.

        Ketan__Patel__0011K 1 Reply Last reply
        1
        • Ketan__Patel__0011K Offline
          Ketan__Patel__0011K Offline
          Ketan__Patel__0011
          wrote on last edited by Ketan__Patel__0011
          #15

          Thanks to All for your reply

          i can't find the length of the Vector

          KroMignonK 1 Reply Last reply
          0
          • Ketan__Patel__0011K Ketan__Patel__0011

            Thanks to All for your reply

            i can't find the length of the Vector

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #16

            @Ketan__Patel__0011 have you really searched ? ==> https://www.cplusplus.com/reference/vector/vector/size/

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            Ketan__Patel__0011K 1 Reply Last reply
            0
            • A Asperamanca

              Maybe something like this helps:
              https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdvector/

              It also refers to std::array, which might be an alternative if you really only need fixed-sized arrays.

              Ketan__Patel__0011K Offline
              Ketan__Patel__0011K Offline
              Ketan__Patel__0011
              wrote on last edited by
              #17

              @Asperamanca
              thanks for your reply

              But Fixed Sized array not useful in my case

              1 Reply Last reply
              0
              • KroMignonK KroMignon

                @Ketan__Patel__0011 have you really searched ? ==> https://www.cplusplus.com/reference/vector/vector/size/

                Ketan__Patel__0011K Offline
                Ketan__Patel__0011K Offline
                Ketan__Patel__0011
                wrote on last edited by
                #18

                @KroMignon

                Yes i see this example

                They Are Declare Singla Dimension Vector and Add First 10 Element and Again Add 10 Element After the First 10th Element Index and in last step they are remove the element and display the size of vector not a length

                KroMignonK 1 Reply Last reply
                0
                • Ketan__Patel__0011K Ketan__Patel__0011

                  @KroMignon

                  Yes i see this example

                  They Are Declare Singla Dimension Vector and Add First 10 Element and Again Add 10 Element After the First 10th Element Index and in last step they are remove the element and display the size of vector not a length

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #19

                  @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

                  display the size of vector not a length

                  I don't understand what you want?!?
                  By simply reading std::vector() documentation, you could find yourself:

                  • vector::size(): Returns the number of elements in the vector.
                  • vector::capacity(): Returns the size of the storage space currently allocated for the vector, expressed in terms of elements.

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  Ketan__Patel__0011K 1 Reply Last reply
                  0
                  • KroMignonK KroMignon

                    @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

                    display the size of vector not a length

                    I don't understand what you want?!?
                    By simply reading std::vector() documentation, you could find yourself:

                    • vector::size(): Returns the number of elements in the vector.
                    • vector::capacity(): Returns the size of the storage space currently allocated for the vector, expressed in terms of elements.
                    Ketan__Patel__0011K Offline
                    Ketan__Patel__0011K Offline
                    Ketan__Patel__0011
                    wrote on last edited by
                    #20

                    @KroMignon

                    i want to find how many elements are in the My Vector

                    i was used vector::size() function but it is return to me total size of vector

                    for example

                    my Vector can store 10 elements but i am add only 6 elements

                    so my Vector length is 6 And Vector size is 10

                    KroMignonK Christian EhrlicherC 2 Replies Last reply
                    0
                    • Ketan__Patel__0011K Ketan__Patel__0011

                      @KroMignon

                      i want to find how many elements are in the My Vector

                      i was used vector::size() function but it is return to me total size of vector

                      for example

                      my Vector can store 10 elements but i am add only 6 elements

                      so my Vector length is 6 And Vector size is 10

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by
                      #21

                      @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

                      i want to find how many elements are in the My Vector

                      Here are a little code extract which should be self explaining:

                         std::vector<int> test;
                          qDebug() << "Size is" << test.size() << "/ Capacity is" << test.capacity();
                          test.reserve(10);
                          qDebug() << "Size is" << test.size() << "/ Capacity is" << test.capacity();
                          test.push_back(11);
                          qDebug() << "Size is" << test.size() << "/ Capacity is" << test.capacity();
                          test.push_back(11);
                          test.push_back(11);
                          qDebug() << "Size is" << test.size() << "/ Capacity is" << test.capacity();
                          test.pop_back();
                          qDebug() << "Size is" << test.size() << "/ Capacity is" << test.capacity();
                      

                      and output is:
                      Size is 0 / Capacity is 0
                      Size is 0 / Capacity is 10
                      Size is 1 / Capacity is 10
                      Size is 3 / Capacity is 10
                      Size is 2 / Capacity is 10

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      1 Reply Last reply
                      4
                      • Ketan__Patel__0011K Ketan__Patel__0011

                        @KroMignon

                        i want to find how many elements are in the My Vector

                        i was used vector::size() function but it is return to me total size of vector

                        for example

                        my Vector can store 10 elements but i am add only 6 elements

                        so my Vector length is 6 And Vector size is 10

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #22

                        @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

                        so my Vector length is 6 And Vector size is 10

                        No, thats wrong. The size is 6, the std::vector<> has no function length().

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        4
                        • Kent-DorfmanK Offline
                          Kent-DorfmanK Offline
                          Kent-Dorfman
                          wrote on last edited by Kent-Dorfman
                          #23
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • Kent-DorfmanK Offline
                            Kent-DorfmanK Offline
                            Kent-Dorfman
                            wrote on last edited by
                            #24

                            "length" of a multidimensional array is kind of a meaningless concept...and resizing those structures requires well defined conops of what it means to resize, and what to do with new elements, or elements that must be thrown away...AND...will almost always require complete data move to a new container of appropriate size.

                            While sometimes useful, a vector of vectors is nontrivial to manage correctly.

                            1 Reply Last reply
                            0
                            • JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #25

                              @Ketan__Patel__0011
                              I don't really understand just which "sizes" or "lengths" you are wanting to discover.

                              A vector of vectors is a jagged array, meaning that each sub-vector element in a parent vector has its own size/length/number of elements. It is not "rectangular" like C/C++ int array[outer_elements][inner_elements] would be. Also, unlike the C one, you can & do add elements at all levels dynamically, so the number of items change at runtime. You would have to iterate the vector to discover its child element sizes to discover the total size used. You can resize() and/or reserve() if you want to reserve a certain size (number of elements)/make it "rectangular", and thereby avoid needing to iterate to discover size if desired.

                              @Christian-Ehrlicher , @KroMignon , @J-Hilk have given you code/references for the above.

                              1 Reply Last reply
                              4
                              • X Offline
                                X Offline
                                xtofl
                                wrote on last edited by xtofl
                                #26

                                vector will solve allocation/deallocation while keeping contiguous storage. It is intended for 1D arrays; everything more dimensional needs to come from somewhere else; doing it yourself while not familiar with C++ is probably not the best idea.

                                Since it seems you're dealing with matrices, getting a dedicated, fast, reliable library is probably the best thing to do. I suggest looking into Eigen.

                                1 Reply Last reply
                                1

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved