oStream不显示概率C ++
ostream does not display proberly c++
我有一个项目,其中我使用人员目录。 在我的主课上,我将 cout <<my<<Directory 称为 endl;以显示此目录中人员的整个列表。 为此,我覆盖了目录类和 Person 类中的 <<运算符。
在目录类中,我出于特定原因使用地图,因此我以这种方式显示每个人:
std::ostream& operator<<(std::ostream& os, const T& myDirectory)
{
typedef map<Person, Location>::const_iterator Iterator;
for(Iterator it=myDirectory.m_directory.begin();it!=myDirectory.m_directory.end;++it){
os<<it->first<<endl;
}
return os;
为此,我还必须以这种方式重载 Person 类中的 <<运算符:
ostream& operator<<(std::ostream& os, const Person& p){
os << p.lastName << "," << p.firstName << endl;
return os;
}
我的问题是它没有正确显示。 逗号在姓氏和姓氏之前,名字和名字合并在一起。 例如,如果我的名字是杰克丹尼尔斯,我会在输出中得到这个:
,杰克尔斯
如果我使用 endl; 在姓氏和名字之后,它工作得很好。 例如:
os << p.lastName << endl;
os << "," << p.firstName << endl;
我得到这个输出:
丹尼尔斯
千斤顶
知道我做错了什么吗?
在这里
猜测,因为您没有提供完整的示例来说明问题,但我认为您会在姓氏末尾有一个尾随的"返回"字符。
它输出"Daniels"并将光标移回行首,然后用",Jack"覆盖它,因此输出将是",Jackls"
Endl 还会清理缓冲区。也许你应该添加out.flush()而不是endl,然后它会起作用吗?只是猜测。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何根据单词在文本中出现的概率输出单词
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- oStream不显示概率C ++