c++ - overload ostream::<< to print vector of vectors -


working off chris's answer in post how print out contents of vector? trying overload ostream << print vector vectors. here's i'm reading in file, each line becomes vector:

1, 2, 3 4, 4, 4, 4, 4  7, 7 41, 52, 632 

so structure looks

v[0] = [1, 2, 3] 

and on. thought chris's solution should apply situation, , works 1d vector, i'm trying understand why doesn't work 2d vector. code's below:

template <typename t> std::ostream& operator << (std::ostream& out, const std::vector<t>& v){    if(! v.empty()){     std::cout<< '[';     std::copy(v.begin(), v.end(), std::ostream_iterator<t>(std::cout, " "));     std::cout<< ']' << std::endl;    }   else{     std::cout << "[ ]" << std::endl;   }   } 

i'm thinking need replace function in std::ostream_iterator << since overriding in first place, have tried << , ostream::<< , std:: << , these yield compile-time errors. proper way this?

if try run code above on vector of vectors of ints, have following compile-time error:

in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h: in instantiation of ‘std::ostream_iterator<_tp, _chart, _traits>& std::ostream_iterator<_tp, _chart, _traits>::operator=(const _tp&) [with _tp = std::vector<int>; _chart = char; _traits = std::char_traits<char>]’: /usr/include/c++/4.9/bits/stl_algobase.h:336:18:   required ‘static _oi std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_ii, _ii, _oi) [with _ii = const std::vector<int>*; _oi = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’ /usr/include/c++/4.9/bits/stl_algobase.h:396:70:   required ‘_oi std::__copy_move_a(_ii, _ii, _oi) [with bool _ismove = false; _ii = const std::vector<int>*; _oi = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’ /usr/include/c++/4.9/bits/stl_algobase.h:434:38:   required ‘_oi std::__copy_move_a2(_ii, _ii, _oi) [with bool _ismove = false; _ii = __gnu_cxx::__normal_iterator<const std::vector<int>*, std::vector<std::vector<int> > >; _oi = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’ /usr/include/c++/4.9/bits/stl_algobase.h:466:17:   required ‘_oi std::copy(_ii, _ii, _oi) [with _ii = __gnu_cxx::__normal_iterator<const std::vector<int>*, std::vector<std::vector<int> > >; _oi = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’ ctest.cpp:15:75:   required ‘std::ostream& operator<<(std::ostream&, const std::vector<t>&) [with t = std::vector<int>; std::ostream = std::basic_ostream<char>]’ ctest.cpp:70:18:   required here /usr/include/c++/4.9/bits/stream_iterator.h:198:13: error: no match ‘operator<<’ (operand types ‘std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >::ostream_type {aka std::basic_ostream<char>}’ , ‘const std::vector<int>’)   *_m_stream << __value;              ^ /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: candidates are: in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:108:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(std::basic_ostream<_chart, _traits>::__ostream_type& (*)(std::basic_ostream<_chart, _traits>::__ostream_type&)) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(__ostream_type& (*__pf)(__ostream_type&))        ^ /usr/include/c++/4.9/ostream:108:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’ /usr/include/c++/4.9/ostream:117:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(std::basic_ostream<_chart, _traits>::__ios_type& (*)(std::basic_ostream<_chart, _traits>::__ios_type&)) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_chart, _traits>::__ios_type = std::basic_ios<char>]        operator<<(__ios_type& (*__pf)(__ios_type&))        ^ /usr/include/c++/4.9/ostream:117:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’ /usr/include/c++/4.9/ostream:127:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(ios_base& (*__pf) (ios_base&))        ^ /usr/include/c++/4.9/ostream:127:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘std::ios_base& (*)(std::ios_base&)’ /usr/include/c++/4.9/ostream:166:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(long int) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(long __n)        ^ /usr/include/c++/4.9/ostream:166:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘long int’ /usr/include/c++/4.9/ostream:170:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(long unsigned int) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(unsigned long __n)        ^ /usr/include/c++/4.9/ostream:170:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘long unsigned int’ /usr/include/c++/4.9/ostream:174:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(bool) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(bool __n)        ^ /usr/include/c++/4.9/ostream:174:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘bool’ in file included /usr/include/c++/4.9/ostream:612:0,                  /usr/include/c++/4.9/iostream:39,                  ctest.cpp:1: /usr/include/c++/4.9/bits/ostream.tcc:91:5: note: std::basic_ostream<_chart, _traits>& std::basic_ostream<_chart, _traits>::operator<<(short int) [with _chart = char; _traits = std::char_traits<char>]      basic_ostream<_chart, _traits>::      ^ /usr/include/c++/4.9/bits/ostream.tcc:91:5: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘short int’ in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:181:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(short unsigned int) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(unsigned short __n)        ^ /usr/include/c++/4.9/ostream:181:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘short unsigned int’ in file included /usr/include/c++/4.9/ostream:612:0,                  /usr/include/c++/4.9/iostream:39,                  ctest.cpp:1: /usr/include/c++/4.9/bits/ostream.tcc:105:5: note: std::basic_ostream<_chart, _traits>& std::basic_ostream<_chart, _traits>::operator<<(int) [with _chart = char; _traits = std::char_traits<char>]      basic_ostream<_chart, _traits>::      ^ /usr/include/c++/4.9/bits/ostream.tcc:105:5: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘int’ in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:192:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(unsigned int) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(unsigned int __n)        ^ /usr/include/c++/4.9/ostream:192:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘unsigned int’ /usr/include/c++/4.9/ostream:201:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(long long int) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(long long __n)        ^ /usr/include/c++/4.9/ostream:201:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘long long int’ /usr/include/c++/4.9/ostream:205:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(long long unsigned int) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(unsigned long long __n)        ^ /usr/include/c++/4.9/ostream:205:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘long long unsigned int’ /usr/include/c++/4.9/ostream:220:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(double) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(double __f)        ^ /usr/include/c++/4.9/ostream:220:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘double’ /usr/include/c++/4.9/ostream:224:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(float) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(float __f)        ^ /usr/include/c++/4.9/ostream:224:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘float’ /usr/include/c++/4.9/ostream:232:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(long double) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(long double __f)        ^ /usr/include/c++/4.9/ostream:232:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘long double’ /usr/include/c++/4.9/ostream:245:7: note: std::basic_ostream<_chart, _traits>::__ostream_type& std::basic_ostream<_chart, _traits>::operator<<(const void*) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__ostream_type = std::basic_ostream<char>]        operator<<(const void* __p)        ^ /usr/include/c++/4.9/ostream:245:7: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘const void*’ in file included /usr/include/c++/4.9/ostream:612:0,                  /usr/include/c++/4.9/iostream:39,                  ctest.cpp:1: /usr/include/c++/4.9/bits/ostream.tcc:119:5: note: std::basic_ostream<_chart, _traits>& std::basic_ostream<_chart, _traits>::operator<<(std::basic_ostream<_chart, _traits>::__streambuf_type*) [with _chart = char; _traits = std::char_traits<char>; std::basic_ostream<_chart, _traits>::__streambuf_type = std::basic_streambuf<char>]      basic_ostream<_chart, _traits>::      ^ /usr/include/c++/4.9/bits/ostream.tcc:119:5: note:   no known conversion argument 1 ‘const std::vector<int>’ ‘std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’ in file included /usr/include/c++/4.9/string:52:0,                  /usr/include/c++/4.9/bits/locale_classes.h:40,                  /usr/include/c++/4.9/bits/ios_base.h:41,                  /usr/include/c++/4.9/ios:42,                  /usr/include/c++/4.9/ostream:38,                  /usr/include/c++/4.9/iostream:39,                  ctest.cpp:1: /usr/include/c++/4.9/bits/basic_string.h:2772:5: note: template<class _chart, class _traits, class _alloc> std::basic_ostream<_chart, _traits>& std::operator<<(std::basic_ostream<_chart, _traits>&, const std::basic_string<_chart, _traits, _alloc>&)      operator<<(basic_ostream<_chart, _traits>& __os,      ^ /usr/include/c++/4.9/bits/basic_string.h:2772:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note:   ‘const std::vector<int>’ not derived ‘const std::basic_string<_chart, _traits, _alloc>’   *_m_stream << __value;              ^ in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:471:5: note: template<class _chart, class _traits> std::basic_ostream<_chart, _traits>& std::operator<<(std::basic_ostream<_chart, _traits>&, _chart)      operator<<(basic_ostream<_chart, _traits>& __out, _chart __c)      ^ /usr/include/c++/4.9/ostream:471:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note:   deduced conflicting types parameter ‘_chart’ (‘char’ , ‘std::vector<int>’)   *_m_stream << __value;              ^ in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:476:5: note: template<class _chart, class _traits> std::basic_ostream<_chart, _traits>& std::operator<<(std::basic_ostream<_chart, _traits>&, char)      operator<<(basic_ostream<_chart, _traits>& __out, char __c)      ^ /usr/include/c++/4.9/ostream:476:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note:   cannot convert ‘__value’ (type ‘const std::vector<int>’) type ‘char’   *_m_stream << __value;              ^ in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:482:5: note: template<class _traits> std::basic_ostream<char, _traits>& std::operator<<(std::basic_ostream<char, _traits>&, char)      operator<<(basic_ostream<char, _traits>& __out, char __c)      ^ /usr/include/c++/4.9/ostream:482:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note:   cannot convert ‘__value’ (type ‘const std::vector<int>’) type ‘char’   *_m_stream << __value;              ^ in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:488:5: note: template<class _traits> std::basic_ostream<char, _traits>& std::operator<<(std::basic_ostream<char, _traits>&, signed char)      operator<<(basic_ostream<char, _traits>& __out, signed char __c)      ^ /usr/include/c++/4.9/ostream:488:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note:   cannot convert ‘__value’ (type ‘const std::vector<int>’) type ‘signed char’   *_m_stream << __value;              ^ in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:493:5: note: template<class _traits> std::basic_ostream<char, _traits>& std::operator<<(std::basic_ostream<char, _traits>&, unsigned char)      operator<<(basic_ostream<char, _traits>& __out, unsigned char __c)      ^ /usr/include/c++/4.9/ostream:493:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note:   cannot convert ‘__value’ (type ‘const std::vector<int>’) type ‘unsigned char’   *_m_stream << __value;              ^ in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:513:5: note: template<class _chart, class _traits> std::basic_ostream<_chart, _traits>& std::operator<<(std::basic_ostream<_chart, _traits>&, const _chart*)      operator<<(basic_ostream<_chart, _traits>& __out, const _chart* __s)      ^ /usr/include/c++/4.9/ostream:513:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note:   mismatched types ‘const _chart*’ , ‘std::vector<int>’   *_m_stream << __value;              ^ in file included /usr/include/c++/4.9/ostream:612:0,                  /usr/include/c++/4.9/iostream:39,                  ctest.cpp:1: /usr/include/c++/4.9/bits/ostream.tcc:321:5: note: template<class _chart, class _traits> std::basic_ostream<_chart, _traits>& std::operator<<(std::basic_ostream<_chart, _traits>&, const char*)      operator<<(basic_ostream<_chart, _traits>& __out, const char* __s)      ^ /usr/include/c++/4.9/bits/ostream.tcc:321:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note:   cannot convert ‘__value’ (type ‘const std::vector<int>’) type ‘const char*’   *_m_stream << __value;              ^ in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:530:5: note: template<class _traits> std::basic_ostream<char, _traits>& std::operator<<(std::basic_ostream<char, _traits>&, const char*)      operator<<(basic_ostream<char, _traits>& __out, const char* __s)      ^ /usr/include/c++/4.9/ostream:530:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note:   cannot convert ‘__value’ (type ‘const std::vector<int>’) type ‘const char*’   *_m_stream << __value;              ^ in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:543:5: note: template<class _traits> std::basic_ostream<char, _traits>& std::operator<<(std::basic_ostream<char, _traits>&, const signed char*)      operator<<(basic_ostream<char, _traits>& __out, const signed char* __s)      ^ /usr/include/c++/4.9/ostream:543:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note:   cannot convert ‘__value’ (type ‘const std::vector<int>’) type ‘const signed char*’   *_m_stream << __value;              ^ in file included /usr/include/c++/4.9/iostream:39:0,                  ctest.cpp:1: /usr/include/c++/4.9/ostream:548:5: note: template<class _traits> std::basic_ostream<char, _traits>& std::operator<<(std::basic_ostream<char, _traits>&, const unsigned char*)      operator<<(basic_ostream<char, _traits>& __out, const unsigned char* __s)      ^ /usr/include/c++/4.9/ostream:548:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.9/iterator:66:0,                  ctest.cpp:6: /usr/include/c++/4.9/bits/stream_iterator.h:198:13: note:   cannot convert ‘__value’ (type ‘const std::vector<int>’) type ‘const unsigned char*’   *_m_stream << __value;              ^ 

and full file:

   template <typename t>     std::ostream& operator << (std::ostream& out, const std::vector<t>& v){        if(! v.empty()){         std::cout<< '[';         std::copy(v.begin(), v.end(), std::ostream_iterator<t>(std::cout, " "));         std::cout<< ']' << std::endl;        }       else{         std::cout << "[ ]" << std::endl;       }      }      int main()     {        std::ifstream infile("filetest.txt");        std::string output;       std::string line;        std::vector<std::vector<int> > vec;        while(std::getline(infile, line))         {           std::vector<int> subvec;           int i;            std::istringstream iss(line);            while(iss >> i)         {           subvec.push_back(i);            if(iss.peek()==','|| iss.peek()== ' ')             iss.ignore();              }            std::cout << subvec;           vec.push_back(subvec);         }          std::cout << vec;     } 

it depends heavily upon how want output formatted. can use operator as-is, , it'll work (though may or may not formatting):

#include <vector> using std::vector; #include <iostream> using std::ostream;  template<typename t> ostream& operator<< (ostream& out, const vector<t>& v) {     out << "[";     size_t last = v.size() - 1;     (size_t = 0; < v.size(); ++i) {         out << v[i];         if (i != last)             out << ", ";     }     out << "]";     return out; }  int main() {     std::vector<std::vector<int> > x { { 1, 2, 3 }, { 4, 5, 6 } };      std::cout << x; } 

produces:

[[1, 2, 3], [4, 5, 6]] 

things more difficult if need/want change formatting between 1 instance , another, such having commas between numbers, , new-lines between rows. non-trivial solve well, i'll leave @ now, since it's not @ clear want that.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -