将字符串指针连接到另一个字符串指针 C++

发布于 2024-10-28 08:33:23 字数 361 浏览 9 评论 0原文

大家好,我有以下行:

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

其中firstName,lastName,playernumber,pointTotal都是字符串指针。

如何将它们全部放在另一个字符串指针中?编译器抱怨我的代码行。

抱歉,我不太擅长 C++,因为我有 Java 背景。

Hi everyone I have the following line:

string* playerInfo = "Name: " + firstName + " " + lastName + "\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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

恬淡成诗 2024-11-04 08:33:23

少用指针。如果所有变量都只是字符串,那就行了。但既然你说你有指针:

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

从 Java 迁移到 C++ 时你应该打破的第一个习惯就是使用 new 创建所有对象。在C++中,new不是用于创建对象,而是用于内存分配。任何时候您都应该使用局部变量而不是动态分配。当你不能时,尝试让一些库提供的对象(如 std::vector )为你处理分配和释放。

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.

孤星 2024-11-04 08:33:23

为了像这样将一堆片段放在一起,我会使用字符串流,如下所示:

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

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

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();
当梦初醒 2024-11-04 08:33:23

Java背景就是问题。

在 C++ 中你不能这样做。指针指向内存中的位置。它们位于不同的位置,因此您不能将它们连接起来。

为什么要使用字符串指针?您可能会对 char * 指针和 string 感到困惑,其中 在STL中。

您可能只想使用字符串,而不使用指针。你可以这样做:

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

你也可以使用+=。

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

但这很令人困惑,因为你不能这样做:

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

但你可以这样做:

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

所以我只是避免使用运算符 + 并使用 .append。


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.


小兔几 2024-11-04 08:33:23

您可能只想:

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

"Name" 放入 std::string 然后创建一系列 operator+() 调用,进而产生级联。

您可能并不真正需要将playerInfo放在堆上,但如果您这样做,您可以拥有:

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

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);
浅忆 2024-11-04 08:33:23

就像科芬先生使用“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;

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;
狼亦尘 2024-11-04 08:33:23

正如您要求使用指针解决问题一样,这里是一个小学代码解决方案:

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文