[英]Print out a std::vector<std::wstring>
我試圖使用std :: copy修改它以打印出std::vector<std::wstring>
的內容,但我不了解該代碼的工作方式是否足夠好,並且無法對其進行編譯。 代碼應該是什么?
我使用了Rob的示例,但它不起作用:
std::vector<std::wstring> keys = ...;
std::copy(keys.begin(), keys.end(), std::ostream_iterator<std::wstring>(std::wcout, " "));
我收到錯誤:
1>error C2665: 'std::ostream_iterator<_Ty>::ostream_iterator' : none of the 2 overloads could convert all the argument types
1> with
1> [
1> _Ty=std::wstring
1> ]
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\iterator(300): could be 'std::ostream_iterator<_Ty>::ostream_iterator(std::basic_ostream<_Elem,_Traits> &,const _Elem *)'
1> with
1> [
1> _Ty=std::wstring,
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> while trying to match the argument list '(std::wostream, const char [2])'
當我使用wcout
類型的wostream
時,為什么告訴我_Elem=char
?
您必須使用wstring, wchar_t
作為模板參數,並使用wcout
作為輸出流:
std::copy(v.begin(),
v.end(),
std::ostream_iterator<std::wstring, wchar_t>(std::wcout, L"\n"));
測試程序:
#include <vector>
#include <string>
#include <iostream>
#include <iterator>
int main () {
std::vector<std::wstring> v;
v.push_back(L"Hello");
v.push_back(L"World");
std::copy(v.begin(),
v.end(),
std::ostream_iterator<std::wstring, wchar_t>(std::wcout, L"\n"));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.