Solved How to Get Length Of Int* Array
-
@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 -
@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().
-
This post is deleted! -
"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.
-
@Ketan__Patel__0011
I don't really understand just which "sizes" or "lengths" you are wanting to discover.A
vector
ofvector
s is a jagged array, meaning that each sub-vector
element in a parentvector
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 thevector
to discover its child element sizes to discover the total size used. You canresize()
and/orreserve()
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.
-
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.