Search code examples
c++stringqtstackgeneric-programming

Create a stack for various types using Qt


I need to simulate the stack using Qt, as elements which can be used for the int and string. I don't need your code, but i have literally no idea how to do it. I will be grateful for any tips.


Solution

  • You can use a QStack<QVariant> to achieve the result you want.

    #include <QtCore/QStack>
    #include <QtCore/QVariant>
    
    int main( int argc, char* argv[] )
    {
       QStack<QVariant> stack;
    
       stack.push_back( 1 );
       stack.push_back( "two" );
    
       std::cout << stack.pop().toInt() << " " 
                 << stack.pop().toString().toStdString() << std::endl; 
    }
    

    which gives

    1 two