0

In c++ is there any function to concatenate an int or a short piece of code i could write that will do this. Everything that i have found so far seems unnecisarily complicated. I was just wondering, because with strings you just add the two strings together, so any integer equivelant.

user2420395
  • 65
  • 1
  • 1
  • 7
  • What range of integers? – sje397 Jul 28 '13 at 10:36
  • 0 to 9. Is it different for larger integers? – user2420395 Jul 28 '13 at 10:39
  • No, there are no existing standard functions to do it because it's not something that people normally need to do. If you want to concatenate individual digits, then you multiply by 10, add a digit, and repeat (assuming that you want to concatenate in base-10). – jamesdlin Jul 28 '13 at 10:40
  • What do you mean exactly? You have "40" and "50" and you want to get "4050"? – tomi.lee.jones Jul 28 '13 at 10:41
  • Oh ok. I just found it odd. And thanks i will try all the options given. to me at least it seems that all variable types should have a concatenate operator/function built, at least that is what i would have done. just my opinion. – user2420395 Jul 28 '13 at 10:57
  • You can also just add char '0' to each digit to get the appropriate character. – sje397 Jul 28 '13 at 11:50
  • @user2420395 But what does it *mean* to concatenate two integers? Do you want to convert them to string representations and concatenate those (e.g. `concat(10, 30)` produces a string `"1030"`), or do you want to return a number (an integer `1030`)? Should operations happen in base-10? Why not base-16? Or base-2? What would it mean to concatenate two floating point numbers? "Concatenation" is a meaningless operation on things that are not strings. If you want there to be some meaning, you need to specify *what you mean*. – jamesdlin Jul 28 '13 at 22:59
  • Having to put two values of the same type together as you do with strings and the result is the same variable type that you started with. and there are instances where you want to concatenate integers and have the result be an integer. In what base it should be, either of them, all though the base should probable stay the same between the two values. 10 and 1337 becoming 101337. Or something similar. But still just curious. – user2420395 Jul 29 '13 at 06:56
  • @user2420395 That still makes no sense. Integers are not stored in any particular base. An integer is an integer. The base comes into play only *when you convert them to strings*. If you want to concatenate the string representations, then convert to strings first and concatenate those (as described in some of the answers). But concatenating non-string types is nonsense. – jamesdlin Jul 29 '13 at 19:00

3 Answers3

2

Use std::to_string

#include <string>
std::string s("helloworld:") + std::to_string(3);

output: helloworld:3

Or you could use stringstream to arhive what you want

#include <sstream>
std::string s("helloworld:");

std::stringstream ss;
ss << 3;
s += ss.str();

output: helloworld:3

billz
  • 44,644
  • 9
  • 83
  • 100
1

I don't know what are you trying to achieve

Is something like this ?

#define WEIRDCONCAT(a,b) a##b
int main()
{
cout<<WEIRDCONCAT(1,6);
}

Or may be this:

int no_of_digits(int number){
    int digits = 0; 
    while (number != 0) { number /= 10; digits++; }
    return digits;
}
int concat_ints (int n, ...)
{
  int i;
  int val,result=0;
  va_list vl;
  va_start(vl,n);

  for (i=0;i<n;i++)
  {
    val=va_arg(vl,int);
    result=(result*pow(10,no_of_digits(val)))+val;
  }
  va_end(vl);
 return result;
}

int val=concat_ints (3,  //No of intergers
                     62,712,821); //Example Outputs: 62712821
P0W
  • 46,614
  • 9
  • 72
  • 119
0

The fastest way of doing it i can think of:

#include <string>

string constr = to_string(integer1) + to_string(integer2);
int concatenated = stoi(constr);
tomi.lee.jones
  • 1,563
  • 1
  • 15
  • 22