用std::vector的const_iterator对元素赋值会怎样?

用std::vector的const_iterator对元素赋值会怎样?
c++ builder 6中就是改变不了元素的值,不会编译不过,执行也不报错。这玩意儿把我害惨了,害我找了好长时间。
有空测试下vc7.1,vc8和c++ builder 2007,gcc

写了个测试程序vc7.1下居然能改变值:
#include  < vector >

struct  stUpdateItem
{
    
bool  _downloadSucceeded;

    stUpdateItem() : _downloadSucceeded(
false )
    {}
};

struct  stDownItem
{
    stUpdateItem
*  _pItem;
    
bool           _bPack;

    stDownItem(stUpdateItem
*  item,  bool  bPack) : _pItem(item),_bPack(bPack)
    {}
};

typedef std::vector
< stDownItem >  tDownItems;

int  _tmain( int  argc, _TCHAR *  argv[])
{
    tDownItems downList;

    stUpdateItem item1;
    stUpdateItem item2;

    stDownItem downItem1(
& item1, true );
    stDownItem downItem2(
& item2, false );

    downList.push_back(downItem1);
    downList.push_back(downItem2);

    
for  (tDownItems::const_iterator it  =  downList.begin(); it  !=  downList.end();  ++ it)
    {
        
if ( true   ==  it -> _pItem -> _downloadSucceeded)
        {
            std::cout 
<<   " before change, found! "   <<  std::endl;
        }
    }

    
for  (tDownItems::const_iterator it  =  downList.begin(); it  !=  downList.end();  ++ it)
    {
        it
-> _pItem -> _downloadSucceeded  =   true ;
    }

    
for  (tDownItems::const_iterator it  =  downList.begin(); it  !=  downList.end();  ++ it)
    {
        
if ( true   ==  it -> _pItem -> _downloadSucceeded)
        {
            std::cout 
<<   " after change, found! "   <<  std::endl;
        }
    }
    
return   0 ;
}

执行结果:
after change, found !
after change, found
!

你可能感兴趣的:(用std::vector的const_iterator对元素赋值会怎样?)