string and wstring
- 传统的string只能应用于有限的西文字符,由于图书馆的信息中包含中文字符,所以我们需要引入wstring。
- 我们从外部文件读入数据,采用原始的string读入,然后再相应转换为wstring。
- 相应的,当我们将数据写入外部文件时,先将wstring转换为string,然后写入。
- 对于Windows用户,请加上头文件#include 。
wstring字符转换为string字符
- inline string wtos(const wstring&w)
- {
- intlen= WideCharToMultiByte(GetACP(), 0, w.c_str(), -1, NULL, 0, NULL, NULL);
- char *buf= new char[len];
- WideCharToMultiByte(GetACP(), 0, w.c_str(), -1, buf, len, NULL, NULL);
- string s(buf);
- delete[] buf;
- return s;
- }
string字符转换为wstring字符
- inline wstringstow(const string &s)
- {
- intlen= MultiByteToWideChar(GetACP(), 0, s.c_str(), -1, NULL, 0);
- wchar_t*buf= new wchar_t[len];
- MultiByteToWideChar(GetACP(), 0, s.c_str(), -1, buf, len);
- wstringw(buf);
- delete[] buf;
- return w;
- }