2013-06-17 2 views
2
내가 다음 int char 또는 char*로 변환 내가 이런 식으로

변환 INT와

string s = "Scene"; 
string x = to_string(i); 
s = s + x; 
char* si = s.c_str(); 

을 시도한 char*

for (int i = 1; i < mymap.size(); i++) 
{ 
    char * s = "Scene"; 
    cout << mymap[s+i]; 
} 

과을 연결하려는

하지만 오류가 * 문자로를 연결하여 .

const char* si = s.c_str(); 

그러나 나는 mymap와 함께 사용하려고 :

는하지만이 작품

cout << mymap[si]; 

을 나는 오류가 난 char*하지 const char*

+0

문제를 계속 디버그 할 수 있도록 전체 오류를 보내 주시겠습니까? –

+1

'mymap'의 종류는 무엇입니까? 왜 열쇠를'char *'에 넣고'std :: string'으로 보관하지 않겠습니까? –

+4

은'mymap'''std :: map'입니까? 키를'char *'가 아닌'std :: string'으로 만들고 코드가 작동합니다. – David

답변

0

월을 사용해야한다고 얻을 조금 지나치지 만 문자열을 시도해 볼 수 있습니다.

#include <iostream> 
#include <sstream> 
#include <string> 

using namespace std; 

int main(int argc, char * argv[]) { 
char * a = "hello"; 
int b = 11; 

stringstream sstm; 

sstm << a << b; 

string ans = sstm.str(); 
char * ans2 = (char *) ans.c_str(); 

cout << ans2 << endl; //"hello11" 

return 0; 

}