C++ के Iterators
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Const Iterators
Const iterators एक container के elements तक read-only access देते हैं, आपको data traverse और inspect करने देते हुए जबकि compiler guarantee करता है कि आप उस iterator के जरिए accidentally किसी underlying value modify न कर सकें।
उदाहरण: Const Iterators
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Inserter Iterators
Inserter iterators, जैसे std::back_inserter से produced, उस position पर existing elements overwrite करने की बजाय एक container में नए elements insert करते हैं, जो std::copy जैसे algorithms को चलते हुए safely एक destination container बढ़ाने देता है।
उदाहरण: Inserter Iterators
// 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;
}
Login to try C/C++/Java/PHP code in the editor
end()dereference करना, जो last element से एक आगे point करता है और invalid है।- container के modify और invalidate होने के बाद एक iterator इस्तेमाल करना।
- एक range में दो अलग containers के iterators मिलाना।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: