How to print out the contents of a vector?

后端 未结 19 1193
旧时难觅i
旧时难觅i 2020-11-22 03:46

I want to print out the contents of a vector in C++, here is what I have:

#include 
#include 
#include 
#include         


        
19条回答
  •  死守一世寂寞
    2020-11-22 04:17

    For those that are interested: I wrote a generalized solution that takes the best of both worlds, is more generalized to any type of range and puts quotes around non-arithmetic types (desired for string-like types). Additionally, this approach should not have any ADL issues and also avoid 'surprises' (since it's added explicitly on a case-by-case basis):

    template 
    inline constexpr bool is_string_type_v = std::is_convertible_v;
    
    template
    struct range_out {
      range_out(T& range) : r_(range) {
      }
      T& r_;
      static_assert(!::is_string_type_v, "strings and string-like types should use operator << directly");
    };
    
    template 
    std::ostream& operator<< (std::ostream& out, range_out& range) {
      constexpr bool is_string_like = is_string_type_v;
      constexpr std::string_view sep{ is_string_like ? "', '" : ", " };
    
      if (!range.r_.empty()) {
        out << (is_string_like ? "['" : "[");
        out << *range.r_.begin();
        for (auto it = range.r_.begin() + 1; it != range.r_.end(); ++it) {
          out << sep << *it;
        }
        out << (is_string_like ? "']" : "]");
      }
      else {
        out << "[]";
      }
    
      return out;
    }
    

    Now it's fairly easy to use on any range:

    std::cout << range_out{ my_vector };
    

    The string-like check leaves room for improvement. I do also have static_assert check in my solution to avoid std::basic_string<>, but I left it out here for simplicity.

提交回复
热议问题