Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any advantage of using the s suffix in C++ [duplicate]

Tags:

My question is related to the use of the "s" suffix in C++?

Example of code using the "s" suffix:

auto hello = "Hello!"s; // a std::string 

The same could be written as:

auto hello = std::string{"Hello!"}; 

I was able to find online that the "s" suffix should be used to minimizes mistakes and to clarify our intentions in the code.

Therefore, is the use of the "s" suffix only meant for the reader of the code? Or are there others advantages to its use?

like image 221
Rpessoa Avatar asked Jan 26 '18 16:01

Rpessoa


People also ask

What is std::string_view for?

The std::string_view, from the C++17 standard, is a read-only non-owning reference to a char sequence. The motivation behind std::string_view is that it is quite common for functions to require a read-only reference to an std::string-like object where the exact type of the object does not matter.

What is std :: string& in C ++?

C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

Does \n count as a character?

LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.

How do you declare a wide character in string literal?

A wide string literal is a null-terminated array of constant wchar_t that is prefixed by ' L ' and contains any graphic character except the double quotation mark ( " ), backslash ( \ ), or newline character. A wide string literal may contain the escape sequences listed above and any universal character name.


1 Answers

Null characters can be included in the raw string trivially; example from http://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s

int main() {     using namespace std::string_literals;      std::string s1 = "abc\0\0def";     std::string s2 = "abc\0\0def"s;     std::cout << "s1: " << s1.size() << " \"" << s1 << "\"\n";     std::cout << "s2: " << s2.size() << " \"" << s2 << "\"\n"; } 

Possible output:

s1: 3 "abc" s2: 8 "abc^@^@def" 
like image 194
UKMonkey Avatar answered Oct 11 '22 12:10

UKMonkey