循环中的setPrecision
Setprecision in a loop
本文关键字:setPrecision 循环 更新时间:2023-10-16
struct movie{
int week;
int month;
int year;
int rating;
};
在下面的循环中,我试图打印出结构向量中的所有信息,而我只想在评分中进行小数,但这使得它使得第一次迭代之后的所有线路都有小数。
for(int i = 0; i < info.size(); i++) {
cout << info.at(i).week << endl;
cout << info.at(i).month << endl;
cout << info.at(i).year << endl;
cout << fixed << setprecision(2) << info.at(i).rating << endl;
}
有人知道我如何解决这个问题?
另外,如果我不使用指针并且没有分配动态内存,是否仍然有可能有内存泄漏?
它很乏味,但是您可以保存和还原格式标志
for(int i = 0; i < info.size(); i++) {
cout << info.at(i).week << endl;
cout << info.at(i).month << endl;
cout << info.at(i).year << endl;
ios_base::fmt_flags save = cout.flags();
cout << fixed << setprecision(2) << info.at(i).rating << endl;
cout.flags(save);
}
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 循环中的随机函数
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 循环中的setPrecision