質問
次のように 2 つの const char を連結する必要があります。
const char *one = "Hello ";
const char *two = "World";
どうすればよいでしょうか?
私はこれらを渡されました char*
Cインターフェイスを備えたサードパーティライブラリからのものであるため、単純に使用することはできません std::string
その代わり。
解決
1 との2つのはchar型の定数を指す文字ポインター、です。あなたはこれらのポインタで指されるchar型の定数を変更することはできません。何のようなので:
strcat(one,two); // append string two to string one.
は動作しません。代わりに、結果を保持するために別の変数(char配列)を持つべきです。このような何かます:
char result[100]; // array to hold the result.
strcpy(result,one); // copy string one into the result.
strcat(result,two); // append string two to the result.
他のヒント
Cの方法:
char buf[100];
strcpy(buf, one);
strcat(buf, two);
C ++方法:
std::string buf(one);
buf.append(two);
コンパイル時の道ます:
#define one "hello "
#define two "world"
#define concat(first, second) first second
const char* buf = concat(one, two);
あなたがC ++を使用している場合は、なぜあなたは代わりにCスタイルの文字列のstd::string
を使用していない?
std::string one="Hello";
std::string two="World";
std::string three= one+two;
あなたは、単に渡し、C-関数にこの文字列を渡す必要がある場合はthree.c_str()
std::string
を使用します:
#include <string>
std::string result = std::string(one) + std::string(two);
アップデート: かわった string total = string(one) + string(two);
に string total( string(one) + two );
パフォーマンス上の理由から (文字列 2 と一時的な文字列合計の構築を回避します)
const char *one = "Hello ";
const char *two = "World";
string total( string(one) + two ); // OR: string total = string(one) + string(two);
// string total(move(move(string(one)) + two)); // even faster?
// to use the concatenation in const char* use
total.c_str()
もう一つの例:
// calculate the required buffer size (also accounting for the null terminator):
int bufferSize = strlen(one) + strlen(two) + 1;
// allocate enough memory for the concatenated string:
char* concatString = new char[ bufferSize ];
// copy strings one and two over to the new buffer:
strcpy( concatString, one );
strcat( concatString, two );
...
// delete buffer:
delete[] concatString;
しかし、あなたは特に必要はありませんかstd::string
を使用して、C ++標準ライブラリを使用することはできませんしない限り、おそらく安全です。
あなたがCライブラリとC ++を使用しているので、あなたがconst char *
との仕事に必要なように思えます。
私はconst char *
にそれらのstd::string
をラップすることをお勧めます:
const char *a = "hello ";
const char *b = "world";
std::string c = a;
std::string d = b;
cout << c + d;
まず第一に、あなたはいくつかの動的なメモリ空間を作成する必要があります。次に、あなたはそれに2つの文字列をSTRCATすることができます。それとも、C ++、「文字列」クラスを使用することができます。古い学校のCの方法:
char* catString = malloc(strlen(one)+strlen(two)+1);
strcpy(catString, one);
strcat(catString, two);
// use the string then delete it when you're done.
free(catString);
新しいC ++の方法
std::string three(one);
three += two;
あなたは、文字列のサイズがわからない場合、あなたはこのような何かを行うことができます:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
const char* q1 = "First String";
const char* q2 = " Second String";
char * qq = (char*) malloc((strlen(q1)+ strlen(q2))*sizeof(char));
strcpy(qq,q1);
strcat(qq,q2);
printf("%s\n",qq);
return 0;
}
あなたはstrstream
を使用することができます。これは正式に廃止予定していますが、Cの文字列を使用する必要がある場合、それはまだ素晴らしいツールだだと思います。
char result[100]; // max size 100
std::ostrstream s(result, sizeof result - 1);
s << one << two << std::ends;
result[99] = '\0';
これはone
を記述し、ストリームにtwo
、及び\0
を用いて終端std::ends
を追加します。場合、両方の文字列は正確に99
文字を書いて終わる可能性 - にスペースはないだろうので、左の書き込み\0
を - 私たちは最後の位置に手動で1を書きます。
const char* one = "one";
const char* two = "two";
char result[40];
sprintf(result, "%s%s", one, two);
メモリの動的割り当てにstrcpyのコマンドを使用せずに2つの定数チャーポインタを接続する
const char* one = "Hello ";
const char* two = "World!";
char* three = new char[strlen(one) + strlen(two) + 1] {'\0'};
strcat_s(three, strlen(one) + 1, one);
strcat_s(three, strlen(one) + strlen(two) + 1, two);
cout << three << endl;
delete[] three;
three = nullptr;