2013-07-28 3 views
0

C++에는 이렇게 작성할 수있는 int 또는 짧은 코드 조각을 연결하는 함수가 있습니다. 지금까지 내가 발견 한 모든 것은 불필요하게 복잡해 보입니다. 문자열로 인해 두 개의 문자열을 함께 추가하기 때문에 모든 정수 equivelant가 궁금합니다.문자열을 연결하는 데 simmilar 정수를 빠르게 연결하는 방법

+0

어떤 범위를? – sje397

+0

0-9. 더 큰 정수는 다른가? – user2420395

+0

아니요, 사람들이 일반적으로해야하는 것이 아니기 때문에 기존의 표준 기능이 없습니다. 개별 자릿수를 연결하려면 10을 곱하고 숫자를 더한 다음 (10을 기준으로 연결하려는 경우) 반복하십시오. – jamesdlin

답변

1

무엇을하려고하는지 잘 모르겠습니까?

그런 건가요?

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

또는이 될 수있다 :

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 
0

내가 생각할 수있는 그 일의 가장 빠른 방법 :

#include <string> 

string constr = to_string(integer1) + to_string(integer2); 
int concatenated = stoi(constr); 
2

사용 std::to_string

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

출력 : helloworld를 : 3

또는 당신은 당신이

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

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

출력 원하는 arhive하기 이제 stringstream을 사용할 수 helloworld를 : 정수의 3

관련 문제