1
std::vector< std::vector<coords> >::iterator iter;
for(iter = characters.begin(); iter != characters.end(); iter++) 
{
    std::vector<coords>* cha = iter; // doesn't work.
}

// does work.
std::vector<coords>* character = &characters.at(0);
coords* first = &character->at(0);

そして、私には理由がわかりません。iter は、コンテナーが「含む」と想定されている型の要素へのポインターであると想定されていませんか?

これに光を当てたい人はいますか?

動作しないということは、次のことを意味します。

error C2440: 'initializing' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'std::vector<_Ty> *'

これは私にはあまり意味がありません。

4

3 に答える 3

6

イテレータは、ポインタのように逆参照できる型です。つまり、明示的なoperator*()andがありoperator->()ます。ポインターである必要はありません。

したがって&*iter、ベクトルのアドレスを取得する場合に使用します。

于 2009-02-27T05:33:26.780 に答える
1

I know it's obvious now (in hindsight), but in your for loop you could also try:

std::vector<coords> & cha = * iter;

Also, not what you are asking for, and just FYI, but vectors support random access iterators. Meaning you could also write:

for( size_t i=0; i<characters.size();  i ++ )

And, if you needed to convert back to an iterator, you could use:

characters.begin() + i

It's not the C++ way of doing things, it breaks the generic iterator philosophy, but it has its uses.

于 2009-02-27T09:44:46.880 に答える