Questions tagged [stdstring]

std::string is the C++ standard library's byte-based "string" type, defined in the header.

1177 questions
55
votes
4 answers

Remove First and Last Character C++

How to remove first and last character from std::string, I am already doing the following code. But this code only removes the last character m_VirtualHostName = m_VirtualHostName.erase(m_VirtualHostName.size() - 1) How to remove the first…
Putra Fajar Hasanuddin
  • 1,101
  • 3
  • 13
  • 25
54
votes
5 answers

Value and size of an uninitialized std::string variable in c++

If a string is defined like this std::string name; What will be the value of the uninitialized string "name" and what size it would be?
Charzhard
  • 783
  • 1
  • 7
  • 19
53
votes
6 answers

Converting a Json::Value to std::string?

I am using JsonCpp to build a JSON object. Once the object is built, is there a way I can get the object as an std::string?
BRabbit27
  • 6,333
  • 17
  • 90
  • 161
50
votes
4 answers

getting cout output to a std::string

I have the following cout statement. I use char arrays because I have to pass to vsnprintf to convert variable argument list and store in Msg. Is there any way we can get cout output to C++ std::string? char Msg[100]; char appname1[100]; char…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
49
votes
6 answers

C++20 with u8, char8_t and std::string

C++11 brought us the u8 prefix for UTF-8 literals and I thought that was pretty cool a few years ago and peppered my code with things like this: std::string myString = u8"●"; This is all fine and good, but the issue comes up in C++20 it doesn't…
M2tM
  • 4,415
  • 1
  • 34
  • 43
49
votes
2 answers

Is it bad to depend on index 0 of an empty std::string?

std::string my_string = ""; char test = my_string[0]; I've noticed that this doesn't crash, and every time I've tested it, test is 0. Can I depend on it always being 0? or is it arbitrary? Is this bad programming? Edit: From some comments, I gather…
beauxq
  • 1,258
  • 1
  • 13
  • 22
48
votes
4 answers

Can a std::string contain embedded nulls?

For regular C strings, a null character '\0' signifies the end of data. What about std::string, can I have a string with embedded null characters?
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
46
votes
1 answer

Why does the compiler prefer f(const void*) to f(const std::string &)?

Consider the following piece of code: #include #include // void f(const char *) { std::cout << "const char *"; } // <-- comment on purpose void f(const std::string &) { std::cout << "const std::string &"; } void f(const void *)…
46
votes
8 answers

How do you convert a C++ string to an int?

Possible Duplicate: How to parse a string to an int in C++? How do you convert a C++ string to an int? Assume you are expecting the string to have actual numbers in it ("1", "345", "38944", for example). Also, let's assume you don't have boost,…
krupan
  • 3,920
  • 5
  • 27
  • 24
43
votes
1 answer

Why is initializing a string to "" more efficient than the default constructor?

Generally, the default constructor should be the fastest way of making an empty container. That's why I was surprised to see that it's worse than initializing to an empty string literal: #include std::string make_default() { return…
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
42
votes
4 answers

Append int to std::string

I tried two different ways to append an int to a std::string, and to my surprise, I got different results: #include int main() { std::string s; s += 2; // compiles correctly s = s + 2; // compiler error return…
navylover
  • 12,383
  • 5
  • 28
  • 41
41
votes
4 answers

Legal to overwrite std::string's null terminator?

In C++11, we know that std::string is guaranteed to be both contiguous and null-terminated (or more pedantically, terminated by charT(), which in the case of char is the null character 0). There is this C API I need to use that fills in a string by…
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
40
votes
6 answers

Does "&s[0]" point to contiguous characters in a std::string?

I'm doing some maintenance work and ran across something like the following: std::string s; s.resize( strLength ); // strLength is a size_t with the length of a C string in it. memcpy( &s[0], str, strLength ); I know using &s[0] would be safe…
oz10
  • 153,307
  • 27
  • 93
  • 128
38
votes
7 answers

How do I convert wchar_t* to std::string?

I changed my class to use std::string (based on the answer I got here but a function I have returns wchar_t *. How do I convert it to std::string? I tried this: std::string test = args.OptionArg(); but it says error C2440: 'initializing' : cannot…
codefrog
  • 611
  • 3
  • 10
  • 13
37
votes
2 answers

std::string += operator cannot pass 0 as argument

std::string tmp; tmp +=0;//compile error:ambiguous overload for 'operator+=' (operand types are 'std::__cxx11::string {aka std::__cxx11::basic_string}' and 'int') tmp +=1;//ok tmp += '\0';//ok...expected tmp +=INT_MAX;//ok tmp…
Leo Lai
  • 863
  • 8
  • 17
1 2
3
78 79