const char * konkatenacja

Muszę połączyć dwa znaki const takie jak te:

const char *one = "Hello ";
const char *two = "World";
Jak mam to zrobić?

Przekazałem te char*z biblioteki innej firmy z interfejsem C, więc nie mogę po prostu użyć std::string zamiast tego.

Author: jalf, 2010-01-03

12 answers

W twoim przykładzie jeden i dwa są wskaźnikami znakowymi, wskazującymi na stałe znakowe. Nie można zmienić stałych znaków wskazywanych przez te wskaźniki. Czyli wszystko jak:

strcat(one,two); // append string two to string one.
Nie zadziała. Zamiast tego powinieneś mieć oddzielną zmienną (tablicę znaków) do przechowywania wyniku. Coś takiego:
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.
 85
Author: codaddict,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-04 05:18:06

Droga C:

char buf[100];
strcpy(buf, one);
strcat(buf, two);

Sposób C++:

std::string buf(one);
buf.append(two);

Sposób kompilacji:

#define one "hello "
#define two "world"
#define concat(first, second) first second

const char* buf = concat(one, two);
 61
Author: Idan K,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-03 14:09:04

Jeśli używasz C++, dlaczego nie używasz std::string zamiast ciągów w stylu C?

std::string one="Hello";
std::string two="World";

std::string three= one+two;

Jeśli chcesz przekazać ten łańcuch do funkcji C, po prostu przekaż three.c_str()

 28
Author: Prasoon Saurav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-14 23:24:46

Używając std::string:

#include <string>

std::string result = std::string(one) + std::string(two);
 17
Author: Gregory Pakosz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-03 17:55:57

Update: changed string total = string(one) + string(two); do string total( string(one) + two ); ze względów wydajnościowych (unika budowy ciągu drugiego i tymczasowego ciągu całkowitego)

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()
 16
Author: Pedro Reis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-07 17:32:31

Jeszcze jeden przykład:

// 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;

Ale jeśli nie chcesz lub nie możesz używać standardowej biblioteki C++, używanie {[1] } jest prawdopodobnie bezpieczniejsze.

 7
Author: stakx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-03 14:18:23

Wygląda na to, że używasz C++ z biblioteką C i dlatego musisz pracować z const char *.

Proponuję zawinąć te const char * W std::string:

const char *a = "hello "; 
const char *b = "world"; 
std::string c = a; 
std::string d = b; 
cout << c + d;
 5
Author: Luca Matteis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-03 14:28:06

Po pierwsze, musisz stworzyć dynamiczną przestrzeń pamięci. Następnie można po prostu strcat dwa struny do niego. Możesz też użyć klasy "string" c++. Old-school C way:

  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);

Nowy sposób C++

  std::string three(one);
  three += two;
 5
Author: Paul Tomblin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-03 15:56:47

Możesz użyć strstream. Jest formalnie przestarzały, ale nadal jest świetnym narzędziem, jeśli chcesz pracować z ciągami C, myślę.

char result[100]; // max size 100
std::ostrstream s(result, sizeof result - 1);

s << one << two << std::ends;
result[99] = '\0';

To zapisze one, a następnie two do strumienia i dopisze zakończenie \0 za pomocą std::ends. W przypadku, gdy oba łańcuchy mogą skończyć się zapisem dokładnie 99 znaków-więc nie będzie spacji w zapisie \0 - piszemy jeden ręcznie na ostatniej pozycji.

 3
Author: Johannes Schaub - litb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-03 14:23:52
const char* one = "one";
const char* two = "two";
char result[40];
sprintf(result, "%s%s", one, two);
 3
Author: Jagannath,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-03 14:56:48

Jeśli nie znasz rozmiaru strun, możesz zrobić coś takiego:

#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;
}
 1
Author: imoutidi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-26 14:25:30

Podłączenie dwóch stałych wskaźników znakowych bez użycia polecenia strcpy w dynamicznej alokacji pamięci:

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;
 0
Author: Edin Jašarević,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 18:44:36