How to use std::copy for printing a user defined type

为君一笑 提交于 2021-01-24 12:58:06

问题


Below is the code that works perfectly for print the values of type std::string

std::vector<std::string> v;
v.push_back("this");
v.push_back("is");
v.push_back("a");
v.push_back("test");
std::copy(v.begin(),v.end(),std::ostream_iterator<std::string>(std::cout,","));

But when I am trying to print a user defined type (a structure), code is not compiling:

struct Rec
{
    int name;
    int number;
    int result;
};
int main() 
{
    Rec rec1 = {1,1,1};
    Rec rec2 = {2,1,1};
    Rec rec3 = {3,1,1};
    Rec rec4 = {4,1,1};
    Rec rec5 = {4,1,1};

    std::vector<Rec> v;
    record.push_back(rec1);
    record.push_back(rec2);
    record.push_back(rec3);
    record.push_back(rec4);
    record.push_back(rec5);

    std::copy(v.begin(),v.end(),std::ostream_iterator<Rec>(std::cout,","));

    return 1;
}

What am I missing here?


回答1:


From std::ostream_iterator

std::ostream_iterator is a single-pass OutputIterator that writes successive objects of type T into the std::basic_ostream object for which it was constructed, using operator<<. Optional delimiter string is written to the output stream after every write operation. The write operation is performed when the iterator (whether dereferenced or not) is assigned to. Incrementing the std::ostream_iterator is a no-op.

(Confirmed from comment)

You are not overloading operator << for the custom record. Use the following code to overload the operator.

ostream& operator<<(ostream& os, const Rec& r)  
{  
    os << r.name << '-' << r.number << '-' << r.result;  
    return os;  
}



回答2:


You need to implement a stream insertion operator for your record, like this:

#include <iostream>
#include <iterator>
#include <vector>

struct Rec
{
    int name;
    int number;
    int result;
};

std::ostream& operator<<(std::ostream& os, const Rec& rec)
{
    os << "{name: " << rec.name << ", number: " << rec.number 
       << ", result: " << rec.result << "}";
    return os;
}

int main()
{
    Rec rec1 = {1, 1, 1};
    Rec rec2 = {2, 1, 1};
    Rec rec3 = {3, 1, 1};
    Rec rec4 = {4, 1, 1};
    Rec rec5 = {4, 1, 1};

    std::vector<Rec> v;
    v.push_back(rec1);
    v.push_back(rec2);
    v.push_back(rec3);
    v.push_back(rec4);
    v.push_back(rec5);

    std::copy(v.begin(), v.end(), std::ostream_iterator<Rec>(std::cout, ",\n"));

    return 1;
}



回答3:


As mentioned by Mohit you have to overload<< for your custom struct, because only you know what is in your struct, so you are responsible for printing it

struct Rec
{
 int name;
 int number;
 int result;
 friend ostream& operator<<(ostream& os, const Rec& rec); // Here is the overloaded << operator 
};
ostream& operator<<(ostream& os, const Rec& rec)
{
   os<<"[" << rec.name<< '/' << rec.number<< '/' << rec.result<<"] \n";
}
int main()
{

    Rec rec1 = {1,1,1};

    Rec rec2 = {2,1,1};Rec rec3 = {3,1,1};Rec rec4 = {4,1,1};Rec rec5 = {4,1,1};
    std::vector<Rec> record;
    record.push_back(rec1);
    record.push_back(rec2);
    record.push_back(rec3);
    record.push_back(rec4);
    record.push_back(rec5);
    std::copy(record.begin(),record.end(),std::ostream_iterator<Rec>(std::cout,""));  // Now no problem here
    return 1;
}


来源:https://stackoverflow.com/questions/42106614/how-to-use-stdcopy-for-printing-a-user-defined-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!