次のコードを考慮してください。私はostreamにベクトルのベクトルを出力しようとしています。ostreamへのベクトルのベクトルを出力
#include <iterator>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
template<typename T>
std::ostream &operator <<(std::ostream &os, const std::vector<T> &v) {
using namespace std;
copy(v.begin(), v.end(), ostream_iterator<T>(os, "\n"));
return os;
}
int main() {
using namespace std;
vector<string> v1;
cout << v1;
vector<vector<string> > v2;
cout << v2;
return 0;
}
文字列のベクトルを出力するステートメントが機能します。私が文字列のベクトルのベクトルを出力するものはありません。私はg ++ 4.7.0を使用しています。私は、-std = C++ 11フラグなしで&を試しました。 C++ 11モードでは、エラーの半ページにこの行が表示されます。
error: cannot bind 'std::ostream_iterator<std::vector<std::basic_string<char> >, char, std::char_traits<char> >::ostream_type {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
私はそれが何を意味するかはわかりません。誰かが私に説明することができますか?私は多かれ少なかれrvalue参照が何であるか知っていますが、std::basic_ostream<char>
がstd::basic_ostream<char>&&
にバインドしない理由がわかりません。たぶん私は十分にそれをよく知りません。これを行うにはよりよい方法がありますか?
ありがとうございます。
これについて考えると、 'vector>' case、 "\ n"はキャスト可能で、ベクトル 'にする必要があります。原因のそれは動作しません –
@ J - 16SDiZ:何のことを言っている?いいえ、それはありません。 –
@ J-16SDiZ:いいえ、それは単に間違っています。 – Nawaz