- Transfer from http://blog.csdn.net/qq_21400315/article/details/50561030
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- int _tmain (int argc, _tchar* argv[])
- {
- int nums[3] = {0, 1, 2};
- std::vector<Int>vs = {0, 1, 2, 3, 4};
- For (int num:nums)
- {
- Std::cout << num << "\ t";
- }
- This should be the most common, for (int u:nums)
- Std::cout << Std::endl;
- For (int v:vs)
- {
- Std::cout << v<<"\ t";
- }
- Std::cout << Std::endl;
- You can use this one, too.
- For each (int num in nums)
- {
- Std::cout << num << "\ t";
- }
- Std::cout << Std::endl;
- For each (int v in VS)
- {
- Std::cout << v << "\ t";
- }
- System ("pause");
- return 0;
- }
If you have used C # or Java, you will certainly be familiar with the foreach usage, because it is so convenient in a particular loop operation. In fact, a similar usage is provided in C + +, where two uses are provided, and it is important to note that the following is a C + + foreach usage, remembering that for and each are separate.
Application of the new standard for loop in C + +