How to return from some time

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/*This is last 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/*his is outer 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/*This is last 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/*his is outer 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

- ?

+4
2

checkQ, , .

#include <bits/stdc++.h>
using namespace std;

enum CHECKQ_RC {
    GOOD,
    OK,
    NEED_TO_QUIT
};

CHECKQ_RC checkQ(int a, int b) {
    if (a < b)
        return GOOD;
    else if (a == b)
        return OK;
    else/*This is last else.*/
    {
        cout << "I cannot process a even number." << endl;
        return NEED_TO_QUIT;
    }
}

vector<int> fun(vector<int> vec) {
    vector<int> result;
    int die = 29;
    for (int i : vec) {
        int j;
        do {
            i+=2;
            result.push_back(i);
            j = checkQ(i, die);
            if (GOOD != j) break;
        } while (true);
        if (j == NEED_TO_QUIT) break;
    }

    return result;
}


int main()
{
    vector<int> loop_times{1,2,3};
    vector<vector<int>> vec_result;
    for/*his is outer 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;
}

// without checkQ
vector<int> fun(vector<int> vec) {
    vector<int> result;
    int die = 29;
    for (int i : vec) {
        do {
            i+=2;
            result.push_back(i);
        } while (i < die);
        if (i > die) break;
    }

    return result;
}
+1

checkQ , . , , , , .

#include <iostream>
#include <vector>

using namespace std;

struct EvenNumberException
{
};

bool checkQ(int a, int b)
{
  if (a < b)
    return true;
  else if (a == b)
    return false;
  else /*This is last else.*/
  {
    throw EvenNumberException{};
  }
}

vector<int> fun(vector<int> vec)
{
  vector<int> result;
  int die = 29;
  try
  {
    for (int i : vec)
    {
      do
      {
        i += 2;
        result.push_back(i);
      } while (checkQ(i, die));
    }
  }
  catch (EvenNumberException)
  {
    /* no cleanup necessary */
  }
  return result;
}

int main()
{
  vector<int> loop_times{ 1, 2, 3 };
  vector<vector<int> > vec_result;
    for/*his is outer 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;
}
+1

Source: https://habr.com/ru/post/1692746/


All Articles