请教下 wcout 为什么没输出?还有就是 wstring 里的东西为什么是乱码
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
void Test(wstring content)
{
size_t isize = content.size();
size_t ilength = content.length();
wcout<<"size: "<<isize<<" length: "<<ilength<<" "<<content;
return;
}
int main()
{
Test(L"哈哈");
cout<<endl<<"=============="<<endl;
wstring temp;
wcin>>temp;//也输入"哈哈",用这种方式 debug 调试时, content 是乱码,isize 和 ilength 也和上面结果不同,为什么?
Test(temp);//为什么不显示?
Sleep(1000);
}
[解决办法]
#include <iostream >
#include <string >
#include <windows.h >
using namespace std;
void Test(wstring content)
{
size_t isize = content.size();
size_t ilength = content.length();
wcout<<"size: "<<isize<<" length: "<<ilength<<" "<<content;
return;
}
int main()
{
std::wcout.imbue(std::locale("chs"));
Test(L"哈哈");
cout<<endl<<"=============="<<endl;
wstring temp;
wcin>>temp; //也输入"哈哈",用这种方式 debug 调试时, content 是乱码,isize 和 ilength 也和上面结果不同,为什么?
Test(temp); //为什么不显示?
Sleep(1000);
}
[解决办法]
- C/C++ code
#include <iostream > #include <string > #include <windows.h > using namespace std; void Test(wstring content) { size_t isize = content.size(); size_t ilength = content.length(); wcout <<L"size: " <<isize <<L" length: " <<ilength <<L" " <<content; //宽字符输出,字串前都要加L return; } int main() { setlocale(LC_ALL,"chs");///这个不可少 Test(L"哈哈"); cout <<endl <<"==============" <<endl; wstring temp; wcin >>temp; Test(temp); //为什么不显示? Sleep(1000); return 0; }