-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrangeBasedForLoop.cpp
54 lines (40 loc) · 1.45 KB
/
rangeBasedForLoop.cpp
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
#include <cctype>
#include <iostream>
#include <map>
#include <string>
#include <vector>
int main(){
std::cout << "\n";
// iterating over a C-Array
int myArray[5] = {1, 2, 3, 4, 5};
for (int &x : myArray) x *= 2;
for (int x: myArray) std::cout << x << " ";
std::cout << '\n';
// iterating over a std::vector
std::vector<int> vecInt({1, 2, 3, 4, 5});
for (int &x: vecInt) x *= 2;
for (int x: vecInt) std::cout << x << " ";
std::cout << '\n';
std::cout << '\n';
std::string str = {"Only for Testing Purpose."};
// iterating over a std::string
for (char& c: str) c = std::toupper(c);
for (char c: str) std::cout << c;
std::cout << '\n';
// switch each character from upper to lower case and vice versa
str = {"Only for Testing Purpose."};
for (char& c: str) c=std::isupper(c)? std::tolower(c): std::toupper(c);
for (char c: str) std::cout << c;
std::cout << '\n';
std::cout << '\n';
//iterating over a std::map
std::map<std::string, std::string> phonebook{{"Bjarne Stroustrup", "+1 (212) 555-1212"}, {"Gabriel Dos Reis", "+1 (858) 555-9734"}, {"Daveed Vandevoorde", "+44 99 74855424"}};
// the old way
std::map<std::string, std::string>::iterator mapIt;
for (mapIt = phonebook.begin(); mapIt != phonebook.end(); ++mapIt){
std::cout << mapIt->first << ": " << mapIt->second << '\n';
}
// the new way
for (auto mapIt: phonebook) std::cout << mapIt.first << ": " << mapIt.second << '\n';
std::cout << "\n";
}