C++:如何构建字符串/字符*

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2288970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:54:37  来源:igfitidea点击:

C++: How to build Strings / char*

c++stringchar

提问by Martijn Courteaux

I'm new to C++. I want to make a char*, but I don't know how.
In Java is it just this:

我是 C++ 的新手。我想做一个char*,但我不知道怎么做。
在 Java 中是这样的:

int player = 0;
int cpu = 0;
String s = "You: " + player + " CPU: " + cpu;

How can I do this? I need a char*.

我怎样才能做到这一点?我需要一个char*.

I'm focusing on pasting the integer after the string.

我专注于在字符串后粘贴整数。

回答by

You almost certainly don't want to deal with char * if you can help it - you need the C++ std::string class:

如果可以的话,您几乎肯定不想处理 char * - 您需要 C++ std::string 类:

#include <string>
..
string name = "fred";

or the related stringstream class:

或相关的 stringstream 类:

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

int main() {

  int player = 0;
  int cpu = 0;

  ostringstream os;
  os << "You: " << player << " CPU: " << cpu;
  string s = os.str();
  cout << s << endl;
}

if you really need a character pointer (and you haven't said why you think you do), you can get one from a string by using its c_str() member function.

如果你真的需要一个字符指针(你还没有说明你为什么这么认为),你可以使用它的 c_str() 成员函数从字符串中获取一个。

All this should be covered by any introductory C++ text book. If you haven't already bought one, get Accelerated C++. You cannot learn C++ from internet resources alone.

所有这些都应该包含在任何介绍性的 C++ 教科书中。如果您还没有购买,请获取Accelerated C++。您不能仅从互联网资源中学习 C++。

回答by Rupert Jones

If you're working with C++, just use std::string. If you're working with char*, you probably want to work with C directly. In case of C, you can use the sprintffunction:

如果您使用 C++,只需使用std::string. 如果您正在使用char*,您可能想直接使用 C。在 C 的情况下,您可以使用该sprintf函数:

char* s = // initialized properly
sprintf( s, "You: %d CPU: %d", player, cpu );

回答by Artem Barger

Just call s.c_str( );.Hereyou you can see more.

就打电话s.c_str( );在这里你可以看到更多。

PS. You can use strcpyto copy the content to new variable and then you will be able to change it.

附注。您可以使用strcpy将内容复制到新变量,然后您就可以更改它。

回答by Seb Rose

char *means "pointer to a character".

char *表示“指向字符的指针”。

You can create a pointer to a 'string' like this:

您可以像这样创建一个指向“字符串”的指针:

char* myString = "My long string";

Alternatively you can use std::string:

或者,您可以使用 std::string:

std::string myStdString("Another long string");
const char* myStdString.c_str();

Notice the const at the beginning of the last example. This means you can't change the chars that are pointed to. You can do the same with the first example:

请注意上一个示例开头的 const。这意味着您不能更改指向的字符。您可以对第一个示例执行相同操作:

const char* = "My long string";

回答by IVlad

Consider using stringstreams:

考虑使用字符串流:

#include <iostream>
#include <sstream>

using namespace std;
int main ()
{
    int i = 10;

    stringstream t;

    t << "test " << i;

    cout << t.str();
}

回答by T.E.D.

It probably would have been for the best if C++ had overloaded the "+" operator like you show. Sadly, they didn't (you can though, if you want to).

如果 C++ 像您展示的那样重载了“+”运算符,那可能是最好的。可悲的是,他们没有(你可以,如果你想的话)。

There are basicly three methods for converting integer variables to strings in C++; two inherited from C and one new one for C++.

C++中整型变量转字符串的方法主要有以下三种:两个继承自 C,一个新的用于 C++。

  1. The itoa()routine. This is actually non-standard, but most compilers have it. The nice thing about it is that it returns a pointer to the string, so it can be used in functional-style programming.
  2. sprintf(). The second holdover from C, this routine takes a destination string, a format string, and a list of parameters. How many parameters there are, and how they are interpreted depend on how the "format" string parses. This makes sprintf both immensely powerful and immensely dangerous. If you use this approach, I can pretty much guarantee you will have crash bugs your first few tries.
  3. std::ostringstream. The C++ way. This has pretty much all the power of sprintf(), but is muchsafer. The drawback here is that you have to declare it, and it is nota string, so you still have to convert it to one when you are done. That means at least three lines of code are required to do anythingwith an ostringstream. It is also really ugly, particularly if you try any special formatting.
  1. itoa()例程。这实际上是非标准的,但大多数编译器都有。它的好处是它返回一个指向字符串的指针,因此它可以用于函数式编程。
  2. sprintf()。来自 C 的第二个保留,此例程采用目标字符串、格式字符串和参数列表。有多少参数以及如何解释它们取决于“格式”字符串的解析方式。这使得 sprintf 既强大又危险。如果您使用这种方法,我几乎可以保证您在前几次尝试时会遇到崩溃错误。
  3. std::ostringstream。C++方式。这几乎具有 sprintf() 的所有功能,但安全。这里的缺点是你必须声明它,它不是一个字符串,所以你完成后仍然需要将它转换为一个。这意味着至少需要三行代码才能对ostringstream执行任何操作。它也非常丑陋,特别是如果您尝试任何特殊格式。