How do I iterate over a Constant Vector?

后端 未结 6 922
终归单人心
终归单人心 2020-12-02 22:16

I have a vector of Student which has a field name.

I want to iterate over the vector.

void print(const vector& students)
    {
            


        
6条回答
  •  情话喂你
    2020-12-02 22:47

    Use const_iterator instead. An iterator allows modification of the vector, so you can't get one from a const container.

    Also, the idiomatic way to write this loop uses it != students.end() instead of < (though this should work on a vector).

提交回复
热议问题