开发者

Concatenate string points into another string pointer C++

开发者 https://www.devze.com 2023-02-20 12:09 出处:网络
Hi everyone I have the following line: string* playerInfo = \"Name: \" + firstName + \" \" + 开发者_StackOverflowlastName + \"\\n\" +

Hi everyone I have the following line:

string* playerInfo = "Name: " + firstName + " " + 开发者_StackOverflowlastName + "\n" +
                        "Number: " + playerNumber + "\n" +
                        "Points: " + pointTotal + "\n";

where firstName, lastName, playernumber, pointTotal are all string pointers.

How can I put them all together into another string pointer? The compiler complains about my line of code.

Sorry, I'm not good with C++ I come from a Java background.


Use less pointers. That would have worked if all your variables were just strings. But since you say you have pointers:

string playerInfo = "Name: " + *firstName + " " + *lastName + "\n" +
                    "Number: " + *playerNumber + "\n" +
                    "Points: " + *pointTotal + "\n";

One of the first habits you should break when moving from Java to C++ is creating all your objects with new. In C++, new is not for creating objects, it's for memory allocation. Any time you can use a local variable instead of dynamic allocation, you should. And when you can't, try to let some library-provided object like std::vector take care of the allocation and deallocation for you.


For putting a bunch of pieces together like this, I'd use a stringstream, something like this:

std::ostringstream buffer;
buffer << "Name: " << firstName << " " << lastName << "\n"
       << "Number: " << playerNumber << "\n"
       << "Points: " << pointTotal << "\n";

std::string PlayerInfo = buffer.str();


Java background is the problem.

You can't do this in C++. Pointers point to places in memory. They're in separate locations, so you can't just concatenate them.

Why are you using string pointers? You may be confused between char * pointers and string which is in the STL.

You probably just want to use strings, without pointers. You can do it like this:

string str;
str.append("Name: ");
str.append(firstname);
// ...

You can also use +=.

string str;
str += " ";
str += lastname;

But this is confusing, because you CANNOT do:

string str;
str += " " + " Name: "; // WRONG!

But you can do:

string str;
string str2;
str = "Name: " + str2; // OK

So I just avoid the operator + and use .append.



You probably want just:

std::string playerInfo = std::string("Name: ") + firstName + " " + lastName + "\n" +
                "Number: " + playerNumber + "\n" +
                "Points: " + pointTotal + "\n";

Putting "Name" into a std::string then creates a series of operator+() calls that in turn produce the concatenation.

You probably do not really need playerInfo to be on the heap, but if you do, you can have:

std::string* pOnHeap = new std::string(playerInfo);


like Mr. Coffin´s answer using "stringstream":

#include <iostream>
#include <sstream>
using namespace std;

stringstream buffer;     
buffer << "Name: " << firstName << " " << lastName << "\n"
       << "Number: " << playerNumber << "\n"
       << "Points: " << pointTotal << "\n";

cout << buffer.str() << endl;


As you asked to solve your problem by using pointers here is an elementar school code solution:

int StringLength(const char * s){
int l = 0;
while (*s++) l++;
return l;
}

char *StrCat(const char * str1, const char *str2){
  int len1 = StringLength(str1);
  int len2 = StringLength(str2);
  int totLen = len1 + len2 + 1;

  char * str12 = (char *)malloc((totLen)*sizeof(char));
  memset(str12, '\0', totLen);

  for (int i = 0; i < len1; i++)
    *(str12 + i) = *(str1 + i);
  for (int i = 0; i < len2; i++)
    *(str12 + i + len1) = *(str2 + i);

  return str12;
}

int main(int argc, char *argv[]){
  char * S1= "ABCDE";
  char * S2= "FGH";

  char *S12 = NULL;
  S12 = StrCat(S1, S2);
  cout << "S12= "<< S12 << endl; // ABCDEFGH
}
0

精彩评论

暂无评论...
验证码 换一张
取 消