C++ Arrays का परिचय
In this page:
data_type array_name[size];
data_type array_name[size] = {value1, value2, value3};
array_name[index]; // access an element
Array क्या है?
एक array same data type की कई values को एक variable नाम के तहत group करता है, memory में contiguously साथ store किया गया, यही वजह है कि किसी भी element को इसकी position से access करना तेज़ और predictable दोनों है।
यह contiguous layout array और दूसरे collection types के बीच key difference है जो अपने elements को memory में बिखेर सकते हैं।
उदाहरण: What is an Array?
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int scores[3] = {90, 85, 78};
// Print to the console with cout
std::cout << scores[0] << " " << scores[1] << " " << scores[2] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Arrays Declare और Initialize करना
आप curly braces इस्तेमाल करके एक single statement में एक array declare और भर सकते हैं, जैसे int scores[] = {90, 85, 78};, और अगर आप brackets में size छोड़ दें, compiler इसे decide करने के लिए automatically initializer values count करता है।
अपनी initializer list से बड़ी एक size explicitly specify करना बाकी elements को zero-initialized छोड़ता है।
उदाहरण: Declaring and Initializing Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int scores[] = {90, 85, 78};
// Print to the console with cout
std::cout << scores[0] << " " << scores[1] << " " << scores[2] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Array Elements Access करना
Array indexing zero-based है, मतलब पहला element index 0 पर बैठता है और आखिरी size - 1 पर, खुद size पर नहीं — index size access करने की कोशिश array की actual memory से आगे पढ़ती है और एक helpful error की बजाय undefined behavior produce करती है।
यह off-by-one boundary array-heavy code में bugs के सबसे common sources में से एक है।
उदाहरण: Accessing Array Elements
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int scores[] = {90, 85, 78};
// Print to the console with cout
std::cout << "First: " << scores[0] << std::endl;
// Print to the console with cout
std::cout << "Last: " << scores[2] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Array Elements Modify करना
एक indexed position को सीधे एक नई value assign करना, जैसे scores[2] = 100;, वहाँ पहले जो भी store था उसे permanently overwrite कर देता है — arrays history preserve नहीं करते, इसलिए एक बार एक value replace हो जाए, original चला जाता है जब तक आपने इसे कहीं और save न किया हो।
उदाहरण: Modifying Array Elements
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int scores[] = {90, 85, 78};
scores[2] = 100;
// Print to the console with cout
std::cout << scores[2] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Arrays के जरिए Loop करना
एक standard indexed for loop या एक range-based for loop दोनों order में एक array के हर element में चल सकते हैं; indexed version additionally आपको हर element की position देता है, जो range-based version सीधे expose नहीं करता।
Indexed form तब चुनें जब आपको actually जानना हो कि एक element *कहाँ* है, सिर्फ इसकी value नहीं।
उदाहरण: Looping Through Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int scores[] = {90, 85, 78};
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Print to the console with cout
std::cout << scores[i] << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- 5 size के एक array में
arr[5]पढ़ना या लिखना, जब valid indexes 0 से 4 हैं, जो undefined behavior है। - एक uninitialized local array इस्तेमाल करना और इसके elements print करना, जो garbage values दिखाता है।
- एक array को दूसरे को
a = b;से assign करने की कोशिश करना, जो C++ में allowed नहीं है; इसकी बजाय element by element copy करें।
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