我可以将两个For循环集成在一个For循环中吗? [英] Can I Integrates Two For Loop In One For Loop?

查看:119
本文介绍了我可以将两个For循环集成在一个For循环中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

首先我是初学者和



i有一个问题...

我可以将以下代码中的for循环集成为for(int i = 0,j = 0; i< a,j< b; ++ i,++ j)



而不是这个???



  #include   

使用 命名空间标准;

int main(){

int n,a,b;
cin>> n>> a;
int arr [a];
for int i = 0 ; i< a; ++ i){
cin>> arr [i];
}
cin> ;> b;
int arr2 [b];
for (< span class =code-keywo rd> int j = 0 ; j< b; ++ j){
cin>> arr2 [j];
}
}

解决方案

不是真的,没有 - 迭代次数(你去的次数)循环循环)由不同的用户输入控制 - 所以如果他输入两个不同的数字,你必须有相同的循环绕两个不同的次数 - 这是愚蠢的! :笑:

这是可能的,但它需要更多的代码,更难以阅读,并且用户更难以计算出他应该进入的内容。



将它保留为两个循环!


如果两个数组的大小相同,那么它应该有效,但是不管怎么说你不需要两个索引变量

否。但是如果你有这样的多个输入循环,你应该将它们提取到一个函数中。这样您就不必两次编写相同的代码。另请注意,原始代码中存在错误:您无法使用变量作为大小来定义数组 - 您需要动态分配数组。此外,你应该检查大小是否实际上是一个有意义的值:



  bool  read_array( int  *& arr,int& arr_size){
bool 结果= false ;
// 获得所需的数组大小
cin>> arr_size;
// 确保合理的尺寸,假设你没有
// 想手动输入100多个值
if (arr_size> 0 && arr_size< 100 ){
// 在堆上分配数组
arr = new int [arr_size];
// 读取数组元素
for int index = 0 ; index< arr_size; ++ index){
cin>> ARR [指数];
}
result = true ;
}
返回结果;
}
int main(){
int * arr = nullptr ; // 始终初始化
int a = 0 ; // 初始化在这里并不那么重要,但要养成习惯!
if (!read_array(arr,a){
cout<< 无法读取第一个数组<< endl;
a = 0 ; // 重置为0,只是为了确保
}
int * arr2 = nullptr ;
int b = 0 ;
if (!read_array(arr2,b){
cout<< 无法读取第二个数组<< endl;
b = 0 ; // reset t o 0,只是为了确保
}
返回 0 ;
}


hello guys ,
first i am beginner programmer and

i have one question...
can i Integrates the for loop in the following code to be """ for(int i=0,j=0;i<a,j<b;++i,++j)

instead of this ???

#include

using namespace std;

int main(){
	
	int n,a,b;
	cin>>n>>a;
	int arr[a];
	for(int i=0;i<a;++i){
		cin>>arr[i];
	}
	cin>>b;
	int arr2[b];
	for(int j=0;j<b;++j){
		cin>>arr2[j];
	}   
	}

解决方案

Not really, no - the number of iterations (the number of times your go round the loop) is controlled by a different user input - so if he enters two different numbers you would have to have the same loop go round two different numbers of times - which is just silly! :laugh:
It is possible, but it would take more code, be more awkward to read, and harder for the user to work out what he is supposed to be entering.

Leave it as two loops!


That should work if the two arrays are the same size, but then you wouldn't need two index variables anyway.


No. But if you have multiple input loops like that, you should extract them into a function. That way you don't have to write the same code twice. Also note that there is a mistake in your original code: you cannot define a array using a variable as size - you need to allocate the array dynamically. Also you should check whether the size is in fact a meaningful value:

bool read_array(int*& arr, int& arr_size) {
   bool result = false;
   // get desired array size
   cin >> arr_size;
   // ensure reasonable size, assuming you don't really
   // want to enter more than 100 values manually
   if (arr_size > 0 && arr_size < 100) {
      // allocate array on the heap
      arr = new int[arr_size];
      // read array elements
      for (int index = 0; index < arr_size; ++index) {
         cin >> arr[index];
      }
      result = true;
   }
   return result;
}
int main () {
   int* arr = nullptr; // always initialize
   int a = 0; // initialization not so important here, but make it a habit!
   if (!read_array(arr, a) {
      cout << "Failed to read first array" << endl;
      a = 0; // reset to 0, just to be sure
   }
   int* arr2 = nullptr;
   int b = 0;
   if (!read_array(arr2, b) {
      cout << "Failed to read second array" << endl;
      b = 0; // reset to 0, just to be sure
   }
   return 0;
}


这篇关于我可以将两个For循环集成在一个For循环中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆