简体   繁体   English

std :: vector的字符串表示形式<T>

[英]String representation of std::vector<T>

Is it possible to implement a C++ function which gives a string representation of every std::vector<T> , as long as the element of type T can be appended to an output stream like 是否可以实现一个C ++函数,该函数给出每个std::vector<T>的字符串表示形式,只要可以将T类型的元素附加到输出流中,例如

T x;
...
std::cout << x << std::endl;

The string representation should look like 字符串表示应如下所示

[x, y, z]

I've attempted the following, but what should ? 我尝试了以下操作,但是应该?? be? 是?

template <typename T> std::string vectorToString(std::vector<T>& vec) {
    std::string s;
    for (T element : vec) {
        ?
    }
    return s;
}

You'll want a stringstream to do the formatting: 您需要使用stringstream进行格式化:

std::ostringstream ss;
ss << '['
bool first = true;
for (T const & element : vec) {
    if (!first) {
        ss << ", ";
    }
    ss << element;
    first = false;
}
ss << ']';
return ss.str();

Use a std::ostringstream instance for ? std::ostringstream实例用于? and return std::ostringstream::str() : 并返回std::ostringstream::str()

std::ostringstream s;
s << "[";

for (auto i(vec.begin()); i != vec.end(); i++)
{
    if (vec.begin() != i) s << ", ";
    s << *i;
}
s << "]";

return s.str();

If you are working on C++11, you can use this simple version: 如果您使用的是C ++ 11,则可以使用以下简单版本:

#include <sstream>
#include <algorithm>

using namespace std;

template<typename T>
string format(vector<T> const& v)
{
    if (v.empty()) return "[]";
    ostringstream ss;
    ss << "[" << v[0];
    for_each(begin(v) + 1, end(v), [&ss] (T const& s) { ss << ", " << s; });
    ss << "]";
    return ss.str();
}

If you want to make it generic for other types of collections (not just vector ) or even for sub-ranges of a collection , you can generalize it this way: 如果要使其对其他类型的集合 (而不仅仅是vector )甚至对集合的子范围通用,则可以通过以下方式将其通用化:

#include <sstream>
#include <algorithm>

using namespace std;

template<typename It>
string format(It b, It e)
{
    if (b == e) return "[]";
    ostringstream ss;
    ss << "[" << *b;
    for_each(++b, e, [&ss] (decltype(*b)& s) { ss << ", " << s; });
    ss << "]";
    return ss.str();
}

template<typename C>
string format(C const& c)
{
    return format(begin(c), end(c));
}

int main()
{
    vector<int> v = { 4, 5, 5, 8 };
    cout << format(v) << endl;
    return 0;
}

A little bit faster version 更快一点的版本

template <typename T> std::string vectorToString( const std::vector<T>& vec ) {
    if ( vec.empty() ) {
        return "[]";
    }
    std::ostringstream s;
    s << "[" << vec.front();

    for (auto i = vec.begin() + 1, e = vec.end(); i != e; i++)
    {
        s << ", " << *i;
    }
    s << "]";

    return s.str();
}

Another moment: maybe it would be right to specialize this function for strings and quote them, because if a string in vector begins or ends with , it would be hard to understand how many strings was printed. 另一刻:将此函数专用于字符串并引用它们可能是正确的,因为如果vector中的字符串以开头或结尾,将很难理解打印了多少个字符串。

template <>
std::string vectorToString< std::string >( const std::vector<std::string>& vec ) {
    if ( vec.empty() ) {
        return "[]";
    }
    std::ostringstream s;
    s << "[" << vec.front();

    for (auto i = vec.begin() + 1, e = vec.end(); i != e; i++)
    {
        s << ", \"" << *i << "\"";
    }
    s << "]";

    return s.str();
}

Using algorithms, which usually simplify code (not sure if in this case, but added for the sake of completion): 使用通常简化代码的算法(不确定是否在这种情况下,但是为了完成而添加):

template <typename T>
std::string toString( std::vector<T> const & v ) {
   if (v.empty()) 
      return "[]";
   typename std::vector<T>::const_iterator last = std::prev(v.end());
   std::ostringstream st;
   st << "[ ";
   std::copy( v.begin(), last, std::ostream_iterator<T>(st,", ") );
   st << *last << " ]";
   return st.str();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM