mercredi 4 novembre 2020

What's wrong with this stoi function? [duplicate]

I am unable to understand what is the mistake in this function. I am getting error terminate called after throwing an instance of 'std::invalid_argument' what(): stoi. I want to ask is there something wrong with this stoi() which I must know. I tried to debug my code but I am unable to understand, where am I going wrong??

bool solve_three(string s) {
// s.length() will be 3 always.
    string temp="";
     temp+= s[0] ;
     temp+= s[1];
     temp+= s[2];
     int ss=stoi(temp);

    return false;

}

complete code given below.

#include<bits/stdc++.h>

using namespace std;

#pragma GCC push_options
#pragma GCC optimize ("unroll-loops")

#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define watch2(x, y) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<"\n"

#define pow2(x) ((x)*(x))
#define ll long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define pf push_front

#define mod 1000000007
#define clock (clock() * 1000.0 / CLOCKS_PER_SEC)

#define mp make_pair
#define ff first
#define ss second
#define null NULL

#define all(c) (c).begin(),(c).end()
#define nl "\n"

typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef map<ll, ll> mll;

bool solve_three(string s) {
    string temp="";
          temp+= s[0] ;
            temp+= s[1];
            temp+= s[2];
            watch(temp);
            int ss=stoi(temp);

    return false;

}

void solve(vector<string> v) {
    int n = v.size();
    vector<string> res;
    for (int i = 0; i < n; i++)
    {
        string temp = v[i];
        if (temp.length() > 2)
        {
            for (int j = 0; j < temp.length(); j++)
            {
                for (int k = j + 1; k < temp.length(); k++)
                {
                    for (int x = k + 1; x < temp.length(); x++)
                    {
                        string xx="";
                        xx+=temp[j];
                        xx+=temp[k];
                        xx+=temp[x];
//                        string xx = (char)(temp[j]-'0') + (char)temp[k] + (char)temp[x];
                        if(solve_three(xx)){cout<<"YES";}
                        watch(xx);
                    }
                }
            }
        }
    }

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;

        vector <string> s(n);

        for (int i = 0; i < n; i++)
        {
            cin >> s[i];
        }
        solve(s);

    }
    return 0;
}

Can someone tell me whats wrong with this code. I am able o compile it but getting error. Try input

1
1
fcjsfkkf

Aucun commentaire:

Enregistrer un commentaire