// arraytest.cpp : 定义控制台应用程序的入口点。
//
#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";
}
std::cout << std::endl;
for (int v : vs)
{
std::cout << v<<"\t";
}
std::cout << std::endl;
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;
}
如果你使用过c#或者java你肯定会对其中的foreach用法十分熟悉,因为在特定的循环操作中它实在是太方便了。其实在c++中也提供了类似的用法,在这里提供了两种用法,需要注意的是下面那个是c++ foreach用法,记住
for
和each是分开的。
c++你不知道的用法之foreach篇
最新推荐文章于 2025-03-31 16:34:31 发布