← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 7 of 15

C++ के Iterators

एक iterator एक finger जैसा है जो एक collection में एक item की ओर point करता है और अगले पर move कर सकता है। यह algorithms को किसी भी तरह के container में चलने देता है।
Syntax
cpp
for (auto it = container.begin(); it != container.end(); ++it) {
  *it;  // current element
}

for (auto it = container.rbegin(); it != container.rend(); ++it) { }  // reverse

Iterators का Introduction

Iterators वे objects हैं जो एक container के elements में generalized pointers जैसे behave करते हैं, आपको sequentially elements में चलने और access करने का एक uniform तरीका देते हुए चाहे वह particular container internally कैसे भी implemented हो।

उदाहरण: Introduction to Iterators

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};
	std::vector<int>::iterator it = nums.begin();
	// Print to the console with cout
	std::cout << *it << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Reverse Iterators

Reverse iterators आपको एक container को इसके last element से पहले तक traverse करने देते हैं; एक reverse iterator increment करना actually इसे underlying container में backward move कराता है, जो exactly वह behavior है जिस पर std::sort(rbegin(), rend()) जैसे algorithms depend करते हैं।

उदाहरण: Reverse Iterators

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};
	// Loop: repeat while the condition holds
	for (auto it = nums.rbegin(); it != nums.rend(); ++it) {
		// Print to the console with cout
		std::cout << *it << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Const Iterators

Const iterators एक container के elements तक read-only access देते हैं, आपको data traverse और inspect करने देते हुए जबकि compiler guarantee करता है कि आप उस iterator के जरिए accidentally किसी underlying value modify न कर सकें।

उदाहरण: Const Iterators

cpp
#include <iostream>
#include <vector>

int main() {
	std::vector<int> nums = {1, 2, 3};
	for (auto it = nums.cbegin(); it != nums.cend(); ++it) {
		std::cout << *it << " ";
		// *it = 5; would fail to compile: read-only
	}
	std::cout << std::endl;
	return 0;
}

Iterator Arithmetic और Operations

vector जैसे random-access containers अपने iterators पर सीधे arithmetic support करते हैं — आप दो iterators के बीच distance compute कर सकते हैं या एक को constant time में एक arbitrary संख्या steps आगे बढ़ा सकते हैं, simpler forward-only iterators के उलट।

उदाहरण: Iterator Arithmetic and Operations

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 = {10, 20, 30, 40};
	// Declare it and set it to nums.begin()
	auto it = nums.begin();
	it += 2;
	// Print to the console with cout
	std::cout << *it << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Inserter Iterators

Inserter iterators, जैसे std::back_inserter से produced, उस position पर existing elements overwrite करने की बजाय एक container में नए elements insert करते हैं, जो std::copy जैसे algorithms को चलते हुए safely एक destination container बढ़ाने देता है।

उदाहरण: Inserter Iterators

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>

// Program execution starts in main()
int main() {
	// Create a vector named source (a resizable array)
	std::vector<int> source = {1, 2, 3};
	// Create a vector named destination (a resizable array)
	std::vector<int> destination;
	std::copy(source.begin(), source.end(), std::back_inserter(destination));
	for (int n : destination) std::cout << n << " ";
	// 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. end() dereference करना, जो last element से एक आगे point करता है और invalid है।
  2. container के modify और invalidate होने के बाद एक iterator इस्तेमाल करना।
  3. एक range में दो अलग containers के iterators मिलाना।

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.