-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.cpp
More file actions
63 lines (55 loc) · 1.69 KB
/
iterator.cpp
File metadata and controls
63 lines (55 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// C++ Program to demonstrate the working of
// begin(), end(), rbegin(), rend(), cbegin(), cend(), crbegin(), crend()
#include <iostream>
#include <string> // for string class
using namespace std;
// Driver Code
int main()
{
// Initializing string`
string str = "godslayerfist";
// Declaring iterator
std::string::iterator it;
// Declaring reverse iterator
std::string::reverse_iterator it1;
cout<<"Str:"<<str<<"\n";
// Displaying string
cout << "The string using forward iterators is : ";
for (it = str.begin(); it != str.end(); it++){
if(it == str.begin()) *it='G';
cout << *it;
}
cout << endl;
str = "godslayerfist";
// Displaying reverse string
cout << "The reverse string using reverse iterators is "
": ";
for (it1 = str.rbegin(); it1 != str.rend(); it1++){
if(it1 == str.rbegin()) *it1='S';
cout << *it1;
}
cout << endl;
str = "godslayerfist";
//Displaying String
cout<<"The string using constant forward iterator is :";
for(auto it2 = str.cbegin(); it2!=str.cend(); it2++){
//if(it2 == str.cbegin()) *it2='G';
//here modification is NOT Possible
//error: assignment of read-only location
//As it is a pointer to the const content, but we can inc/dec-rement the iterator
cout<<*it2;
}
cout<<"\n";
str = "godslayerfist";
//Displaying String in reverse
cout<<"The reverse string using constant reverse iterator is :";
for(auto it3 = str.crbegin(); it3!=str.crend(); it3++){
//if(it2 == str.cbegin()) *it2='S';
//here modification is NOT Possible
//error: assignment of read-only location
//As it is a pointer to the const content, but we can inc/dec-rement the iterator
cout<<*it3;
}
cout<<"\n";
return 0;
}