C#与C++遍历一维数组二维数组的几种方法

C#中一维数组与二维数组遍历方式:

        string[] arr =new string[4] { "first", "second", "third", "fourth" };
        string[,] arr2 = { { "first", "second" }, { "third", "fourth" } };

        for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("一维数组第一种遍历方式" + arr[i]);
            }

        foreach (string index in arr)
            {
                Console.WriteLine("一维数组第二种遍历方式" + index);
            }

        for (int i = 0; i < 2; i++)
        {
            for(int j = 0; j < 2; j++)
            {
                Console.WriteLine("二维数组第一种遍历方式" + arr2[i,j]);
            }
        }

        foreach (string index in arr2)
        {
            Console.WriteLine("二维数组第二种遍历方式" + index);
        }

C++中一维数组与二维数组遍历方式:

	string arr[4] = { "first", "second", "third", "fourth" };
	string arr2[2][2] = { { "first", "second" },{ "third", "fourth" } };
	//下标
	for (int i = 0; i < 4; i++)
	{
		cout << "一维下标:" <<arr[i] << endl;
	}
	//foreach
	for each (string i in arr)
	{
		cout << "一维foreach:" << i << endl;
	}
	//auto
	for (auto p: arr)
	{
		cout << "一维auto:" << p << endl;
	}
	//auto指针
	for (auto p = arr; p != arr + 3; p++)
	{
		cout << "一维auto指针:" << *p << endl;
	}
	//指针
	for (string(*p) =arr; p != arr+3; p++)
	{
		cout << "一维指针:" << *p << endl;
	}
	//指针,迭代
	for (string(*p)= begin(arr); p != end(arr); p++)
	{
		cout << "一维指针,迭代:" << *p << endl;
	}
	//范围for
	for (string p : arr)
	{
		cout << "一维范围for:" << p << endl;
	}


	//下标
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j <2; j++)
		{
			cout << "二维下标:"<<arr2[i][j] << endl;
		}
	}
	//foreach
	for each (string i in arr2)
	{
		cout << "二维foreach:" << i << endl;
	}
	//auto
	for (auto p = arr2; p != arr2 + 2; p++)
	{
		for (auto q = *p; q != *p + 2; q++)
		{
			cout << "二维auto指针:" << *q << endl;
		}
	}
	//auto+迭代
	for (auto p = begin(arr2); p != end(arr2); p++)
	{
		for (auto q = begin(*p); q != end(*p); q++)
		{
			cout << "二维auto+迭代:" << *q << endl;
		}
	}
	//auto+范围for
	for (auto &p : arr2)
	{
		for (auto q : p)
		{
			cout << "二维auto+范围for:" << q << endl;
		}
	}
	//指针
	for (string(*p)[2] = arr2; p != arr2 + 2; p++)
	{
		for (string *q = *p; q != *p + 2; q++)
		{
			cout << "二维指针:" << *q << endl;
		}
	}
	//指针,迭代
	for (string(*p)[2] = begin(arr2); p != end(arr2); p++)
	{
		for (string* q = begin(*p); q != end(*p); q++)
		{
			cout << "二维指针,迭代:" << *q << endl;
		}
	}
	//范围for
	for (string(&p)[2] : arr2)
	{
		for (string q : p)
		{
			cout << "二维范围for:" << q << endl;
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值