C++ का Array Size
In this page:
data_type array_name[size] = {value1, value2};
int length = sizeof(array_name) / sizeof(array_name[0]);
Declaration पर Fixed Size
एक C-style array का size उस moment fixed हो जाता है जब यह declare होता है, square brackets के अंदर एक number की तरह specify किया गया, और std::vector के उलट, यह इसके बाद बढ़ या सिकुड़ नहीं सकता।
उदाहरण: Fixed Size at Declaration
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Print to the console with cout
std::cout << "Fixed size: 5" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
sizeof से Array का Size ढूंढना
एक array पर apply किया गया sizeof operator इसके occupy किए गए total bytes return करता है, और इसे sizeof(arr[0]) इस्तेमाल करके एक single element के size से divide करना यह compute करने का standard तरीका है कि इसमें कितने elements हैं।
उदाहरण: Finding an Array's Size with sizeof
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {1, 2, 3, 4, 5};
// Declare count and set it to sizeof(arr) / sizeof(arr[0])
int count = sizeof(arr) / sizeof(arr[0]);
// Print to the console with cout
std::cout << count << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Functions को Pass किए गए Arrays का Size
जब एक array को एक function parameter की तरह pass किया जाता है, यह एक pointer में decay हो जाता है, इसलिए उस function के अंदर sizeof original array की बजाय pointer का size report करता है, element count को अलग से pass करना ज़रूरी बनाते हुए।
उदाहरण: Size of Arrays Passed to Functions
#include <iostream>
void checkSize(int arr[]) {
std::cout << sizeof(arr) << std::endl; // reports pointer size, not array size
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
std::cout << sizeof(arr) << std::endl; // full array size
checkSize(arr);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modern Array Sizing के लिए std::size()
C++17 ने free function std::size() introduce किया, जो sizeof division trick के बिना सीधे एक array का element count return करता है, C-style arrays और STL containers दोनों पर काम करते हुए।
उदाहरण: std::size() for Modern Array Sizing
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {1, 2, 3, 4, 5};
// Print to the console with cout
std::cout << std::size(arr) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Sizeable Alternative की तरह std::vector
एक C-style array के उलट, एक std::vector internally अपना size track करता है और runtime पर बढ़ या सिकुड़ सकता है, और इसका .size() method हमेशा सीधे current element count return करता है।
उदाहरण: std::vector as a Sizeable Alternative
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named vec (a resizable array)
std::vector<int> vec = {1, 2, 3};
vec.push_back(4);
// Print to the console with cout
std::cout << vec.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक function के अंदर
sizeof(arr)इस्तेमाल करना जहाँ array pointer में decay हो चुका है, length की बजाय 8 देते हुए। sizeof(arr)को गलत element type से divide करना।int a[n];जैसा एक non-constant size इस्तेमाल करना और इसे standard C++ मानना (यह एक compiler extension है)।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings