2. Applying Range Based For loop –
• Fixed Size Arrays
• Vector and List
• Array
2
C++11: Range Based For Loop
3. Executes a for loop over a range
Used as a more readable equivalent to the
traditional for loop operating over a range
of values, such as all elements in a
container.
C++11: Range Based For Loop
3
Syntax :
for ( for-range-declaration : expression )
{
statement
}
14. Range based for loop can be applied on
containers to simply the programs.
It can be applied on sequence and
associative containers.
• It can be applied for all containers for which we
can apply begin and end algorithm or contains
begin and end members functions
Cannot be used to traverse part of list.
Cannot be applied on pointers.
C++11: Range Based For Loop
14
Editor's Notes
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {5 , 10 , 12, 20 , 40};
for(int i : arr)
cout << i << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
double arr[5] = {5.2 , 10.5 , 1.2, 2.3 , 4.5};
for(double i : arr)
cout << i << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {5 , 10 , 12, 20 , 40};
for(int ele : arr)
ele = ele + 2;
for(int ele : arr)
cout << ele << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {5 , 10 , 12, 20 , 40};
for(int &ele : arr)
ele = ele + 2;
for(int ele : arr)
cout << ele << endl;
return 0;
}
int main()
{
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for(int &ele : arr ) {
ele = ele + x;
}
for( const int &ele : arr ) {
cout << ele << " ";
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int iarr[5] = { 1, 2, 3, 4, 5};
double darr[5] = {5.2 , 10.5 , 1.2, 2.3 , 4.5};
for(const auto &ele : iarr ) {
cout << ele << " ";
}
cout << endl;
for(const auto &ele : darr ) {
cout << ele << " ";
}
return 0;
}
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main()
{
std::vector<int> v = {0, 7, 2, 3, 4, 5};
for (const auto &ele : v)
std::cout << ele << ' ';
cout << endl;
std::list<int> lst = {8, 6, 4, 2, 1};
for (const auto &ele : lst)
std::cout << ele << ' ';
}
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main(){
set <int> s;
s.insert(10);
s.insert(1);
s.insert(34);
s.insert(23);
s.insert(87);
for(auto ele : s)
cout << ele <<" ";
cout << endl;
map <int,string> m;
m.insert(pair<int,string>(1,"sikander"));
m.insert(pair<int,string>(4,"bangalore"));
m.insert(pair<int,string>(3,"chennai"));
m.insert(pair<int,string>(2,"cranes"));
m.insert(pair<int,string>(6,"karnataka"));
m.insert(pair<int,string>(5,"tamilnadu"));
for(auto ele : m)
cout << ele.first <<" " << ele.second << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Base
{
public :
Base() { }
Base(const Base &b)
{
cout <<"Base Copy Constructor \n";
}
virtual void display()
{
cout <<"Base Display \n";
}
};
class Derived : public Base
{
public :
Derived(){}
Derived(const Derived &b) : Base(b)
{
cout <<"Derived Copy Constructor \n";
}
virtual void display()
{
cout <<"Derived Display \n";
}
};
int main()
{
vector < Base *> v;
v.push_back((new Base()));
v.push_back((new Base()));
v.push_back((new Derived()));
v.push_back((new Derived()));
for(Base *ele : v)
ele->display();
}