It is difficult to describe what I want to express, but I confuse this problem for a very long time in my real project. My real project is too complicated to ask here, so I am doing a code example as follows to indicate a match.
bool checkQ(int a, int b) {
if (a < b)
return true;
else if (a == b)
return false;
else
{
cout << "I cannot process a even number." << endl;
return false;
}
}
vector<int> fun(vector<int> vec) {
vector<int> result;
int die = 29;
for (int i : vec) {
do {
i+=2;
result.push_back(i);
} while (checkQ(i,die));
}
return result;
}
int main()
{
vector<int> loop_times{1,2,3};
vector<vector<int>> vec_result;
for (int i : loop_times) {
vector<int> vec{ 23,25,26,25 };
vector<int> tem_vec = fun(vec);
vec_result.push_back(tem_vec);
}
for (vector<int> j : vec_result)
{
for (int k : j)
cout << k << " ";
cout << endl;
}
return 0;
}
This is sample code output
I cannot process a even number.
I cannot process a even number.
I cannot process a even number.
25 27 29 27 29 28 30 27 29
25 27 29 27 29 28 30 27 29
25 27 29 27 29 28 30 27 29
As you can see, my function funcannot handle an even element. I just can return falseto stop the process 30. But in fact, I hope to return continueto the outside to in fun. I just donβt know how to implement such a program. I think I can change return false;to a gotofunction to do this as checkQ
bool checkQ(int a, int b) {
if (a < b)
return true;
else if (a == b)
return false;
else
{
cout << "I cannot process a even number." << endl;
goto tag;
}
}
vector<int> fun(vector<int> vec) {
vector<int> result;
int die = 29;
for (int i : vec) {
do {
i+=2;
result.push_back(i);
} while (checkQ(i,die));
}
return result;
}
int main()
{
vector<int> loop_times{1,2,3};
vector<vector<int>> vec_result;
for (int i : loop_times) {
vector<int> vec{ 23,25,26,25 };
vector<int> tem_vec = fun(vec);
vec_result.push_back(tem_vec);
tag:continue;
}
for (vector<int> j : vec_result)
{
for (int k : j)
cout << k << " ";
cout << endl;
}
return 0;
}
, . , ,
I cannot process a even number.
I cannot process a even number.
I cannot process a even number.
25 27 29 27 29 28 30
25 27 29 27 29 28 30
25 27 29 27 29 28 30
- ?