1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

array elements in a stack.

Discussion in 'C++' started by aravind rao, Sep 12, 2010.

  1. aravind rao

    aravind rao New Member

    Joined:
    Sep 12, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi

    I am developing a program that uses Breadth First Search to map a grid(2d array) out. Breadth First Search involves storing nodes in a stack and then exploring these nodes based on Breadth or level. I know how to create a vector array which I shall use as a stack. I need to know how to store array elements in the stack using the push_back()method. This is what I have so far..

    vectorstack; //stack initialization.
    stack.push_back(array[1][1]) //store array element itself, not Value held in array!

    The value stored is not the array element, but the value which happens to be some integer x.

    Thanks for helping.
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    I got you point..First make a 2D vector like below:-
    Code:
    vector< vector<int>> stack
    
    Now, suppose a array with indexes 'i' and 'j'....i.e. array[j] value is to be stored in 'stack' we first create a 1D vector and store the indexes to that array like below:-
    Code:
    vector<int> p;
    p.push_back(i);
    p.push_back(j);
    
    Now, at the final step you store the 1D vector in 2D 'Stack' vector..like below:-
    Code:
    stack.push_back(temp);
    
    I hope it will help u...
     

Share This Page