C++ 从 char(不是 char*)到 std::string 的首选转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4728699/
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
Preferred conversion from char (not char*) to std::string
提问by pythonic metaphor
I have a char
, a plain old character, that I would like to turn into an std::string
. std::string(char)
doesn't exist of course. I could create an char array and copy it in, I could go through string streams, or many other little roundabout routes. Currently, I prefer boost::lexical_cast
, but even that seems too verbose for this simple task. So what's the preferred way?
我有一个char
,一个普通的旧字符,我想把它变成一个std::string
. std::string(char)
当然不存在。我可以创建一个字符数组并将其复制进去,我可以通过字符串流或许多其他小的迂回路线。目前,我更喜欢boost::lexical_cast
,但即使这样对于这个简单的任务来说似乎也太冗长了。那么什么是首选方式?
回答by Daniel Gallagher
std::string
has a constructor that takes a number and a character. The character will repeat for the given number of times. Thus, you should use:
std::string
有一个构造函数,它接受一个数字和一个字符。角色将重复给定的次数。因此,您应该使用:
std::string str(1, ch);
回答by Gaurav Pant
To add to the answer, you can simply use initializer list
要添加到答案中,您可以简单地使用初始化列表
std::string str = {ch};
回答by messenger
just use the overload that takes a char?
只使用带字符的重载?
i.e. string(1, 'A')
IE string(1, 'A')
回答by Maxim Egorushkin
You still can use the string constructor taking two iterators:
您仍然可以使用带有两个迭代器的字符串构造函数:
char c = 'x';
std::string(&c, &c + 1);
Update:
更新:
Good question James and GMan. Just searched freely downloadable "The New C Standard" by Derek M. Jones for "pointer past" and my first hit was:
詹姆斯和 GMan 的好问题。刚刚在 Derek M. Jones 的可免费下载的“The New C Standard”中搜索“pointer past”,我的第一个搜索结果是:
If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P... even though Q+1 does not point to an element of the array object...
On segmented architectures incrementing a pointer past the end of a segment causes the address to wrap segmented architecture around to the beginning of that segment (usually address zero). If an array is allocated within such a segment, either the implementation must ensure that there is room after the array for there to be a one past the end address, or it uses some other implementation technique to handle this case (e.g., if the segment used is part of a pointer's representation, a special one past the end segment value might be assigned)...
The C relational operator model enables pointers to objects to be treated in the same way as indexes into array objects. Relational comparisons between indexes into two different array objects (that are not both subobjects of a larger object) rarely have any meaning and the standard does not define such support for pointers. Some applications do need to make use of information on the relative locations of different objects in storage. However, this usage was not considered to be of sufficient general utility for the Committee to specify a model defining the behavior...
Most implementations perform no checks prior to any operation on values having pointer type. Most processors use the same instructions for performing relational comparisons involving pointer types as they use for arithmetic types. For processors that use a segmented memory architecture, a pointer value is often represented using two components, a segment number and an offset within that segment. A consequence of this representation is that there are many benefits in allocating storage for objects such that it fits within a single segment (i.e., storage for an object does not span a segment boundary). One benefit is an optimization involving the generated machine code for some of the relational operators, which only needs to check the segment offset component. This can lead to the situation where p >= q is false but p > q is true, when p and q point to different objects.
如果表达式 P 指向数组对象的一个元素,而表达式 Q 指向同一个数组对象的最后一个元素,则指针表达式 Q+1 比较大于 P...即使 Q+1 不指向一个数组对象的元素...
在分段体系结构上,将指针增加超过段末尾会导致地址将分段体系结构环绕到该段的开头(通常地址为零)。如果在这样的段内分配了一个数组,则实现必须确保在数组后面有空间,以便在结束地址之后有一个空间,或者它使用其他一些实现技术来处理这种情况(例如,如果段used 是指针表示的一部分,可能会分配结束段值之后的特殊值)...
C 关系运算符模型允许以与数组对象的索引相同的方式处理指向对象的指针。两个不同数组对象(不是一个更大对象的两个子对象)的索引之间的关系比较很少有任何意义,并且标准没有定义对指针的这种支持。某些应用程序确实需要利用有关存储中不同对象的相对位置的信息。然而,这种用法被认为没有足够的普遍效用让委员会指定一个定义行为的模型......
大多数实现在对具有指针类型的值进行任何操作之前不执行任何检查。大多数处理器使用与用于算术类型相同的指令来执行涉及指针类型的关系比较。对于使用分段内存体系结构的处理器,指针值通常使用两个组件表示,即段号和该段内的偏移量。这种表示的结果是为对象分配存储以使其适合单个段(即,对象的存储不跨越段边界)有很多好处。一个好处是涉及为一些关系运算符生成的机器代码的优化,它只需要检查段偏移组件。这可能导致 p >
回答by Sascha
This works on gcc C++ 4.9.2 (http://ideone.com/f3qhTe)
这适用于 gcc C++ 4.9.2 ( http://ideone.com/f3qhTe)
#include <iostream>
using namespace std;
int main() {
// your code goes here
std::string test;
test = (char) 76;
test += (char) 77;
test += (char) 78;
test += (char) 79;
std::cout << "test contains: " << test << std::endl;
return 0;
}