In my previous articles about C++11 standard I talked about automatic type detection , decltype keyword and uniform initializer list introduced in C++11. Lets see the Range-based for loop today:
Earlier, the for loop which we use to traverse a list has unnecessary counter increment and check for the last element in the list. For example:If I want to print all the elements in an array, then the code will be
// C++03 code
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
for(int cnt=0; cnt < 10; cnt++)
cout << arr[cnt] << endl;
Languages like java and C# already had for each loop which is easier to use in such cases. the java code will be
// Java Code
for(int x : arr)
System.out.println(x); // similar to cout in C++
C++11 introduces range-based for loop which is similar to for-each loop of other languages. The above code in C++11 will be
// C++11 Code
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
for (int &x : arr)
cout << x << endl;
Lets look at a more complex problem:
// Code before C++11
map another_class_map;
for (map<myclass, vector>::const_iterator it = another_class_map.begin();
it != another_class_map.end(); ++it)
{
....
}
// C++11 Code
map<myclass, vector> another_class_map;
for (auto x : another_class_map)
{
....
}
See how convenient 🙂
Previous: Uniform Initializer List in C++11
Next: Separate Null Pointer in C++11
FOUR CORNERS PHARMACY NEW ZEALAND canadian pharmacy viagra Cialis PHARMACY ONLINE https://www.acanadapharmacy.com Cialis generic 5mg Viagra Online Canada. Approved Canadian Pharmacy Store.
TOP RATED GENERIC VIAGRA PHARMACY tadalafil canadian pharmacy online Medical Specialists Pharmacy – Viagra UK – Buy Genuine Pfizer without prescription pharmacies india Canadian pharm support group Medical Specialists Pharmacy – Viagra UK – Buy Genuine Pfizer online pharmacy no prescription canada cialis no prescription CANADIAN DISCOUNT PHARMACIES Canadian Pharmacy Online BEST NATIONAL PHARMACY Viagra Online Australia
ONLINE PHARMACY INDIA Viagra-Online.be : Het verschil tussen no prescription foreign pharmacies
ontario no script pharmacy
sky pharmacy canada
cialis for sale canada
supremesuppliers
supreme suppliers
https://www.weightlossprescribedbydoctors.com
Comments – Guaranteed Cheapest Viagra
cheap cialis online
healthy male viagra
on line pharmacy
no prescription pharmacy
CANADA PHARMACY MEDS canadian pharmacy cialis 20mg Viagra Online Pharmacy – THE LOWEST VIAGRA PRICE GUARANTEED, fast canadian pharmacy Buy primatene mist canada *** Cheap Prednisone online *** Choose from the best pharmacies ***
CANADIAN PHARMACIES ONLINE amoxicillin order from canada Canadian Pharmacy https://www.drugs1mg.com Viagra very fast shipping Sildenafil Citrate – The Generic Meds Online Pharmacy You Can
TADALAFIL INDIA PHARMACY no prescription pharmacy online Flagyl (Metronidazole) Drug Information: Clinical Pharmacology glucophage no perscriptionpharmacy What are the highest mg of cialis Discount Online Pharmacy