ENV: windows 7 x64, Visual Studio Community 2015
I am a newbie of cplusplus and stl. I am trying to find what a list iterator exactly is. I write two piece of codes, and iterator in both of them seems have a different value with the address of the list element. It looks wired that it still can get the value of element using operator *. Any references, ideas about this? Thanks for your time.
Codes with struct list:
#include <iostream>
#include <list>
#include <cstdio>
typedef struct st
{
int m;
int n;
}st_t;
int main (int argc, char *argv[])
{
int *_i1, *_i2;
st_t *pST;
std::list<st_t> stList;
std::list<st_t>::iterator iterSTList;
st_t varST;
varST.m = 1;
varST.n = 10;
stList.push_back(varST);
varST.m = 2;
varST.n = 20;
stList.push_back(varST);
for (iterSTList=stList.begin(); iterSTList!=stList.end(); iterSTList++)
{
pST = &(*iterSTList);
std::cout << " pST->m " << pST->m << std::endl
<< " pST->n " << pST->n << std::endl
<< " (*pST).m " << (*pST).m << std::endl
<< " (*pST).n " << (*pST).n << std::endl
<< " pST " << pST << std::endl
<< " &(*iterSTList) " << &(*iterSTList) << std::endl;
std::printf("iterSTList %X \n", iterSTList);
_i1 = &(iterSTList->m);
_i2 = &(iterSTList->n);
std::cout << " _i1 " << _i1 << " iterSTList->m " << iterSTList->m << std::endl
<< " *_i1 " << *_i1 << " (*iterSTList).m " << (*iterSTList).m << std::endl
<< " _i2 " << _i2 << " iterSTList->n " << iterSTList->n << std::endl
<< " *_i2 " << *_i2 << " (*iterSTList).n " << (*iterSTList).n << std::endl;
}
return 0;
}
result
pST->m 1
pST->n 10
(*pST).m 1
(*pST).n 10
pST 00323F90
&(*iterSTList) 00323F90
iterSTList 323960
_i1 00323F90 iterSTList->m 1
*_i1 1 (*iterSTList).m 1
_i2 00323F94 iterSTList->n 10
*_i2 10 (*iterSTList).n 10
pST->m 2
pST->n 20
(*pST).m 2
(*pST).n 20
pST 00323FD0
&(*iterSTList) 00323FD0
iterSTList 323960
_i1 00323FD0 iterSTList->m 2
*_i1 2 (*iterSTList).m 2
_i2 00323FD4 iterSTList->n 20
*_i2 20 (*iterSTList).n 20
See. the value of iterSTList is different with &(*iterSTList), but iterSTList can get the element value like a pointer
Codes without struct:
#include <iostream>
#include <list>
#include <cstdio>
int main (int argc, char *argv[])
{
int *p;
std::list<int> iList;
std::list<int>::iterator iter;
int m;
m = 1;
iList.push_back(m);
m = 2;
iList.push_back(m);
for (iter=iList.begin(); iter!=iList.end(); iter++)
{
p = &(*iter);
std::cout << " p " << p << " *p " << *p << std::endl
<< " &(*iter) " << &(*iter) << " *iter " << *iter << std::endl;
std::printf("iter %X \n", iter);
}
return 0;
}
result
p 007039D8 *p 1
&(*iter) 007039D8 *iter 1
iter 703998
p 00703A10 *p 2
&(*iter) 00703A10 *iter 2
iter 703998
same thing. the value of iter is different with &(*iter) and p. But *iter and *p get the same value.
Any thing wrong with my code? iter is an iterator, of course. Does it can be treated as a pointer sometimes? when? Thanks.
After compiling on VS 2015:
Severity Code Description Project File Line Suppression State
Warning C4477 'printf' : format string '%X' requires an argument of type 'unsigned int', but variadic argument 1 has type 'std::_List_iterator<std::_List_val<std::_List_simple_types<st>>>' tlist2 c:\users\cnpc\documents\visual studio 2015\projects\tlist2\tlist2\source.cpp 35