[英]C++ problem to write vector data to file
我想将向量v的内容写入文件。 问题在于,不是内容而是地址将放置在文本文件中。
当我用* pos 代替 &pos时,出现错误: 错误C2679:二进制'<<':未找到采用'entry'类型的右操作数的运算符
如何正确运作?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <limits>
/*
Data.txt
John
6543
23
Max
342
2
A Team
5645
23
*/
struct entry
{
// passengers data
std::string name;
int weight; // kg
std::string group_code;
};
void reservations(std::vector<entry> v)
{
std::ofstream outfile;
outfile.clear();
outfile.open("reservations.txt");
// print data in vector
std::vector<entry>::iterator pos;
for (pos = v.begin(); pos!= v.end();++pos)
{
outfile << &pos << std::endl;
std::cout << &pos << std::endl;
}
outfile.close();
}
entry read_passenger(std::ifstream &stream_in)
{
entry passenger;
if (stream_in)
{
std::getline(stream_in, passenger.name);
stream_in >> passenger.weight;
stream_in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(stream_in, passenger.group_code);
stream_in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return passenger;
}
return passenger;
}
int main(void)
{
std::ifstream stream_in("data.txt");
std::vector<entry> v; // contains the reservations
std::vector<entry> a; // contains the cancellations
const int limit_total_weight = 10000; // kg
int total_weight = 0; // kg
entry current;
while (stream_in)
{
current = read_passenger(stream_in);
if (total_weight + current.weight >= limit_total_weight)
{
// push data (cancellations) to vector
a.push_back(current);
}
else
{
total_weight = total_weight + current.weight;
// push data (reservations) to vector
v.push_back(current);
}
}
reservations(v); // write reservations to file
std::cout << "Rest " << limit_total_weight - total_weight << "kg" <<std::endl;
return 0;
}
您应该重载operator <<
以便entry
:
std::ostream& operator << (std::ostream& o, const entry& e)
{
return o << e.name
<< " " << e.weight
<< " " << e.gruop_code;
}
现在您可以编写:
outfile << *pos << std::endl;
std::cout << *pos << std::endl;
首先,您正在编写一个指针,然后您没有告诉C ++ entry
应如何用文本表示。
创建一个operator<<
重载。
您将必须执行outfile << pos->name << " " << pos->weight << /* etc */
,或实现输出运算operator<<
。
它看起来像
std::ostream& operator<<(std::ostream& os, const entry&) {
return os << entry.name << " " << entry.weight << " " << entry.group_code;
}
根据需要设置格式。
由于entry
是普通结构,并且您尚未定义任何operator<<
方法将其内容写入输出流,因此,当您尝试直接写出*pos
时,编译器会抱怨。
您需要定义一个合适的operator<<
方法,或者逐个写出struct的成员,例如
outfile << pos->name << std::endl;
outfile << pos->weight << std::endl;
outfile << pos->group_code << std::endl;
尝试类似:
std::ostream& operator<<(std::ostream& os,const entry& toPrint)
{
os << "name :" << toPrint.name << '\n';
os << "weight :" << toPrint.weight << "(kg) \n";
os << "group_code :" << toPrint.group_code << '\n';
return os;
}
您也可能希望将保留功能的签名更改为
void reservations(const std::vector<entry>& v)
您的输出看起来像带有当前代码的地址的原因是因为您正在输出迭代器的地址。
之所以出现错误“没有找到采用'entry'类型的右手运算符的错误”的原因是因为没有<<
将entry
作为其右手操作数的运算符。 您可以轻松定义一个:
std::ostream&
operator<<( std::ostream& dest, entry const& obj )
{
dest << obj.name;
return dest;
}
但这有点专业。 在其他情况下,您可能希望输出其他entry
字段。 实际上,经典而言,实际上,用于entry
的operator<<
将输出所有相关字段。 一种替代解决方案是定义一个变压器功能对象并使用它:
struct EntryToName
{
std::string operator()( entry const& obj ) const
{
return obj.name;
}
};
然后在reservations
:
std::transform(
v.begin(), v.end(),
std::ostream_iterator<std::string>( outfile, "\n" ),
EntryToName() );
顺便说一句,通常认为最好通过const引用而不是通过值传递向量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.