This question already has answers here:
What is the type of a string literal in C++? [duplicate]
(2 answers)
Closed 7 years ago.
When I use :
std::cout << "Hello world ";
Which type is "Hello world" ?
Where does it stored , so I can get it out and work with it ?
For some reasons, I don't want to use something like :
std::string str = "Hello world";
std::cout << str;
Please help me, I searched an hour but still no answer.
The type of a string literal is "constant array of char", with as many elements as characters in the literal, plus one for a final null character. Other versions of string literals (wide, unicode) are arrays of other character types (wchar_t, char16_t etc.) (e.g. see here).
Related
This question already has answers here:
escape R"()" in a raw string in C++
(2 answers)
Include )" in raw string literal without terminating said literal
(3 answers)
Closed 9 months ago.
I want to output a string like this: onclick="func()". So I wrote the following code:
std::string s = R"(
onclick="func()"
)";
But here two )" let the compiler confused.
Please forgive me if it's a silly question.
I googled but found nothing (I don't know which keyword I should use when I googled).
Simply add a unique string outside the ()
std::string s = R"anystring(
onclick="func()"
)anystring";
This question already has answers here:
What do single quotes do in C++ when used on multiple characters?
(5 answers)
Closed 3 years ago.
#include <iostream>
int main() {
std::cout << 'hello';
return 0;
}
This program output is:
1701604463
I wonder why it actually runs, although the compiler gives a warning message: character constant too long for its type.
What these numbers actually mean, are they garbage digits?
It is multicharacter literal which has type int.
Multicharacter literal, e.g. 'AB', has type int and
implementation-defined value.
This question already has answers here:
Getting the actual length of a UTF-8 encoded std::string?
(11 answers)
Closed 4 years ago.
Consider
#include <string>
#include <iostream>
int main()
{
std::string test="αλφα";
std::cout << "size() of '" << test << "' = " << test.size() << std::endl;
}
which produces
size() of 'αλφα' = 8
How can I with the C++ standard library find the width of the output that will be produced by writing a string (i.e. 4 in the example above)?
The problem here is related to the encoding associated with the string.
This looks like UTF-8 encoding to me (the first character is not the lower case 'a'). In that encoding, the characters you present take two bytes each which accounts for the answer.
UTF-8 encoding is broadly supported by the C++11 standard (rather elegantly UTF-8 doesn't have any zero bytes in any text stream cf. Windows Unicode) - you can use std::string although the lengths will, in general, be understated - but care must be taken when creating string literals of that type directly in your editor.
More reading from here: How to use Unicode (UTF-8) in C++
This question already has answers here:
Rules for C++ string literals escape character
(6 answers)
Closed 6 years ago.
I am trying to create a QStringList containing all punctuation signs.
How can I add the element " into it ?
You can use \ to escape the character ". The code may look like this:
QStringList foo;
foo << "\"";
An other option would be to construct a QString from a char declared between simple quotes ':
foo << QString('"');
Since the constructor isn't declared as explicit in documentation, this should also work with implicit conversion:
foo << '"';
This question already has answers here:
Get the last element of a std::string
(4 answers)
Closed 7 years ago.
In python you can say print "String"[-1] and it would print be the last character, 'g'. Is there an equivalent for this in c++?
You can use string.back() to get a reference to the last character in the string. The last character of the string is the first character in the reversed string, so string.rbegin() will give you an iterator to the last character.
Use the back() function for std::string:
std::string str ("Some string");
cout << str.back()
Output:
g
For C strings, it is
String[strlen(String) - 1];
For C++ style strings, it is either
String.back();
*String.rbegin();
String[String.length() - 1];
You can use the function:
my_string.back();
If you want to output it, then:
#include <iostream>
std::cout << my_string.back();