← Back to C++ Course | Chapter 16: Modern C++ | Lesson 2 of 9

C++ का Range-based for Loop

एक range-based for loop बिना आपके count किए एक collection के हर item से गुज़रता है, एक line में हर बच्चे को एक sticker देने की तरह। यह count करने से shorter और safer है।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. for (auto x : v) में & भूल जाना और सिर्फ एक copy modify करना।
  2. loop के अंदर container के elements add या remove करना।
  3. जब loop सिर्फ elements पढ़ता है तो const भूल जाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.