本文整理汇总了C++中Stack类的典型用法代码示例。如果您正苦于以下问题:C++ Stack类的具体用法?C++ Stack怎么用?C++ Stack使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Stack类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testStack
void testStack() {
Stack<int> mycontainer;
cout << "\n\n Begin test function for the Stack<T> class\n";
// Testing the push function
cout << "Testing size of new empty container: " << mycontainer.length() << endl;
mycontainer.push(1);
cout << "Testing push(1), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "Size is " << mycontainer.length() << endl;
mycontainer.push(2);
cout << "Testing push(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "Size is " << mycontainer.length() << endl;
mycontainer.push(2);
cout << "Testing push(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "Size is " << mycontainer.length() << endl;
mycontainer.push(2);
cout << "Testing push(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "Size is " << mycontainer.length() << endl;
mycontainer.push(2);
cout << "Testing push(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "Size is " << mycontainer.length() << endl;
mycontainer.push(2);
cout << "Testing push(2), length(), and isEmpty() functions. mycontainer is empty? "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "Size is " << mycontainer.length() << endl << endl;
int size = mycontainer.length();
cout << "Testing pop(), top(), length() and isEmpty() functions \n"
<< "in a for loop with iterations greater than container size.";
for (int i = 0; i < size + 1; i++) {
cout << "\nIteration: " << i + 1 << "\n";
if (!mycontainer.isEmpty()) {
cout << " mycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "Stack size before pop is " << mycontainer.length() << endl;
cout << "Top of container is " << mycontainer.top() << endl;
mycontainer.pop();
} else {
cout << "The Stack is empty.\n";
}
cout << "Stack size is now: " << mycontainer.length() << endl;
}
cout << "\nFinished for loop\n";
cout << "\nTesting the reference for top() function.\n";
cout << "Start with int test = 7. mycontainer.push(test)\n";
int test = 7;
mycontainer.push(test);
cout << "Testing with test = 8. test=mycontainer.top(). mycontainer.top() = 13 \n";
test = 8;
test = mycontainer.top();
mycontainer.top() = 13;
cout << "Test is now " << test << " front of container is " << mycontainer.top() << "\n";
test = 11;
mycontainer.push(test);
cout << "Test is now " << test << " top of container is " << mycontainer.top() << "\n";
mycontainer.top() = test;
cout << "Test is now " << test << " top of container is " << mycontainer.top() << "\n";
cout << "\nmycontainer size is " << mycontainer.length() << endl;
cout << "\nTesting the clear() function: \n";
mycontainer.clear();
cout << "mycontainer size now is " << mycontainer.length()
<< " mycontainer is empty: "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
cout << "\nTesting assignment operator: container2 = mycontainer\n";
cout << "Filling mycontainer with ints starting at 42\n";
size = 5;
// Fill mycontainer with ints to test copy constructor
for (int i = 0; i < size; i++) {
mycontainer.push(i + 41);
}
cout << "mycontainer size now is " << mycontainer.length()
<< " mycontainer is empty: "
<< (mycontainer.isEmpty() ? " true\n" : "false\n");
Stack<int> container2;
container2 = mycontainer;
cout << "mycontainer size is: " << mycontainer.length() << endl;
cout << "container2 size now is " << container2.length()
<< " container is empty: "
<< (container2.isEmpty() ? " true\n" : "false\n");
cout << "Testing the contents of container2 and mycontainer using pop() functions:\n";
//.........这里部分代码省略.........
示例2: TEST
TEST(Stack, isnt_full_stack_IsFull_0)
{
Stack<int> *S = new Stack<int>;
S->Push(1);
EXPECT_EQ(0, S->IsFull());
}
示例3: while
//---------------------------------------------------------
// Compute x-coordinates (LP-based approach) (for cluster graphs)
//---------------------------------------------------------
void OptimalHierarchyClusterLayout::computeXCoordinates(
const ExtendedNestingGraph& H,
ClusterGraphCopyAttributes &AGC)
{
const ClusterGraphCopy &CG = H.getClusterGraph();
const int k = H.numberOfLayers();
//
// preprocessing: determine nodes that are considered as virtual
//
m_isVirtual.init(H);
int i;
for(i = 0; i < k; ++i)
{
int last = -1;
// Process nodes on layer i from left to right
Stack<const LHTreeNode*> S;
S.push(H.layerHierarchyTree(i));
while(!S.empty())
{
const LHTreeNode *vNode = S.pop();
if(vNode->isCompound()) {
for(int j = vNode->numberOfChildren()-1; j >= 0; --j)
S.push(vNode->child(j));
} else {
node v = vNode->getNode();
if(H.isLongEdgeDummy(v) == true) {
m_isVirtual[v] = true;
edge e = v->firstAdj()->theEdge();
if(e->target() == v)
e = v->lastAdj()->theEdge();
node u = e->target();
if(H.verticalSegment(e) == false)
continue;
if(H.isLongEdgeDummy(u) == true) {
int down = H.pos(u);
if(last != -1 && last > down) {
m_isVirtual[v] = false;
} else {
last = down;
}
}
} else {
m_isVirtual[v] = false;
}
}
}
}
//
// determine variables of LP
//
int nSegments = 0; // number of vertical segments
int nRealVertices = 0; // number of real vertices
int nEdges = 0; // number of edges not in vertical segments
int nBalanced = 0; // number of real vertices with deg > 1 for which balancing constraints may be applied
m_vIndex.init(H,-1); // for real node: index of x[v] for dummy: index of corresponding segment
NodeArray<int> bIndex(H,-1); // (relative) index of b[v]
EdgeArray<int> eIndex(H,-1); // for edge not in vertical segment: its index
Array<int> count(H.numberOfEdges()); // counts the number of dummy vertices
// in corresponding segment that are not at
// position 0
int nSpacingConstraints = 0;
for(i = 0; i < k; ++i)
{
Stack<const LHTreeNode*> S;
S.push(H.layerHierarchyTree(i));
while(!S.empty())
{
const LHTreeNode *vNode = S.pop();
if(vNode->isCompound()) {
cluster c = vNode->originalCluster();
if(H.isVirtual(c) == false)
nSpacingConstraints += (c == CG.rootCluster()) ? 1 : 2;
for(int j = vNode->numberOfChildren()-1; j >= 0; --j)
S.push(vNode->child(j));
} else {
node v = vNode->getNode();
// ignore dummy nodes and nodes representing cluster
// (top or bottom) border
if(H.type(v) == ExtendedNestingGraph::ntClusterBottom || H.type(v) == ExtendedNestingGraph::ntClusterTop)
//.........这里部分代码省略.........
示例4: main
/**********************************************************************
* MAIN: Simple driver program to exercise our Stack data type
***********************************************************************/
int main()
{
//
// Integer Stack
//
cout << "#### Integers ####\n";
Stack <int> stack;
int value;
// add three to the stack
for (int i = 0; i < 3; i++)
{
cout << "push: ";
cin >> value;
stack.push(value);
}
// remove one just for kicks
stack.pop(value);
cout << "\tpop: " << value << endl;
// add three more
for (int i = 0; i < 3; i++)
{
cout << "push: ";
cin >> value;
stack.push(value);
}
// remove them all
while (stack.pop(value))
cout << "\tpop: " << value << endl;
assert(stack.isEmpty());
//
// String Stack
//
cout << "#### Strings ####\n";
Stack <string> stringStack;
string stringValue;
// add three to the stack
for (int i = 0; i < 3; i++)
{
cout << "push: ";
cin >> stringValue;
stringStack.push(stringValue);
}
// remove one just for kicks
stringStack.pop(stringValue);
cout << "\tpop: " << stringValue << endl;
// add three more
for (int i = 0; i < 3; i++)
{
cout << "push: ";
cin >> stringValue;
stringStack.push(stringValue);
}
// remove them all
while (stringStack.pop(stringValue))
cout << "\tpop: " << stringValue << endl;
assert(stringStack.isEmpty());
return 0;
}
示例5: main
int main()
{
Stack<double, 30> d;
Stack<char , 20> s;
s.push('+');
s.push('-');
s.push('*');
s.push('/');
cout << typeid(s).name() << endl;
cout << typeid(d).name() << endl;
while(!s.empty())
cout << s.pop() << endl;
d.push(100);
d.push(60);
d.push(234.34);d.push(324.234);
while(!d.empty())
cout << d.pop() << endl;
}
示例6: EXECUTE
bool EXECUTE(int ret_inst)
{
cout << "\n\t At any time during the exection of the program, the following keywords can be used\n";
cout << "\t'rc' : To change the current register contents\n";
cout << "\t'rd' : To display the current register contents\n";
cout << "\t'md' : To display the current memory and register contents\n";
cout << "\t'mc' : To change the current execution mode\n";
cout << "\t'rc' : To change the current break point state \n";
cout << "\t'fp' : To print all the current flag contents\n";
string tmp = "" , mode = "";
char input[100];input[0]=0;
cout << "\n Set the Execution Mode by pressing one of the following :\n";
cout << "\t'm' : execute Microinstruction by Microinstruction\n";
cout << "\t'i' : execute Instruction by Instruction\n";
cout << "\t'p' : execute entire program (default mode)\n";
strcpy(input,"p");
while (true)
{
cout << "\tExecution Mode : ";
getchar();
scanf("%[^\n]",input);
mode = input ;
if (mode != "p" && mode !="m" && mode!="i")
{
cout << "Invalid Entry . Press (m/i/p) .\n";
continue ;
}
break;
}
cout << "\tPress 'bp' to set additional break-points besides (brk) in the program : ";
getchar();
scanf("%[^\n]",input);
tmp = input ;
bool be = false ;
if (tmp == "bp")
{
if (SetBreakPoints() == false )
return false ;
}
cout << "\tPress be/bd to enable/disable all break-points (default disable)\n";
string Microinstruction="";
int count_inst = -1; // Counter to count the number of instructions executed
cout << "\n\tProgram Execution Begins .......\n\n";
while ( TRUE )
{
/*
This is the main loop. All modules are executed in this order. NOTICE
*/
ret_inst = MS.Sequencer(ret_inst , DC , FL);
if (ret_inst == 0)
count_inst++;
Microinstruction = MPM.Fetch(ret_inst);
PC.Operation(Microinstruction , DB); //Program Counter Module
MR.Operation(Microinstruction,DB); //Memory Address Module
Mem.Operation(Microinstruction,DB,MR); //Memory Address Module
DC.Operation(Microinstruction,DB); //Decoder Module
MS.Operation(Microinstruction,DC); //MicroSequencer Module
IOR.Operation(Microinstruction,DB,RG,FL); //Instr. Oper. Register Module
RG.Operation(Microinstruction,DB,PC,SP,AC,OP,ALU1); //Register File Module
OP.Operation(Microinstruction,DB); //Operand Module
AC.S_Operation(Microinstruction,DB); // Special Accumulator Operation (only to EAR)
ALU1.Operation(Microinstruction,DB,OP,FL); //ALU Module
AC.Operation(Microinstruction,DB,ALU1); //Accumulator Module
SP.Operation(Microinstruction,DB); //Stack Module
MR.Operation(Microinstruction,DB); //Memory Address Module
Mem.Operation(Microinstruction,DB,MR); //Memory Address Module
RG.Operation(Microinstruction,DB,PC,SP,AC,OP,ALU1); //Register File Module
OP.Operation(Microinstruction,DB); //Operand Module
PC.S_Operation(Microinstruction, DB); // Special PC Operation
// FL.Operation(Microinstruction); //Flag Register Module (only to LPC)
if (Microinstruction == "000000000000000000001000000000000000000000")
break;
clocks++;
if (Print(mode,count_inst,ret_inst,Microinstruction , be) == false)
return false;
}
return true;
}
示例7: main
int main()
{
// Testing List
List<std::string> list;
std::cout << "Is the list empty? = " << list.empty() << std::endl;
std::string line;
std::ifstream myfile ("datain.dat");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
list.push_back(line);
}
myfile.close();
}
list.sort();
std::ofstream outfile("output.txt");
outfile << "List in ascending output (strings): " << std::endl << std::endl;
if (outfile.is_open())
{
for (List<std::string>::iterator it = list.begin(); it != list.end(); ++it)
outfile << *it << std::endl;
}
list.sort("desc");
std::ofstream desc_outfile("output2.txt");
desc_outfile << "List in descending output (strings): " << std::endl << std::endl;
if (desc_outfile.is_open())
{
for (List<std::string>::iterator it = list.begin(); it != list.end(); ++it)
desc_outfile << *it << std::endl;
}
std::cout << "pop back is " << list.pop_back() << std::endl;
list.remove(list.begin() + 2);
List<std::string>::iterator iters = list.begin();
std::cout << " get next is " << iters.get_next() << std::endl;
list.push_back(*iters);
std::cout << "size is " << list.size() << std::endl;
List<std::string> list2 = list;
for (List<std::string>::iterator iter = list2.begin();
iter != list2.end(); ++iter)
{
std::cout << (*iter) << std::endl;
}
std::cout << "search result " << list.search("5") << std::endl;
std::cout << "get_head() " << list.get_head() << std::endl;
List<std::string>::iterator it = list.begin() + 1;
std::cout << "has_next() " << it.has_next() << std::endl;
// Stack testing
Stack<std::string> stack;
std::cout << "stack.empty() (should be 1) " << stack.empty() << std::endl;
std::ifstream my_stack_file ("datain.dat");
if (my_stack_file.is_open())
{
while ( my_stack_file.good() )
{
getline (my_stack_file,line);
stack.push(line);
}
my_stack_file.close();
}
stack.pop();
std::cout << "stack top is " << stack.show_top() << std::endl;
std::cout << "stack.empty() " << stack.empty() << std::endl;
std::cout << "stack.is_full() " << stack.is_full() << std::endl;
std::ofstream stack_out("stack_output.txt");
stack_out << "Popping stack output ( minus 1): " << std::endl << std::endl;
while (!stack.empty())
stack_out << stack.pop() << std::endl;
// Testing Queue
Queue<std::string> queue;
std::cout << "queue.empty() (should be 1) " << queue.empty() << std::endl;
std::ifstream my_queue_file ("datain.dat");
if (my_queue_file.is_open())
{
while ( my_queue_file.good() )
{
getline (my_queue_file,line);
queue.enqueue(line);
}
my_queue_file.close();
}
std::cout << "searching queue for '5': " << queue.search("5") << std::endl;
std::cout << "queue.empty() (should be 0) " << queue.empty() << std::endl;
std::ofstream queue_out("queue_output.txt");
queue.sort();
queue_out << "Dequeue queue output sorted ascending: " << std::endl << std::endl;
while (!queue.empty())
queue_out << queue.dequeue() << std::endl;
if (my_queue_file.is_open())
{
while ( my_queue_file.good() )
{
//.........这里部分代码省略.........
示例8: remove
void Level::remove(Vector target) {
Stack<GameObject*>* stack = this->tiles[target.y][target.x];
if (stack) {
stack->pop();
}
}
示例9: main
int main(/*int argc, char *argv[]*/)
{
Stack<string> s;
#ifdef DEBUG
assert(s.size() == 0);
#endif
try{
s.pop();
} catch(string e){
cout << e << endl;
}
s.push("Hello11");
s.push("Hello22");
s.push("Hello33");
#ifdef DEBUG
assert(s.size() == 3);
#endif
string str = s.getElement();
s.pop();
#ifdef DEBUG
assert(s.size() == 2);
#endif
cout << "String: " << str << endl;
s.clear();
#ifdef DEBUG
assert(s.size() == 0);
#endif
return 0;
}
示例10: charToObject
void Level::charToObject(int i, int j, char c, bool empty) {
if (c == ' ') {
return;
}
Stack<GameObject*>* stack;
if (this->tiles[i][j] == NULL) {
stack = new Stack<GameObject*>();
this->tiles[i][j] = stack;
}
else {
stack = this->tiles[i][j];
}
GameObject* gobj = NULL;
if (!empty) {
switch(c) {
case Global::VWallSymbol:
case Global::HWallSymbol:
case Global::FloorSymbol:
case Global::PassageSymbol:
case Global::DoorSymbol:
break;
// Potions!
case Global::RHPotionSymbol:
gobj = this->game->addObject(RHPotionKind);
break;
case Global::BAPotionSymbol:
gobj = this->game->addObject(BAPotionKind);
break;
case Global::BDPotionSymbol:
gobj = this->game->addObject(BDPotionKind);
break;
case Global::PHPotionSymbol:
gobj = this->game->addObject(PHPotionKind);
break;
case Global::WAPotionSymbol:
gobj = this->game->addObject(WAPotionKind);
break;
case Global::WDPotionSymbol:
gobj = this->game->addObject(WDPotionKind);
break;
// Gold!
case Global::GoldSmallSymbol:
gobj = this->game->addObject(GoldSmallKind);
break;
case Global::GoldNormalSymbol:
gobj = this->game->addObject(GoldNormalKind);
break;
case Global::GoldMerchantSymbol:
gobj = this->game->addObject(GoldMerchantKind);
break;
case Global::GoldDragonSymbol:
gobj = this->game->addObject(GoldDragonKind);
break;
case Global::HumanSymbol:
gobj = this->game->addObject(HumanKind);
break;
case Global::DwarfSymbol:
gobj = this->game->addObject(DwarfKind);
break;
case Global::ElfSymbol:
gobj = this->game->addObject(ElfKind);
break;
case Global::OrcSymbol:
gobj = this->game->addObject(OrcKind);
break;
case Global::MerchantSymbol:
gobj = this->game->addObject(MerchantKind);
break;
case Global::HalflingSymbol:
gobj = this->game->addObject(HalflingKind);
break;
case Global::DragonSymbol:
gobj = this->game->addObject(DragonKind);
break;
case Global::StairsSymbol:
gobj = this->game->addObject(StairsKind);
this->stairs = gobj;
break;
case Global::PlayerSymbol:
this->spawn.x = j;
this->spawn.y = i;
break;
default:
gobj = this->game->addObject(DoorKind);
break;
}
}
else {
switch(c) {
case Global::VWallSymbol:
gobj = this->game->addObject(VWallKind);
//.........这里部分代码省略.........
示例11: add_queue
void add_queue(Stack<Queue<temp_automaton>>& s) {
Queue<temp_automaton> q;
temp_automaton a;
q.push(a);
s.push(q);
}
示例12: contextPtr
void
ThreadStackHelper::GetNativeStack(Stack& aStack)
{
#ifdef MOZ_THREADSTACKHELPER_NATIVE
ThreadContext context;
context.mStack = MakeUnique<uint8_t[]>(ThreadContext::kMaxStackSize);
ScopedSetPtr<ThreadContext> contextPtr(mContextToFill, &context);
// Get pseudostack first and fill the thread context.
GetStack(aStack);
NS_ENSURE_TRUE_VOID(context.mValid);
CodeModulesProvider modulesProvider;
google_breakpad::BasicCodeModules modules(&modulesProvider);
google_breakpad::BasicSourceLineResolver resolver;
google_breakpad::StackFrameSymbolizer symbolizer(nullptr, &resolver);
#if defined(MOZ_THREADSTACKHELPER_X86)
google_breakpad::StackwalkerX86 stackWalker(
nullptr, &context.mContext, &context, &modules, &symbolizer);
#elif defined(MOZ_THREADSTACKHELPER_X64)
google_breakpad::StackwalkerAMD64 stackWalker(
nullptr, &context.mContext, &context, &modules, &symbolizer);
#elif defined(MOZ_THREADSTACKHELPER_ARM)
google_breakpad::StackwalkerARM stackWalker(
nullptr, &context.mContext, -1, &context, &modules, &symbolizer);
#else
#error "Unsupported architecture"
#endif
google_breakpad::CallStack callStack;
std::vector<const google_breakpad::CodeModule*> modules_without_symbols;
google_breakpad::Stackwalker::set_max_frames(ThreadContext::kMaxStackFrames);
google_breakpad::Stackwalker::
set_max_frames_scanned(ThreadContext::kMaxStackFrames);
NS_ENSURE_TRUE_VOID(stackWalker.Walk(&callStack, &modules_without_symbols));
const std::vector<google_breakpad::StackFrame*>& frames(*callStack.frames());
for (intptr_t i = frames.size() - 1; i >= 0; i--) {
const google_breakpad::StackFrame& frame = *frames[i];
if (!frame.module) {
continue;
}
const string& module = frame.module->code_file();
#if defined(XP_LINUX) || defined(XP_MACOSX)
const char PATH_SEP = '/';
#elif defined(XP_WIN)
const char PATH_SEP = '\\';
#endif
const char* const module_basename = strrchr(module.c_str(), PATH_SEP);
const char* const module_name = module_basename ?
module_basename + 1 : module.c_str();
char buffer[0x100];
size_t len = 0;
if (!frame.function_name.empty()) {
len = PR_snprintf(buffer, sizeof(buffer), "%s:%s",
module_name, frame.function_name.c_str());
} else {
len = PR_snprintf(buffer, sizeof(buffer), "%s:0x%p",
module_name, (intptr_t)
(frame.instruction - frame.module->base_address()));
}
if (len) {
aStack.AppendViaBuffer(buffer, len);
}
}
#endif // MOZ_THREADSTACKHELPER_NATIVE
}
示例13: izraz
void izraz(char a[1000])
{
int br=0;
char c[1200];
Stack<char> b;
Stack<char> d;
/// pravi izraz samo ot znacite
for(int i=0;i<1000;i++)
{
if(a[i]<='0'||a[i]>='9')
{
b.push(a[i]);
}
}
while(!b.isempty())
{
if(b.top()=='(')
{
d.push(b.pop());
}
if(b.top()=='-'|| b.top()=='+')
{
d.push(b.pop());
if(b.top()=='*'|| b.top()=='/')
{
d.push(')');
d.push(b.pop());
}
else d.push(b.pop());
}
}
///dobavq 4islata kum znacite
int i;
while(a[i]<='0'||a[i]>='9')
{
c[i]=a[i];
d.pop();
}
int j=i;
for( i=0;i<1000;i++)
{
if(a[i]>='0'||a[i]<='9')
{
c[j]=a[i];
c[j+1]=d.pop();
if((a[i+2]<='0'||a[i+2]>='9')&&(i+3<=1000))
{
c[j+2]=d.pop();
i=i+3;
if(d.top()=')')
{c[j+3]=d.pop();
j=j+4;
}
else j=j+3;
}
}
}
for(int i=0;i<1200;i++)
{
cout<<c[i];
}
}
示例14: empty
bool empty() const { return in.empty() and out.empty(); }
示例15: testSeq
void testSeq(int *A, int size){
for (int i=0; i<size; i++) S.push(A[i]);
qDebug() << "After " << size << " push operations, size:" << S.getSize();
}