我需要使用该Reverse方法反转堆栈的顺序。
但是我已经尝试过了,但是它不起作用:
Dim StackObject As New Stack(Of String)
StackObject.Push("S")
StackObject.Push("T")
StackObject.Push("A")
StackObject.Push("C")
StackObject.Push("K")
StackObject = StackObject.Reverse.Cast(Of String)() ' <-- InvalidCastException
For Each str As String In StackObject
MsgBox(str)
Next
Run Code Online (Sandbox Code Playgroud)
我编写了这个通用函数来反转堆栈,但是我想使用Reverse方法而不是所有这些不必要的代码来做到这一点:
Private Function Reverse_Stack(Of T)(stack As Stack(Of T))
Dim new_stack As New Stack(Of T)
While Not stack.Count = 0
new_stack.Push(stack.Pop)
End While
Return new_stack
End Function
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用链表实现堆栈类这里是我的stack.h
// File: Stack.h
#ifndef STACK_H
#define STACK_H
class Stack
{
private:
struct linklst{
int num;
int* next;
};
linklst* top;
public:
Stack();
~Stack();
void push(int i);
int pop();
bool isempty();
};
#endif
Run Code Online (Sandbox Code Playgroud)
和我的堆栈.cpp
// Stack.cpp
#include"Stack.h"
using namespace std;
Stack::Stack(){
top = new linklst();
top->num = -1;
top->next = nullptr;
};
Stack::~Stack() {
linklst * r = new linklst();
while (true)
{
r = top;
top = top->next;
delete r;
}
delete top;
};
void Stack::push(int i){
linklst * …Run Code Online (Sandbox Code Playgroud) 我一直在尝试打印一个字符串文字,但似乎我做错了,因为我收到了编译警告.这可能是由于错误的格式化或我对c_str()功能的误解,我认为应该返回一个string.
parser.y: In function ‘void setVal(int)’:
parser.y:617:41: warning: format not a string literal and no format arguments [-Wformat-security]
Run Code Online (Sandbox Code Playgroud)
617行:
sprintf(temp, constStack.top().c_str());
Run Code Online (Sandbox Code Playgroud)
有这些声明
#include <stack>
const int LENGTH = 15;
char *temp = new char[LENGTH];
stack<string> constStack;
Run Code Online (Sandbox Code Playgroud)
如何为字符串提供正确的格式?
所以,我有这个代码.
void main(void){
int n = 9999;
int *array = calloc(n, sizeof(int));
int i, j;
// Populate array up to N
for(i = 0; i < n; i++){
array[i] = 2 + i;
}
// Run sievs
sievs(array, n);
print_prime(array, n);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果N足够大,程序将核心转储.我的理论是在堆栈上分配了一些东西,并且它不足以容纳那么多数据,但是我使用的是calloc,所以它应该是heep.
这两个打印函数看起来像这样:
void print_prime(int *a, int n){
int i;
for (i = 0; i < n; i++){
if(a[i] != -1)
printf("Prime: %d \n", a[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
是否有任何此代码导致它?我不明白为什么.
这是siev功能:
void sievs(int *array, int n){
int i, j;
for(i = …Run Code Online (Sandbox Code Playgroud) 为什么这一小段代码在第6行和第10行(for循环)中给出了非法的类型错误启动....我找不到任何不匹配的大括号......
class StackDemo{
final int size = 10;
Stack s = new Stack(size);
//Push charecters into the stack
for(int i=0; i<size; i++){
s.push((char)'A'+i);
}
//pop the stack untill its empty
for(int i=0; i<size; i++){
System.out.println("Pooped element "+i+" is "+ s.pop());
}
}
Run Code Online (Sandbox Code Playgroud)
我实现了Stack类,
有人能回答我吗?
public class ReplaceString{
public static void main(String arg[]) {
String a = "Hariom";
a = a.replace('H', 'b');
System.out.println(a);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用.NETFramework,Version = v4.5并尝试创建以下堆栈:
using System;
using System.Collections;
using System.Collections.Generic;
...
Stack<int> s = new Stack<int>();
Run Code Online (Sandbox Code Playgroud)
为什么我收到此错误?
类'System.Collections.Stack'没有类型参数
没有类型参数的堆栈工作正常.
我正在实现堆栈引用.但是我收到了"Segmentation fault(core dumped)"的错误.我在Ubuntu 14.04上使用g ++(Ubuntu 4.8.2-19ubuntu1)4.8.2.非常感谢.
代码如下所示.
#include<iostream>
#include<stack>
using namespace std;
int main() {
stack<int>* S;
S->push(4);
return 0;
}
Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
struct number
{
int value;
int pos;
public:
number(int a,int b)
{
value=a;
pos=b;
}
};
int main() {
// your code goes here
number(1,2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,在哪里分配内存number(1,2)?在堆或堆栈上?我们如何验证它?
我想知道确定程序使用多少堆栈空间的最佳方法是什么,是否有任何技术或工具来生成统计信息,而不是手动计数?
该程序希望分析是代码编写器中的C程序,如果这有所不同.
谢谢