Difference Between std::wstring and std::string
Last Updated :
01 Feb, 2024
The std::wstring and std::string are the classes in C++ used to store sequences of characters. While serving similar purposes, they serve different requirements. In this article, we will look at some major differences between the std::wstring and std::string in C++.
Wide String in C++
The std::wstring (also called Wide String) is used to represent the string of wide characters. The "w" in the string stands for the word "wide" indicating that it handles wide character encodings, which usually require more than one byte per character. This makes wstring
suitable for supporting a large character set, including Unicode characters and characters from various languages.
To use wstring
, we need to include the <string>
header.
Syntax
wstring nameOfwstring = L" " ;
Here, the L
prefix before the string indicates that it's a wide string literal.
Example
C++
// C++ program to demonstrate the declaration and use of
// wstring in C++.
#include <iostream>
#include <string>
using namespace std;
int main()
{
// For wide character strings, use std::wstring (Unicode
// support)
wstring wString = L"Hello, GeeksForGeeks";
// Putting the long string on display
wcout << wString << endl;
// Reaching specific large characters
for (wchar_t ch : wString) {
wcout << ch << L" ";
}
return 0;
}
OutputHello, GeeksForGeeks
H e l l o , G e e k s F o r G e e k s
Narrow String in C++
The std::string
class in the C++ standard library represents a string of narrow characters. It is designed to handle characters in the ASCII character set. Compared to wide character strings, each character in a string
usually takes only one byte of storage, making it more space-efficient.
Syntax
string stringName=" " ;
Example
C++
// C++ program to demonstrate the use of string
#include <iostream>
#include <string>
using namespace std;
int main()
{
// creating string name myStr
string myStr = "Hello, GeeksForGeeks!";
// Displaying the string
cout << myStr << endl;
// Access individually
for (char ch : myStr) {
cout << ch << " ";
}
return 0;
}
OutputHello, GeeksForGeeks!
H e l l o , G e e k s F o r G e e k s !
Difference Between std::wstring and std::string
The main distinction between the wstring and string is the kind of characters they store. Wide characters represents Unicode characters and require more than one byte of storage, are intended to be handled by wstring. Whereas, the purpose of string is to handle narrow characters, which are often characters that fall under the expanded ASCII character set or the ASCII character set.
The below table illustrates the key differences among them:
|
Character type of wstring is wchar_t
| Character type of string is char
|
It is mainly used for wide characters.
| It is mainly used for narrow characters .
|
It supports Unicode, with most characters requiring more than one byte.
| It supports ASCII or extended ASCII characters.
|
I/O stream used is wcout.
| I/O stream used is cout.
|
It is suitable for multilingual text.
| It is suitable for single-byte encoding.
|
String literal prefix L is used (e.g., L"Hello" ).
| No string literal prefix is used.
|
Conclusion
In conclusion string is more useful while working with smaller character sets, whereas wstring is made to handle wide characters and serve a wider range of languages. The choice between them depends on the specific needs of the program, taking into account factors like memory utilization and internationalization.
Similar Reads
Difference Between Null Strings and Empty String in C++ In C++, we generally initialize a string that does not store anything at declaration as either a null string or an empty string. They both may seem similar to each other but they are different in terms of what they store or refer to. In this article, we will discuss the key differences between the n
3 min read
Difference Between string and char[] Types in C++ In C++, we can store the sequence of characters i.e. string in two ways: either as a std::string object or char array. In this article, we will discuss some major differences between string and char[] in C++.Character Array Type StringsA character array is simply an array in C++ that contains charac
2 min read
What is the Difference Between C++ String == and compare()? In C++ == and compare() both are used to compare strings and find if the given strings are equal or not but they differ in working. In this article, we will learn the key differences between == and compare() of string in C++. "==" Operator in C++The == operator in C++ is used to compare two strings
3 min read
Difference Between Array of Characters and std::string in C++ In C++, we have character array and std::string class both of which are used to store the sequence of characters. The character arrays are a part of C-style programming on the other hand std::string is a part of the C++ standard library. In this article, we will discuss what are some major differenc
3 min read
std::string::crbegin() and std::string::crend() in C++ with Examples std::string::crbegin() The std::string::crbegin() is a string class built-in function that returns a constant reverse iterator referring to the last element in the string. Using this iterator starts the string traversal from the end of the string. Header File: #include <string> Template Class:
3 min read
Processing strings using std::istringstream The std::istringstream is a string class object which is used to stream the string into different variables and similarly files can be stream into strings. Objects of this class use a string buffer that contains a sequence of characters. This sequence of characters can be accessed as a string object
5 min read
How to Convert wstring to int in C++? In C++, std::wstring is a type of string where each character is of a wide character type. These types of strings can be used to store the numerical strings which can then be converted to their corresponding type. In this article, we will see how to convert a wstring to an int in C++. Input: wstr =
2 min read
Convert char* to std::string in C++ Strings are generally represented as an instance of std::string class in C++. But the language also supports the older C-Style representation where they are represented as array of characters (char* or char[]) terminated by null character '\0'. In this article, we will learn how to convert the char*
3 min read
std::string::empty() in C++ The std::string::empty() function in C++ is a predefined member function of the std::string class template. This function is used to check if a string is empty, i.e., whether its length is 0 or not, and returns a boolean value true if the string is empty otherwise, it returns false. Syntax of String
2 min read
Create a string of specific length in C++ C++ has in its definition a way to represent a sequence of characters as an object of a 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 characters. This article focuses on discussing how t
3 min read