C++ का Range-based for Loop
In this page:
for (data_type element : container) {
// use element
}
for (auto &element : container) {
// modify element
}
Arrays पर Iterate करना
एक range-based for loop बिना आपके एक index variable manage किए sequence में एक array या container के हर element पर iterate करता है, off-by-one bugs की एक पूरी class खत्म करते हुए जो एक traditional indexed loop में गलत तरीके से i < size लिखने से आती है।
उदाहरण: Iterating Over Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[3] = {1, 2, 3};
for (int x : arr) std::cout << x << " ";
// 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
auto Keyword Use करना
for (auto x : container) लिखना compiler को हर element का type automatically deduce करने देता है, जो तब बहुत matter करता है जब container एक ऐसा type रखता है जिसे spell out करना tedious हो, जैसे std::map<std::string, std::vector<int>>::value_type।
उदाहरण: Using auto Keyword
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2, 3};
// Range-based for loop: visit every element of nums
for (auto x : nums) std::cout << x << " ";
// 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
References से Elements Modify करना
Default से, loop variable हर element की एक copy है, इसलिए loop body के अंदर इसे modify करने का original container पर कोई असर नहीं होता।
loop variable को एक reference (auto&) declare करना इसे सीधे हर element से in place bind करता है, इसलिए आपके किए changes container में ही reflect होते हैं।
उदाहरण: Modifying Elements with References
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2, 3};
// Range-based for loop: visit every element of nums
for (auto &x : nums) x *= 2;
// Range-based for loop: visit every element of nums
for (auto x : nums) std::cout << x << " ";
// 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
const References से Read-Only Access
जब आपको सिर्फ elements पढ़ने हों -- खासकर बड़े वाले, जैसे strings या structs -- loop variable को const auto& declare करना हर element copy करने की cost से बचाता है और साथ ही accidental modification रोकता है, जिसे compiler एक error की तरह flag कर देगा अगर आप कोशिश करें।
उदाहरण: Read-Only Access with const References
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <string>
// Program execution starts in main()
int main() {
// Create a vector named words (a resizable array)
std::vector<std::string> words = {"hello", "world"};
// Range-based for loop: visit every element of words
for (const auto &w : words) std::cout << w << " ";
// 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
Initializer Lists
आप सीधे एक brace-enclosed initializer list पर range-for कर सकते हैं, जैसे for (int x : {1, 2, 3}), पहले एक named array declare किए बिना -- values के एक छोटे, fixed set पर inline quickly iterate करने के लिए handy।
उदाहरण: Initializer Lists
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
for (int x : {1, 2, 3}) std::cout << x << " ";
// 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
for (auto x : v)में&भूल जाना और सिर्फ एक copy modify करना।- loop के अंदर container के elements add या remove करना।
- जब loop सिर्फ elements पढ़ता है तो
constभूल जाना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: