std::string is the C++ standard library's byte-based "string" type, defined in the
Questions tagged [stdstring]
1177 questions
91
votes
16 answers
How do you convert CString and std::string std::wstring to each other?
CString is quite handy, while std::string is more compatible with STL container. I am using hash_map. However, hash_map does not support CStrings as keys, so I want to convert the CString into a std::string.
Writing a CString hash function seems to…

user25749
- 4,825
- 14
- 61
- 83
90
votes
1 answer
What is the point of STL Character Traits?
I notice that in my copy of the SGI STL reference, there is a page about Character Traits but I can't see how these are used? Do they replace the string.h functions? They don't seem to be used by std::string, e.g. the length() method on std::string…

Matthew Smith
- 6,165
- 6
- 34
- 35
90
votes
6 answers
Error: invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’
At the top of my file I have
#define AGE "42"
Later in the file I use ID multiple times including some lines that look like
std::string name = "Obama";
std::string str = "Hello " + name + " you are " + AGE + " years old!";
str += "Do you feel " +…

janovak
- 1,531
- 2
- 12
- 22
88
votes
12 answers
char* vs std::string in c++
When should I use std::string and when should I use char* to manage arrays of chars in C++?
It seems you should use char* if performance(speed) is crucial and you're willing to accept some of a risky business because of the memory management.
Are…
anon
84
votes
4 answers
Concatenating strings doesn't work as expected
I know it is a common issue, but looking for references and other material I don't find a clear answer to this question.
Consider the following code:
#include
// ...
// in a method
std::string a = "Hello ";
std::string b =…

Andry
- 16,172
- 27
- 138
- 246
81
votes
6 answers
What are some algorithms for comparing how similar two strings are?
I need to compare strings to decide whether they represent the same thing. This relates to case titles entered by humans where abbreviations and other small details may differ. For example, consider the following two titles:
std::string first =…

WilliamKF
- 41,123
- 68
- 193
- 295
77
votes
3 answers
c++ integer->std::string conversion. Simple function?
Problem: I have an integer; this integer needs to be converted to a stl::string type.
In the past, I've used stringstream to do a conversion, and that's just kind of cumbersome. I know the C way is to do a sprintf, but I'd much rather do a C++…

Paul Nathan
- 39,638
- 28
- 112
- 212
75
votes
14 answers
Padding stl strings in C++
I'm using std::string and need to left pad them to a given width. What is the recommended way to do this in C++?
Sample input:
123
pad to 10 characters.
Sample output:
123
(7 spaces in front of 123)

Alex B
- 24,678
- 14
- 64
- 87
74
votes
9 answers
Are there downsides to using std::string as a buffer?
I have recently seen a colleague of mine using std::string as a buffer:
std::string receive_data(const Receiver& receiver) {
std::string buff;
int size = receiver.size();
if (size > 0) {
buff.resize(size);
const char* dst_ptr =…

duong_dajgja
- 4,196
- 1
- 38
- 65
70
votes
3 answers
std::string::c_str() and temporaries
Is the following C++ code well-formed:
void consumer(char const* p)
{
std::printf("%s", p);
}
std::string random_string_generator()
{
// returns a random std::string object
}
consumer(random_string_generator().c_str());
The problem I have…

user1095108
- 14,119
- 9
- 58
- 116
69
votes
5 answers
I want to convert std::string into a const wchar_t *
Is there any method?
My computer is AMD64.
::std::string str;
BOOL loadU(const wchar_t* lpszPathName, int flag = 0);
When I used:
loadU(&str);
the VS2005 compiler says:
Error 7 error C2664:: cannot convert parameter 1 from 'std::string *__w64 '…

user25749
- 4,825
- 14
- 61
- 83
68
votes
5 answers
How to check if a string contains a char?
I have a text file that I want to read. I want to know if one of the lines contains [ so I tried :
if(array[i] == "[")
But this isn't working.
How can I check if a string contains a certain character?

Robert Lewis
- 701
- 1
- 5
- 3
65
votes
8 answers
How to construct a std::string from a std::vector?
I'd like to build a std::string from a std::vector.
I could use std::stringsteam, but imagine there is a shorter way:
std::string string_from_vector(const std::vector &pieces) {
std::stringstream ss;
…

WilliamKF
- 41,123
- 68
- 193
- 295
61
votes
2 answers
Does std::atomic work appropriately?
I am reading through Anthony Williams' "C++ Concurrency in Action" and in Chapter 5, which talks about the new multithreading-aware memory model and atomic operations, and he states:
In order to use std::atomic for some user-defined UDT, this…

Thomas Russell
- 5,870
- 4
- 33
- 68
59
votes
9 answers
Convert a number to a string with specified length in C++
I have some numbers of different length (like 1, 999, 76492, so on) and I want to convert them all to strings with a common length (for example, if the length is 6, then those strings will be: '000001', '000999', '076492').
In other words, I need…

Degvik
- 3,050
- 6
- 25
- 20