const char* concatenation
-
22-09-2019 - |
سؤال
أحتاج إلى تسلسل اثنين من chars const مثل هذه:
const char *one = "Hello ";
const char *two = "World";
كيف يمكنني القيام بذلك؟
لقد مرت هذه char*
S من مكتبة طرف ثالث مع واجهة C لذلك لا يمكنني الاستخدام ببساطة std::string
في حين أن.
المحلول
في مثالك واحد و اثنين هي مؤشرات شار ، مشيرا إلى ثوابت شار. لا يمكنك تغيير ثوابت شار التي أشار إليها هذه المؤشرات. لذلك أي شيء مثل:
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.
نصائح أخرى
الطريق ج:
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 ++ ، فلماذا لا تستخدم std::string
بدلا من السلاسل على غرار C؟
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 );
لأسباب الأداء (يتجنب بناء السلسلة الثانية والإجمالي سلسلة مؤقتة)
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;
ولكن ما لم تكن على وجه التحديد لا تريد أو لا يمكنك استخدام المكتبة القياسية C ++ ، باستخدام std::string
ربما أكثر أمانًا.
يبدو أنك تستخدم 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;
بادئ ذي بدء ، عليك إنشاء مساحة للذاكرة الديناميكية. ثم يمكنك فقط أن تضغط على السلاسل فيه. أو يمكنك استخدام فئة C ++ "سلسلة". الطريق C-School 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
. لقد تم إهمالها رسميًا ، لكنها لا تزال أداة رائعة إذا كنت بحاجة إلى العمل مع Counts 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
- نكتب واحدة يدويًا في الموقف الأخير.
const char* one = "one";
const char* two = "two";
char result[40];
sprintf(result, "%s%s", one, two);
توصيل اثنين من مؤشر char ثابت دون استخدام أمر strcpy في التخصيص الديناميكي للذاكرة:
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;