vector iteration loop throwing an error?

前端 未结 1 537
不知归路
不知归路 2021-01-19 07:31
for (std::vector::iterator it = v.begin(); it != v.end(); ++it)

error: conversion from \'std::vector::const_iterator {aka

相关标签:
1条回答
  • 2021-01-19 08:21

    You are in a context where v is const. Use const_iterator instead.

    for (std::vector<int>::const_iterator it = v.begin(); it != v.end(); ++it)
    

    Note 1. auto would do this automatically for you:

    for (auto it = v.begin(); it != v.end(); ++it)
    

    Note 2. You could use a range based loop if you don't need access to the iterators themselves, but to the elements of the container:

    for (auto elem : v) // elem is a copy. For reference, use auto& elem
    
    0 讨论(0)
提交回复
热议问题