<commit_before>---
layout: til
title: "C++ range-for-statement"
date: 2017-09-12
author: OctoMiao
comments: true
categories: programming
filter: programming
summary: In C++ we can use range-for-statement
---


{% highlight cpp %}

#include <iostream>

using namespace std;

main(){
  int v[] = {1,3,5,7};

  for( auto x:v) {
    cout << x << endl;
  }

}

{% endhighlight %}<commit_msg>update TIL about range for statement<commit_after>---
layout: til
title: "C++ range-for-statement"
date: 2017-09-12
author: OctoMiao
comments: true
categories: programming
filter: programming
summary: In C++ we can use range-for-statement
---


{% highlight cpp %}

#include <iostream>

using namespace std;

main(){
  int v[] = {1,3,5,7};

  for( auto x:v) {
    cout << x << endl;
  }

}

{% endhighlight %}

What this does is to copy each element of array v into x and print it. For efficiency, we could use instead pointers.

{% highlight cpp %}
for(auto& x : v) {
   cout << &x << endl;
}
{% endhighlight %}


<script src="//repl.it/embed/LAIt/5.js"></script>
{'fraction_non_alphanumeric': 0.1580952380952381, 'fraction_numerical': 0.023809523809523808, 'mean_word_length': 3.6096491228070176, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2390655', 'n_tokens_mistral': 400, 'n_tokens_neox': 366, 'n_words': 121}