online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include <iostream> #include <stdexcept> #include <vector> #include <algorithm> using namespace std; template <typename T> class Stack { public: Stack() : root (nullptr) {} //По умолчанию стек пуст void push(const T& x) //добавление в стек { Node* newnode = new Node; newnode->data = x; newnode->next = root; root = newnode; } bool empty() const //проверка на пустоту { return root == nullptr; } const T& top()const //вершина стека { if (empty()) { throw length_error("stack is empty"); } return root->data; } T pop() //void pop() ??? удаление из стека { if (empty()) { throw length_error("stack is empty"); } Node * delnode = root; T x = delnode->data; root = delnode->next; delete delnode; return x; } /*void exchange(int idx1, int idx2) //st[idx1] <--> st[idx2] { Node *ptr1, *ptr2; //установить указатели на элементы c индексами idx1 и idx2, соответственно swap(ptr1->data, ptr2->data); }*/ ~Stack() //деструктор { while (!empty()) { pop(); //некоторая проблема - из-за неоптимальной реализации pop прозводится лишнее копирование } } private: struct Node //вспомогательная структура - узел стека { T data; //Поле с данными Node * next; //Указатель на следующий }; Node* root; //вершина стека }; int main() { Stack<int> st; for (int i=1;i<=10;i++) //добавляем 1,2,...,10 { st.push(i); } //Stack<int> st2 = st; -- если раскомментировать, будет большая большая проблема!!! //Нужно реализовать явно конструктор копирования, а также operator= while (!st.empty()) //печатаем все что в стеке { cout <<st.pop() <<" "; } cout <<endl; /*while (!st2.empty()) //печатаем все что в стеке { cout <<st2.pop() <<" "; }*/ vector<Stack<int>> vec(5); swap(vec[1],vec[3]); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue