Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
basic_string
string
communitycreator

What is std::basic_string::basic_string in C/C++?

Zarish Nadeem

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The basic_string class is a generalized class that provides many functional operations on strings like searching, sorting, copying, clearing, concatenation, conversion, comparisons, and more.

The class definition and all utility functionalities are provided by namespace std, e.g., the typedef statements. The basic_string class supports the following character sets:

  • char
  • wchar_t

basic_string requires a header file:

#include <string>

string

A string is a specialized instance of the basic_string class with type char.

//typedef in namespace std
typedef basic_string<char> string
string

Code

#include <iostream>
#include <string>
using namespace std;
int main() {
//declare a string
string str("I love chicks"); //calling constructor
//print string and its length
cout<<str<<endl<<str.length()<<endl;
//declare anothor string
string str2=str; //assignment operator of string class
str2+=" and goats"; //contatenation
cout<<str2<<endl<<str2.length()<<endl;
return 0;
}
A simple code to show some useful functionalities of string.

wstring

This is a specialized instance of the basic_string class with type wchar_t.

//typedef in namespace std
typedef basic_string<wchar_t> wstring
wstring

Note: Type wchar_t commonly represents Unicode, but the standard does not specify its size and is implementation-defined. C++ 11 has also support for types char16_t and char32_t. Visit www.unicode.org for more detail on the Unicode Standard.

Code

#include <iostream>
#include <string>
using namespace std;
int main() {
//declare a wide character string
wstring str(L"programming is fun");
//declare anothor wstring
wstring str1=(L"traveling is fun");
//contatenation
wstring str2= str+ L" and "+str1;
//wcout is a output stream oriented to wide characters
//wchar_t
wcout<<str<<endl<<str1<<endl<<str2<<endl;
//size of character in terms of bytes
wcout<<sizeof(wchar_t)<<endl;
return 0;
}

Unlike char strings, strings are not necessarily null-terminated.

RELATED TAGS

c++
basic_string
string
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring