How To Append an Integer (with an Integer) in C++

让人想犯罪 __ 提交于 2019-12-22 10:57:30

问题


I was wondering if anyone could tell me how to append an integer (with another integer) in C++. Basically, if I have an int with this the value 67, how would I append it with the number 4 so the integer is now 674? Thanks in advance!


回答1:


Multiply first by ten to the power of digit number of second and add the other .

Example: 63 and 5

63*10=630
630+5 =635

Example: 75 and 34

75*100=7500
7500+34=7534
int i1=75;
int i2=34;
int dn=ceil(log10(i2+0.001));     //0.001 is for exact 10, exact 100, ...
int i3=i1*ceil(pow(10,dn)); <---- because pow would give 99.999999(for some optimization modes)
i3+=i2;

Edit: String version needs 2 int to str conversion (which is slow) and 1 string concatenation (which is not fast) and 1 str to int conversion (which is slow). Upper conversion needs 2 additions, 1 logarithm, 2 ceilings, 1 power, 1 multiplication all of which could be done in cpu without touching main memory to get/set data for sub steps that is surely less latency then string versions. If 3-4 character strings are stored in sse registers by compiler design, then both would compete for performance. Because while one would be busy computing "power" function, other would be busy extracting string from sse and putting it necessary registers one by one and constructing on another register by starting additions and multiplications. Power(10,x) function can be traded for 10*10*10.... x times so pure math version becomes faster again.

If it is readability you need, eq- 's answer is best imo.




回答2:


int appended = std::stoi(std::to_string(i1) + std::to_string(i2));
// error checking left as an exercise



回答3:


#include <iostream>
#include <string>

int appendDigit(int base, int append) {
   std::string sBase = std::to_string(base);
   std::string sAppend = std::to_string(append);
   std::string result = sBase + sAppend;
   return std::stoi(result);

}

int main() {
   int a = 67;
   int b = 4;
   int c = appendDigit(a,b);
   std::cout << c;
}



回答4:


Count the digits of the existing numbers, multiply by its tenth power and add to the second number.




回答5:


int append_digits(int i1, int i2) {
    int result = 0;
    while (i1) {
        result *= 10;
        result += i1 % 10;
        i1 /= 10;
    }
    while (i2) {
        result *= 10;
        result += i2 % 10;
        i2 /= 10;
    }
    int final_result = 0;
    while (result) {
        final_result *= 10;
        final_result += result % 10;
        result /= 10;
    }
    return final_result;
}

Refactoring to reduce code duplication is left as an exercise for the reader.




回答6:


Here's a more serious one:

int append_digits(int i1, int i2) {
    int i2_copy = i2;
    while (i2_copy) {
        i1 *= 10;
        i2_copy /= 10;
    }
    return i1 + i2;
}

This avoids floating-point math and string conversions.



来源:https://stackoverflow.com/questions/12239015/how-to-append-an-integer-with-an-integer-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!