For an array in style C, you can determine the size of the array in the following ways: *Not available for pointers (such as receiving dynamic assignments).
int a[7];
std::cout << "Length of array = " << (sizeof(a)/sizeof(*a)) << std::endl;
In most container classes,
The method is already defined.
std::array always has the same size() and max_size(/a).
#include <array>
int main(){
array<int, 7> myarr = {1,2,3,4,5};
cout << myarr.size() << endl;
}
std::vector lets you determine the number of elements in the current vector with vector:size().
#include <vector>
int main(){
vector<int> myvector;
myvector.insert(myvector.end(), 1);
myvector.insert(myvector.end(), 2);
myvector.insert(myvector.end(), 3);
cout << myvector.size() << endl;
}
© 2024 OneMinuteCode. All rights reserved.