-
There are The Lot's of reason for it like Runtime Size Or Memory Allocation Based On My Data
-
like @Asperamanca said, you can't,
why not use std::vector ? thats the perfect substitution -
-
@Ketan__Patel__0011 said in How to Get Length Of Int* Array:
std::vector is good idea but some issues like Memory location access or Memory Allocation
what do you mean? vector is literally a heap allocated array
And Very difficult for Multidimensional
multidimensionalism is an illusion anyway but if you want the
[][]
orat().at()
syntax available to youstd::vector<std::vector<int>>
here, admittedly, you may get into differences between std::vector and good old c-array, memory wise
-
Application crashed When i used this Syntax for Vector
std::vector<std::vector<std::vector<int>>> Temp; Temp.at(0).at(0).at(0) = 10; /// My Application Crashed at this point Temp.at(0).at(0).at(1) = 20; Temp.at(0).at(0).at(2) = 30; Temp.at(0).at(0).at(3) = 40; cout << Temp[0][0][0]; cout << Temp[0][0][1]; cout << Temp[0][0][2]; cout << Temp[0][0][3];
What's Wrong in this ?
-
@Ketan__Patel__0011
you need to either reserve a size for your vectors or use the vector functions to append datapush_back, resize, reserve etc
https://en.cppreference.com/w/cpp/container/vector
or use initializer lists
-
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);
-
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;
-
@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/
-
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.
-
Thanks to All for your reply
i can't find the length of the Vector
-
@Ketan__Patel__0011 have you really searched ? ==> https://www.cplusplus.com/reference/vector/vector/size/
-
@Asperamanca
thanks for your replyBut Fixed Sized array not useful in my case
-
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
-
@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.
-
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
-
@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!