All Questions
16,415
questions
3358
votes
83
answers
2.4m
views
How do I iterate over the words of a string?
How do I iterate over the words of a string composed of words separated by whitespace?
Note that I'm not interested in C string functions or that kind of character manipulation/access. I prefer ...
2129
votes
29
answers
4.7m
views
How to convert int to string in C++?
How can I convert from int to the equivalent string in C++? I am aware of two methods. Is there another way?
(1)
int a = 10;
char *intStr = itoa(a);
string str = string(intStr);
(2)
int a = 10;
...
1082
votes
11
answers
1.2m
views
How to convert a std::string to const char* or char*
How can I convert an std::string to a char* or a const char*?
1006
votes
31
answers
1.4m
views
How to convert an instance of std::string to lower case
I want to convert a std::string to lowercase. I am aware of the function tolower(). However, in the past I have had issues with this function and it is hardly ideal anyway as using it with a std::...
902
votes
14
answers
426k
views
std::wstring VS std::string
I am not able to understand the differences between std::string and std::wstring. I know wstring supports wide characters such as Unicode characters. I have got the following questions:
When should I ...
777
votes
26
answers
2.2m
views
How can I convert a std::string to int?
I want to convert a string to an int and I don't mean ASCII codes.
For a quick run-down, we are passed in an equation as a string. We are to break it down, format it correctly and solve the linear ...
753
votes
16
answers
1.4m
views
Check if a string contains a string in C++
I have a variable of type std::string. I want to check if it contains a certain std::string. How would I do that?
Is there a function that returns true if the string is found, and false if it isn't?...
728
votes
9
answers
829k
views
Read whole ASCII file into C++ std::string [duplicate]
I need to read a whole file into memory and place it in a C++ std::string.
If I were to read it into a char[], the answer would be very simple:
std::ifstream t;
int length;
t.open("file.txt"); /...
639
votes
44
answers
1.4m
views
std::string formatting like sprintf
I have to format std::string with sprintf and send it into file stream. How can I do this?
556
votes
32
answers
368k
views
How to convert an enum to a string in modern C++
Contrary to all other similar questions, this question is about using the new C++ features.
2008 c Is there a simple way to convert C++ enum to string?
2008 c Easy way to use variables of enum types ...
530
votes
11
answers
495k
views
How can you define a static data member of type const std::string?
I'd like to have a private static constant for a class (in this case a shape-factory).
I'd like to have something of the sort.
class A {
private:
static const string RECTANGLE = "...
488
votes
9
answers
795k
views
Differences between C++ string == and compare()?
I just read some recommendations on using
std::string s = get_string();
std::string t = another_string();
if( !s.compare(t) )
{
instead of
if( s == t )
{
I'm almost always using the last one ...
478
votes
37
answers
660k
views
How do I tokenize a string in C++?
Java has a convenient split method:
String str = "The quick brown fox";
String[] results = str.split(" ");
Is there an easy way to do this in C++?
475
votes
19
answers
1.2m
views
std::string to char*
I want to convert a std::string into a char* or char[] data type.
std::string str = "string";
char* chr = str;
Results in: “error: cannot convert ‘std::string’ to ‘char’ ...”.
What methods are ...
457
votes
6
answers
277k
views
How do I print the full value of a long string in gdb?
I want to print the full length of a C-string in GDB. By default it's being abbreviated, how do I force GDB to print the whole string?
429
votes
23
answers
464k
views
How to get rid of `deprecated conversion from string constant to ‘char*’` warnings in GCC
I'm working on an exceedingly large codebase, and recently upgraded to GCC 4.3, which now triggers this warning:
warning: deprecated conversion from string constant to ‘char*’
Obviously, the correct ...
420
votes
24
answers
529k
views
How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?
How do I implement the following (Python pseudocode) in C++?
if argv[1].startswith('--foo='):
foo_value = int(argv[1][len('--foo='):])
(For example, if argv[1] is --foo=98, then foo_value is 98.)...
387
votes
5
answers
109k
views
How exactly is std::string_view faster than const std::string&?
std::string_view has made it to C++17 and it is widely recommended to use it instead of const std::string&.
One of the reasons is performance.
Can someone explain how exactly std::string_view is/...
372
votes
30
answers
644k
views
Case-insensitive string comparison in C++ [closed]
What is the best way of doing case-insensitive string comparison in C++ without transforming a string to all uppercase or all lowercase?
Please indicate whether the methods are Unicode-friendly and ...
365
votes
22
answers
285k
views
Find out if string ends with another string in C++
How can I find out if a string ends with another string in C++?
351
votes
2
answers
241k
views
Why does std::string have a length() member function in addition to size()?
I was reading the answers for How to get the number of characters in a std::string? and found that there is actually a method called length() for std::string (I always used size()).
Is there any ...
331
votes
23
answers
590k
views
Why can't the switch statement be applied to strings?
Compiling the following code gives the error message: type illegal.
int main()
{
// Compilation error - switch expression of type illegal
switch(std::string("raj"))
{
case&...
329
votes
31
answers
751k
views
Convert a String In C++ To Upper Case
How could one convert a string to upper case. The examples I have found from googling only have to deal with chars.
304
votes
20
answers
33k
views
What's the rationale for null terminated strings?
As much as I love C and C++, I can't help but scratch my head at the choice of null terminated strings:
Length prefixed (i.e. Pascal) strings existed before C
Length prefixed strings make several ...
298
votes
5
answers
1.0m
views
How to convert a char array to a string?
Converting a C++ string to a char array is pretty straightorward using the c_str function of string and then doing strcpy. However, how to do the opposite?
I have a char array like: char arr[ ] = "...
295
votes
12
answers
509k
views
Remove last character from C++ string
How can I remove last character from a C++ string?
I tried st = substr(st.length()-1); But it didn't work.
284
votes
13
answers
481k
views
Count character occurrences in a string in C++
How can I count the number of "_" in a string like "bla_bla_blabla_bla"?
278
votes
4
answers
275k
views
Why is conversion from string literal to 'char*' valid in C but invalid in C++
The C++11 Standard (ISO/IEC 14882:2011) says in § C.1.1:
char* p = "abc"; // valid in C, invalid in C++
For the C++ it's OK as a pointer to a String Literal is harmful since any attempt to modify it ...
274
votes
19
answers
476k
views
Replace part of a string with another string
How do I replace part of a string with another string using the standard C++ libraries?
QString s("hello $name"); // Example using Qt.
s.replace("$name", "Somename");
274
votes
19
answers
513k
views
Remove spaces from std::string in C++
What is the preferred way to remove spaces from a string in C++? I could loop through all the characters and build a new string, but is there a better way?
266
votes
24
answers
151k
views
How do I read an entire file into a std::string in C++?
How do I read a file into a std::string, i.e., read the whole file at once?
Text or binary mode should be specified by the caller. The solution should be standard-compliant, portable and efficient. ...
254
votes
15
answers
482k
views
How to append a char to a std::string?
The following fails with the error prog.cpp:5:13: error: invalid conversion from ‘char’ to ‘const char*’
int main()
{
char d = 'd';
std::string y("Hello worl");
y.append(d); // Line 5 - this ...
230
votes
12
answers
407k
views
Converting an int to std::string
What is the shortest way, preferably inline-able, to convert an int to a string? Answers using stl and boost will be welcomed.
229
votes
20
answers
410k
views
C++ Convert string (or char*) to wstring (or wchar_t*)
string s = "おはよう";
wstring ws = FUNCTION(s, ws);
How would i assign the contents of s to ws?
Searched google and used some techniques but they can't assign the exact content. The content is ...
220
votes
20
answers
310k
views
How do I create a random alpha-numeric string in C++?
I'd like to create a random string, consisting of alpha-numeric characters. I want to be able to be specify the length of the string.
How do I do this in C++?
219
votes
8
answers
774k
views
std::cin input with spaces?
#include <string>
std::string input;
std::cin >> input;
The user wants to enter "Hello World". But cin fails at the space between the two words. How can I make cin take in the whole of ...
217
votes
8
answers
101k
views
What's the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?
When would I use std::istringstream, std::ostringstream and std::stringstream and why shouldn't I just use std::stringstream in every scenario (are there any runtime performance issues?).
Lastly, is ...
216
votes
9
answers
536k
views
How to use printf with std::string
My understanding is that string is a member of the std namespace, so why does the following occur?
#include <iostream>
int main()
{
using namespace std;
string myString = "Press ENTER ...
206
votes
19
answers
499k
views
How do I convert a double into a string in C++?
I need to store a double as a string. I know I can use printf if I wanted to display it, but I just want to store it in a string variable so that I can store it in a map later (as the value, not the ...
200
votes
3
answers
46k
views
Meaning of acronym SSO in the context of std::string
In a C++ question about optimization and code style, several answers referred to "SSO" in the context of optimizing copies of std::string. What does SSO mean in that context?
Clearly not "single sign ...
197
votes
21
answers
343k
views
How do you reverse a string in place in C or C++?
How do you reverse a string in C or C++ without requiring a separate buffer to hold the reversed string?
195
votes
7
answers
305k
views
How to change string into QString?
What is the most basic way to do it?
193
votes
25
answers
514k
views
How do I concatenate multiple C++ strings on one line?
C# has a syntax feature where you can concatenate many data types together on 1 line.
string s = new String();
s += "Hello world, " + myInt + niceToSeeYouString;
s += someChar1 + interestingDecimal + ...
182
votes
4
answers
235k
views
How do I convert from stringstream to string in C++?
How do I convert from std::stringstream to std::string in C++?
Do I need to call a method on the string stream?
181
votes
9
answers
399k
views
How can I create a string from a single character?
I have a single char value, and I need to cast/convert it to a std::string. How can I do this?
I know how to go the opposite way, to retrieve a char from a std::string object: you just need to index ...
180
votes
11
answers
330k
views
C++ deprecated conversion from string constant to 'char*'
I have a class with a private char str[256];
and for it I have an explicit constructor:
explicit myClass(char *func)
{
strcpy(str,func);
}
I call it as:
myClass obj("example");
When I ...
166
votes
18
answers
386k
views
Parsing a comma-delimited std::string [duplicate]
If I have a std::string containing a comma-separated list of numbers, what's the simplest way to parse out the numbers and put them in an integer array?
I don't want to generalise this out into ...
164
votes
21
answers
105k
views
Conveniently Declaring Compile-Time Strings in C++
Being able to create and manipulate strings during compile-time in C++ has several useful applications. Although it is possible to create compile-time strings in C++, the process is very cumbersome, ...
156
votes
5
answers
192k
views
stringstream, string, and char* conversion confusion
My question can be boiled down to, where does the string returned from stringstream.str().c_str() live in memory, and why can't it be assigned to a const char*?
This code example will explain it ...
152
votes
7
answers
300k
views
Difference between string and char[] types in C++
For C, we use char[] to represent strings.
For C++, I see examples using both std::string and char arrays.
#include <iostream>
#include <string>
using namespace std;
int main () {
...