- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 C++ STL 库中的优先级队列,我想一直将元素插入队列,直到用户按下 Enter。
我试过使用 getchar() 来获取元素的输入,但它在队列中被保存为一系列 1。
priority_queue<int> v1;
priority_queue<int, vector<int>, greater<int> > v2;
cout<<"Enter the elements of the queue. Press enter to stop.\n";
while(a = getchar()!= '\n')
{
v1.push(a);
v2.push(a);
}
while(!v1.empty())
{
cout<<v1.top()<<" ";
v1.pop();
}
我希望输出是输入元素的最小和最大堆,但它给我的只是一系列 1
最佳答案
while(a = getchar()!= '\n')
it gives me is a series of 1's
warning with the priority of operators, you want
while((a = getchar())!= '\n')
目前你确实喜欢
while(a = (getchar()!= '\n'))
虽然输入字符不是换行符,但测试为真,所以 a 设置为 1
考虑到您下面的评论,如果该行为空或仅包含换行符或者如果该行不包含文字 int 则您想要读取行然后停止,否则提取 < em>int push 它
例如:
#include <iostream>
#include <queue>
#include <string>
#include <sstream>
using namespace std;
int main()
{
priority_queue<int> v1;
priority_queue<int, vector<int>, greater<int> > v2;
cout<<"Enter the elements of the queue. Press enter to stop.\n";
int a;
string s;
while (getline(cin, s) && (istringstream(s) >> a))
{
v1.push(a);
v2.push(a);
}
while(!v1.empty())
{
cout<<v1.top()<<" ";
v1.pop();
}
cout << endl;
}
编译和执行:
pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra c.cc
pi@raspberrypi:/tmp $ ./a.out
Enter the elements of the queue. Press enter to stop.
12
32
32 12
pi@raspberrypi:/tmp $
关于c++ - 如何从用户那里获取整数输入,直到在 C++ 中遇到输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56792148/
我需要在 JavaScript 中的笛卡尔坐标和球坐标之间进行转换。我在论坛上浏览了一下,没有找到我要找的东西。 现在我有这个: this.rho = sqrt((x*x) + (y*y) + (z*
有没有matrix3d可以像这样把矩形变成梯形的?我知道常规的 2d 矩阵变换只能以平行四边形结束,因为您只能有效地倾斜和旋转。 div { width: 300px; height:
关于这个例子(d3.j radial tree node links different sizes),我想知道是否可以在 d3.js 中混合径向树和直线树。 对于我的 jsFiddle 示例:htt
我尽量把标题写得最好,但我不确定如何准确描述这里发生的事情,所以请随时更正。 我想使用 › 直 Angular 引号 (›) 而不是 > 直 Angular 引号 (>),虽然 › 字符比 > 短,但
我正在尝试使用 CSS 创建一个具有圆边的矩形棱柱,如下图所示。 到目前为止,我已经指定了顶部和底部的边界半径。问题是我不知道如何让另一边的左右边缘向内 curl 。因此,拐 Angular 处不应有
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 去年关闭。 社区去年审查了是否重
首先,我刚刚开始学习 HTML 和 CSS。 我想如何使用这段代码: https://codepen.io/martinjkelly/pen/vEOBvL .container { width:
我是一名优秀的程序员,十分优秀!