The C++ wstring type, the wide character string program example


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Comparing two wide strings using wstring template specialization in C++ programming

To show: How to use the C++ wstring type, a type that describes a specialization of the basic_string template class with elements of type wchar_t as a wstring




// the C++ wstring type

#include <string>

#include <iostream>

using namespace std;


int main(void)


// equivalent ways to declare an object of type basic_string <wchar_t>

const basic_string <wchar_t> str1(L"yourabcdef");

// uses the typedef for wstring

wstring str2(L"myabcdef");


// comparison between two objects of type basic_string <wchar_t>

if (str1 == str2)

cout<<"The strings str1 and str2 are equal."<<endl;


cout<<"The strings str1 and str2 are not equal."<<endl;


return 0;



Output example:


The strings str1 and str2 are not equal.

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index