0

I am trying to compare to integer lengths in terms of their digit lengths, and padding out the smallest one with 0's so they are both the same size, ie:

6 and 1500 

becomes

0006 and 1500

I cannot get std::stoi to work, to then check the length of each number.

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int schoolMethod(int a, int b, int base){

if(std::stoi(a)<std::stoi(b)){
  //do stuff  
  return 0;
  }
}


karatsuba.cpp: In function ‘int schoolMethod(int, int, int)’:
karatsuba.cpp:50: error: ‘stoi’ is not a member of ‘std’
Barney Chambers
  • 2,720
  • 6
  • 42
  • 78
  • 2
    You know [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) turns strings into `int`, not the other way around, right? – WhozCraig Aug 10 '15 at 05:49
  • possible duplicate of [std::stoi missing in g++ 4.7.2?](http://stackoverflow.com/questions/14743904/stdstoi-missing-in-g-4-7-2) – Ivan Aksamentov - Drop Aug 10 '15 at 06:07
  • Might help as well: [How can I count the digits in an integer without a string cast?](http://stackoverflow.com/questions/554521/how-can-i-count-the-digits-in-an-integer-without-a-string-cast) – Ivan Aksamentov - Drop Aug 10 '15 at 06:11

2 Answers2

1

stoi takes Const String as parameter, but here you are passing intto it.

Use std::to_string method to convert int into string and then compare their lengths.

Nishant
  • 1,635
  • 1
  • 11
  • 24
1

If you want to convert your ints to strings to compare length, the following is a guide on how to basically do this.

std::to_string() will convert your ints to strings so you can then compare them.

string.length() will then return the length of the string.

#include <string>

int schoolMethod(int a, int b, int base){
    std::string stringa = std::to_string(a); //convert to string
    std::string stringb = std::to_string(b); //convert to string
    if(stringa.length()<stringb.length()){ //compare string lengths
      //do stuff  
      return 0;
    }
}
FiringSquadWitness
  • 666
  • 1
  • 10
  • 27