0
我想使用iostream_iterator元組。 我有超載輸入操作符>>元組。 但編譯器給編譯錯誤。 問題與iostream_iterator與元組C++
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <tuple>
using namespace std;
using rec_t= tuple<int, string>;
istream& operator>>(istream& strm, rec_t& r) {
strm>>get<0>(r)>>get<1>(r);
return strm;
}
int main() {
int n;
cin>>n;
vector<rec_t> ar(n);
copy_n(istream_iterator<rec_t>(cin), n, begin(ar));
return 0;
}
編譯器錯誤信息是:
--------------體形:調試(編譯器:GNU GCC編譯器)-------- -------
g++ -Wall -fexceptions -std=c++11 -g -c main.cpp -o obj/Debug/main.o
In file included from /usr/include/c++/4.9/iterator:66:0,
from main.cpp:4:
/usr/include/c++/4.9/bits/stream_iterator.h: In instantiation of ‘void std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::_M_read() [with _Tp = std::tuple<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >; _CharT = char; _Traits = std::char_traits<char>; _Dist = long int]’:
/usr/include/c++/4.9/bits/stream_iterator.h:70:17: required from ‘std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type&) [with _Tp = std::tuple<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >; _CharT = char; _Traits = std::char_traits<char>; _Dist = long int; std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type = std::basic_istream<char>]’
main.cpp:18:39: required from here
/usr/include/c++/4.9/bits/stream_iterator.h:121:17: error: cannot bind ‘std::istream_iterator<std::tuple<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::istream_type {aka std::basic_istream<char>}’ lvalue to ‘std::basic_istream<char>&&’
*_M_stream >> _M_value;
^
In file included from /usr/include/c++/4.9/iostream:40:0,
from main.cpp:2:
/usr/include/c++/4.9/istream:872:5: note: initializing argument 1 of ‘std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::tuple<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]’
operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
^
Process terminated with status 1 (0 minute(s), 1 second(s))
1 error(s), 3 warning(s) (0 minute(s), 1 second(s))
但它當然是不合法的,把它放在'std'中。 – 2015-03-19 05:44:08