for loops vs for each loop vectors C++

Personal contribution.

Using for each loop is a common task when you do not want to create an index i for example inside a for loop. For each loops work as long as there are elements inside an array or vectors.

Here are the following codes with the same output:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <vector>
using namespace std;

// Program to show the difference between for loop and for each loop using vectors.

void functionOne (){
	vector <string> v = {"1", "2", "3", "4", "5"};
	for (int i = 0; i < v.size(); i++){
	       cout << "The values of the vector are: " << v[i] << endl;
	}
    cout << endl;
}

void functionTwo (){
	vector <string> v = {"1", "2", "3", "4", "5"};
	for (int i = 0; i < v.size(); i++){
     	for (string& s: v){
    	    if (v[i] == s){
                  cout << "The strings are: " << s << endl;
            }
        }
    }
    cout << endl;
}

void functionThree (){
     vector <string> v = {"1", "2", "3", "4", "5"};
     for (string& s: v){
   	   cout << "The strings are: " << s << endl;
     }
}

int main(){
    functionOne();
    functionTwo();
    functionThree();

	return 0;
}



The values of the vector are: 1
The values of the vector are: 2
The values of the vector are: 3
The values of the vector are: 4
The values of the vector are: 5

The strings are: 1
The strings are: 2
The strings are: 3
The strings are: 4
The strings are: 5

The strings are: 1
The strings are: 2
The strings are: 3
The strings are: 4
The strings are: 5

Last edited on
Hi,

Line 10 and others similar to it should be:

for (std::size_t i = 0; i < v.size(); i++){

Because that is the type the size functions return.

Line 20 doesn't make sense - why compare each element to itself?

A best practise version of the function:

1
2
3
4
5
6
void functionThree (const std::vector<std::string>& v) {
     
     for (auto&& s :  v){ // forward reference - works with const, non const, lvalue and rvalues
   	   std::cout << "The strings are: " << s << std::endl;
     }
}


Obviously you need to send a vector to the function as an argument.

In your version of functionThree, you should have had const std::string& - use const wherever you can
@TheIdeasMan,

yes since I am using, using namespace std; I didn't use this format:
for (std::size_t i = 0; i < v.size(); i++){


I wonder why at university they do not teach this syntax ?

Line 20 doesn't make sense - why compare each element to itself?


Yes, it is a bit extra work, but I was just trying to prove that the vector elements(strings) are now saved inside string s, so they are equal together. However, example 3 is a simplified version of it.


Also, your code seems interesting. Could you please explain what the purpose of auto&& is ?

1
2
3
4
5
6
void functionThree (const std::vector<std::string>& v) {
     
     for (auto&& s :  v){ // forward reference - works with const, non const, lvalue and rvalues
   	   std::cout << "The strings are: " << s << std::endl;
     }
}



Last, but not least, const is useful. Your are right, we can use it as const vector argument like the example you have provided. This will prevent any change to he vector by giving compile errors.
yes since I am using, using namespace std; I didn't use this format:


It wasn't the std:: part I wanted to emphasise, it was the type size_t as opposed to int

I wonder why at university they do not teach this syntax ?


Beats me :+( They teach using namespace std; then one day it will bite you on the ass, and you won't know why. Google it.

Also, your code seems interesting. Could you please explain what the purpose of auto&& is ?


Well that is pretty advanced, if you put that in an assignment, it probably won't go down well - your tutor might not even know what it means. Look at the links provided by JLBorges here:

http://www.cplusplus.com/forum/beginner/207040/#msg978227

Also look up what auto means.
It wasn't the std:: part I wanted to emphasise, it was the type size_t as opposed to int


Oh I see, yeah I added it to the code. The code actually looks really nice now.


Beats me :+( They teach using namespace std; then one day it will bite you on the ass, and you won't know why. Google it.


Haha, its a hassle man. I don't know why programmers need to suffer so much.

Also look up what auto means.

People had different explanations and examples all over the place, but the main idea I got out of is that it acts as a key word that loops and increments until it prints out the necessary elements.


The following code is modified based on the above info. Man, it feels really good when you learn something new :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <vector>
using namespace std;

// Program to show the difference between for loop and for each loop using vectors.

void functionOne (const vector <string>& v){
	for (std::size_t i = 0; i < v.size(); i++){
	       cout << "The values of the vector are: " << v[i] << endl;
	}
    cout << endl;
}

void functionThree (const vector <string>& v){
     for (auto&& s: v){
   	   cout << "The strings are: " << s << endl;
     }
}

int main(){
    
    vector <string> v = {"1", "2", "3", "4", "5"};
    functionOne(v);
    functionThree(v);

	return 0;
}
http://en.cppreference.com/w/cpp/language/range-for

http://en.cppreference.com/w/cpp/language/auto

but the main idea I got out of is that it acts as a key word that loops and increments until it prints out the necessary elements.


That's a ranged based for loop. Both of these are:

1
2
3
4
5
void functionThree (const vector <std::string>& v){
     for (auto&& s: v){ // forwarding reference, auto type deduction
   	   std::cout << "The strings are: " << s << std::endl;
     }
}


1
2
3
4
5
void functionThree ( vector <std::string>& v){
     for (const std::string& s : v) { // ordinary const lvalue reference, explicit  type specified
   	   std::cout << "The strings are: " << s << std::endl;
     }
}


Last edited on
Topic archived. No new replies allowed.