看一下set的iterator.由于map和set的相似性,只要看set就可以了.
1 #include <set>
2
3 void init( std::set<int>& set )
4 {
5 for ( int i = 0; i < 0x10; i++ )
6 {
7 set.insert( i );
8 }
9 }
10
11 int getSum( std::set<int>& set )
12 {
13 std::set<int>::iterator iter;
14 int result = 0;
15
16 for ( iter = set.begin(); iter != set.end(); iter++ )
17 {
18 result += *iter;
19 }
20
21 return result;
22 }
23
24 int main()
25 {
26 std::set<int> set;
27 init( set );
28
29 return getSum( set );
30 }
同样是看一下getSum的汇编:
(gdb) disassemble getSum
Dump of assembler code for function _Z6getSumRSt3setIiSt4lessIiESaIiEE:
0x080487a7 <+0>: push %ebp
0x080487a8 <+1>: mov %esp,%ebp
0x080487aa <+3>: sub $0x38,%esp
0x080487ad <+6>: lea -0x18(%ebp),%eax
0x080487b0 <+9>: mov %eax,(%esp)
0x080487b3 <+12>: call 0x8048914 <_ZNSt23_Rb_tree_const_iteratorIiEC2Ev>
0x080487b8 <+17>: movl $0x0,-0xc(%ebp)
0x080487bf <+24>: lea -0x1c(%ebp),%eax
0x080487c2 <+27>: mov 0x8(%ebp),%edx
0x080487c5 <+30>: mov %edx,0x4(%esp)
0x080487c9 <+34>: mov %eax,(%esp)
0x080487cc <+37>: call 0x8048922 <_ZNKSt3setIiSt4lessIiESaIiEE5beginEv>
0x080487d1 <+42>: sub $0x4,%esp
0x080487d4 <+45>: mov -0x1c(%ebp),%eax
0x080487d7 <+48>: mov %eax,-0x18(%ebp)
0x080487da <+51>: jmp 0x8048809 <_Z6getSumRSt3setIiSt4lessIiESaIiEE+98>
0x080487dc <+53>: lea -0x18(%ebp),%eax
0x080487df <+56>: mov %eax,(%esp)
0x080487e2 <+59>: call 0x80489c0 <_ZNKSt23_Rb_tree_const_iteratorIiEdeEv>
0x080487e7 <+64>: mov (%eax),%eax
0x080487e9 <+66>: add %eax,-0xc(%ebp)
0x080487ec <+69>: lea -0x10(%ebp),%eax
0x080487ef <+72>: movl $0x0,0x8(%esp)
0x080487f7 <+80>: lea -0x18(%ebp),%edx
0x080487fa <+83>: mov %edx,0x4(%esp)
0x080487fe <+87>: mov %eax,(%esp)
0x08048801 <+90>: call 0x8048982 <_ZNSt23_Rb_tree_const_iteratorIiEppEi>
0x08048806 <+95>: sub $0x4,%esp
0x08048809 <+98>: lea -0x14(%ebp),%eax
0x0804880c <+101>: mov 0x8(%ebp),%edx
0x0804880f <+104>: mov %edx,0x4(%esp)
0x08048813 <+108>: mov %eax,(%esp)
0x08048816 <+111>: call 0x8048948 <_ZNKSt3setIiSt4lessIiESaIiEE3endEv>
0x0804881b <+116>: sub $0x4,%esp
0x0804881e <+119>: lea -0x14(%ebp),%eax
0x08048821 <+122>: mov %eax,0x4(%esp)
0x08048825 <+126>: lea -0x18(%ebp),%eax
0x08048828 <+129>: mov %eax,(%esp)
0x0804882b <+132>: call 0x804896e <_ZNKSt23_Rb_tree_const_iteratorIiEneERKS0_>
0x08048830 <+137>: test %al,%al
0x08048832 <+139>: jne 0x80487dc <_Z6getSumRSt3setIiSt4lessIiESaIiEE+53>
0x08048834 <+141>: mov -0xc(%ebp),%eax
0x08048837 <+144>: leave
0x08048838 <+145>: ret
End of assembler dump.
更多内容请关注微信公众号“debugeeker", 链接为https://mp.weixin.qq.com/s/-DztNs1rhiUGQiNHV88Ekg