C语言 stack STL应用

cjh94520 2013-03-30 12:46:22
#include <iostream>
#include <Stack>
using namespace std;
typedef struct
{
int x;
int y;
}element;
void main()
{
stack<double> s;
for( int i=0; i < 10; i++ )
s.push(i);
while(!s.empty())
{
printf("%lf\n",s.top());
s.pop();
}
cout << "This stack has a size of " << s.size() << endl;
}


这个代码是对的,我现在想将element作为栈的元素,应该怎样修改??
PS:我在stack<element>s这样修改不提示错。可是push(),pop()应该怎么写?
高手求教!!
...全文
111 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cjh94520 2013-03-30
  • 打赏
  • 举报
回复
如果我想在push里面直接填,应该怎样写?怎样输出pop()的结果
苹果皮 2013-03-30
  • 打赏
  • 举报
回复
double怎么用 element就怎么用
ri_aje 2013-03-30
  • 打赏
  • 举报
回复
这样写

#include <iostream>
#include <stack>

struct element_t
{
 int x;
 int y;
};
int main ()
{
 std::stack<element_t> s;
 for(int i = 0; i != 10; ++i)
 {
  s.push(element_t{i,i+1});
 }
 while (!s.empty())
 {
  element_t const& e = s.top();
  printf("%d %d\n",e.x,e.y);
  s.pop();
 }
 std::cout << "This stack has a size of " << s.size() << std::endl;
}
ps. 这是 c++ 程序,不是 c 程序。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧