comparing 2 int lengths C++
我正在尝试将整数长度与它们的数字长度进行比较,并用0填充最小的长度,以便它们都具有相同的大小,即:
1 | 6 and 1500 |
成为
1 | 0006 and 1500 |
我无法使std :: stoi正常工作,然后再检查每个数字的长度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #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 a€?int schoolMethod(int, int, int)a€?: karatsuba.cpp:50: error: a€?stoia€? is not a member of a€?stda€? |
如果要将int转换为字符串以比较长度,则以下是有关如何基本执行此操作的指南。
1 2 3 4 5 6 7 8 9 10 | #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; } } |
stoi将Const String作为参数,但是在这里您将
使用