How to verify if an item is in a stack and then pass it to a queue in c ++ using standard libraries (stl)

0

Good morning. I have a doubt with a c ++ code, in which I use stacks and tails, basically I have to build a stack and a dictionary, both of characters, for each character belonging to the stack to verify if it is in the dictionary, and if the answer is positive the character must be entered in a queue (initially empty), otherwise it must be discarded.

This is what I have:

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

int main ()
{
    stack <string> pila; 
    pila.push("Palabra1"); 
    pila.push("Palabra2");  
    pila.push("Palabra3");
    pila.push("Palabra4");
    cout << "Hay en total " << pila.size () << " palabras en el diccionario" << endl;
    cout<<endl;
    cout << "Palabras en el diccionario: ";
    while(!pila.empty()) {
        cout <<endl<< pila.top() << endl;
        pila.pop();
    }    

    string diccionario[] = {"Palabra1", "Palabra2", "Palabra3", "Palabra4"};

    cout<<"Verificando si las palabras de la pila se encuentran en el diccionario"<<endl;
    cout<<"Opciones: Palabra1, Palabra2, Palabra3, Palabra4"<<endl;
    cout<<endl;

    //Aqui deberia hacer la comparacion entre la pila y el diccionario para luego agregar los valores a la cola

    queue <string> cola;     
    //estas serian las sentencias si se encontrara la palbra en el diccionario
    cout<<"La palabra introducida se encuentra en el diccionario"<<endl;
    cout<<"Se procedera a introducirla en la cola"<<endl;

    cola.push("Palabra1"); } else {
    cout<<"La palabra no se encuentra en el diccionario por lo tanto no se agregara a la cola"<<endl;}

    while(!cola.empty()) {
    cout <<endl<< cola.front() << endl;
    cola.pop(); }    

    system("pause");
}

My doubt would be like comparing the stack and the "dictionary" that I do not know if it would be useful to create it as an array or as another stack or queue, since they do not specify if the dictionary should be a stack, queue or array. Thank you very much for your answers

    
asked by Grecia V. P. Valero 15.03.2017 в 04:43
source

1 answer

2
  

I have a question with a c ++ code, in which I use stacks and tails.

First of all, you should ask yourself if you are using the right containers for the tasks you want to do; You can take a look at this question to get an idea.

A stack is characterized by maintaining the order in which the data are inserted and the order in which they are extracted, also depending on the order of insertion (being able to be FIFO or LIFO ); If your application is indifferent to the order of insertion and extraction, surely a pile is not the container you should choose.

As for the dictionary, this is a associative container (that associates key and value) that generally does not allow repetitions. You are using an array called diccionario but as the habit does not make the monk your arrangement is not a dictionary.

  

For each character belonging to the stack, check if it is in the dictionary.

I assume you mean for each element of the stack, not for each character.

To find the elements of the stack that are in the dictionary you should compare each element of the stack with each element of the dictionary. Unfortunately, a stack is not meant to be sequentially traversed if not to add ( push ) and remove ( pop ) items in a certain order, that's why a stack ( stack ) does not offer any function or utility to traverse its elements (and for this reason, for show the elements that your stack contains, in your code you must delete the elements while the samples).

So let's replace your stack ( stack ) with a list ( list ) and your fix for another list, since to be a dictionary we should know the key (and you have not specified no key), with these other containers it is possible to go through the contained data and check if they exist in one or another container, for this it helped me with the function std::count in the header <algorithm> :

int main()
{
    std::list <std::string> pila;
    pila.push_back("Palabra1");
    pila.push_back("Palabra2");
    pila.push_back("Palabra3");
    pila.push_back("Palabra4");
    std::cout << "Hay en total " << pila.size() << " palabras en el diccionario\n\n";

    std::cout << "Palabras en la pila: ";
    for (const auto &palabra: pila) std::cout << '\n' << palabra << '\n';

    std::list <std::string> diccionario = {"Palabra1", "Palabra2", "Palabra3", "Palabra4"};

    for (const auto &palabra: pila)
    {
        std::cout << "La entrada " << palabra << " esta en el diccionario?: "
                  << ((std::count(diccionario.begin(), diccionario.end(), palabra) != 0) ? "Si" : "No") << '\n';
    }

    return 0;
}
    
answered by 15.03.2017 в 09:23