使用foreach迭代向量

时间:2013-09-09 23:19:27

标签: c++ vector

以下代码:

std::vector< int > keysDown;
    for each ( int currentKey in keysDown )
    {

    }

引发了一个智能感知错误:

1   IntelliSense: a 'for each' statement cannot operate on an expression of type "std::vector<int, std::allocator<int>>"

但似乎编译和运行,任何想法?


似乎没有遍历向量:

void Keyboard::AddKeyToVector( int key )
{
    keysDown.push_back( key );
}

void Keyboard::RemoveKeyFromVector( int key )
{
    std::vector< int > keysDown; // Placing this here for brevity, it's declared in the class's .h and is instanciated globally
    int i;
    for( int currentKey : keysDown ) 
    {
        if( currentKey == key )
        {
            keysDown.erase(keysDown.begin()+i);
        }
        i++;
    }
}

0 个答案:

没有答案