Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f4e800b

Browse files
add method 2
1 parent 6b22d33 commit f4e800b

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

‎map_iteration.cpp‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
#include <iostream>
22
#include <map>
33

4-
using namespace std;
4+
int main() {
5+
std::map<char, int> m;
56

6-
int main()
7-
{
8-
map<char, int> m;
9-
107
m['a'] = 2;
118
m['b'] = 5;
129
m['c'] = 8;
1310

14-
//https://stackoverflow.com/questions/1063453/how-to-display-map-contents
15-
for (const auto &p : m) {
11+
// Method 1
12+
// https://stackoverflow.com/questions/1063453/how-to-display-map-contents
13+
for(const auto &p : m) {
1614
std::cout << "m[" << p.first << "] = " << p.second << '\n';
1715
}
1816

17+
// Method 2
18+
// https://stackoverflow.com/questions/1443793/iterate-keys-in-a-c-map
19+
for(const auto &[key, value] : m) {
20+
std::cout << "m[" << key << "] = " << value << '\n';
21+
}
22+
1923
return 0;
2024
}
21-

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /