samedi 24 août 2019

Best way to loop in C++

Studying C++ I came across with at least three ways of iterating over a standard vector array:

#include <vector>

using namespace std;

int main(){
    vector<int> v;
    v.resize(10);
    for (int&i : v){
        i=0;
    }
    for (int i=0;i<v.size();++i){
       v[i]=0;
    }
    for (vector<int>::iterator i=v.begin();i!=v.end();++i){
       *i=0;
    }
    return 0;
}

  1. What are the differences ?
  2. What is the best way according to the task needed ?

Aucun commentaire:

Enregistrer un commentaire