Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to access an index of an std::stack

  void PDA::parse(vector<string> words){
    for(int i=0; i<words.size();i++){//for each string in the input file
    string token=words[i];
    for(int j=0; j<token.length(); j++) //for each character in the string
      {
        char input=token[j];
        char matchingBracket=getMatchingBracket(input); //returns the matching bracket, should probably just have ( and [

        if(!stack[j]){//since j-1 when the index is 0 will cause an error
          if(stack[j-1]==matchingBracket){
            stack.pop();
          }else{
            stack.push(input);
          }

        }
  }
    accepted()?cout<<"The string "<<words[i]<<" is balanced and was accepted"<<endl : cout<<"The string "<<words[i]<<" is not balanced and was not accepted"<<endl;
}
}

I'm getting these errors

PDA.cpp:25: error: no match for âoperator[]â in â((PDA*)this)->PDA::stack[j]â
PDA.cpp:26: error: no match for âoperator[]â in â((PDA*)this)->PDA::stack[(j - 1)]â

for these lines

if(!stack[j]){//since j-1 when the index is 0 will cause an error
              if(stack[j-1]==matchingBracket){

I looked up std::stack and found out that "By default, if no container class is specified for a particular stack class, the standard container class template deque is used." When I looked up deque I found out it supports operator[]. This is how I declared my stack. In the corresponding header file to this source file.

#ifndef PDA_H
#define PDA_H
#include <stack>
#include <vector>
#include <deque>
class PDA{
 private:
  std::stack<char> stack;
 public:
  PDA();
  bool isEmpty();
  void parse(std::vector<std::string>);
  char getMatchingBracket(char);
  bool accepted();
};
#endif

As I see it, using operator[] on a std::stack should work just fine. Any ideas?

like image 213
Tyler Pfaff Avatar asked Nov 17 '12 07:11

Tyler Pfaff


People also ask

How do you access an element in a stack?

top() is used to access the element at the top of the stack container. In a stack, the top element is the element that is inserted at the last or most recently inserted element.

Can you search a stack?

Stack.search(Object element) method in Java is used to search for an element in the stack and get its distance from the top. This method starts the count of the position from 1 and not from 0. The element that is on the top of the stack is considered to be at position 1.

Do stacks have index?

"Stack" is an abstract concept, not something that exists in reality. In the real world, they are generally implemented as data in consecutive memory addresses, and so indexing them is certainly possible, depending on the language/library/API you're using.

What is STD stack?

The std::stack class is a container adaptor that gives the programmer the functionality of a stack - specifically, a LIFO (last-in, first-out) data structure. The class template acts as a wrapper to the underlying container - only a specific set of functions is provided.


3 Answers

std::stack doesn't inherit from the underlying container type, it adapts it to a completely new interface. The underlying container is not exposed. That's essentially the point of the adaptors std::stack and std::queue: they ensure that you're using a more limited interface that will be the same regardless of the underlying structures.

That said, you can inherit from std::stack and access the underlying container from a subclass. It is a protected member named c.

class my_stack : public std::stack< char > {
public:
    using std::stack<char>::c; // expose the container
};

int main() {
    my_stack blah;
    blah.push( 'a' );
    blah.push( 'b' );
    std::cout << blah.c[ 1 ]; 
}

http://ideone.com/2LHlC7

like image 133
Potatoswatter Avatar answered Oct 05 '22 23:10

Potatoswatter


You should use the .top() method to check what's on top of the stack, not indexing.


Thus, instead of your current code …

if(!stack[j]){//since j-1 when the index is 0 will cause an error
  if(stack[j-1]==matchingBracket){
    stack.pop();
  }else{
    stack.push(input);
  }
}

write

if(!stack.empty() && stack.top() == matchingBracket) {
    stack.pop();
} else {
    stack.push(input);
}
like image 24
Cheers and hth. - Alf Avatar answered Oct 05 '22 22:10

Cheers and hth. - Alf


Stack doesn't support random access to its elements by definition. See std::stack reference.

Actually in your case container choice is wrong. If you need to access elements randomly (not only top stack element) use std::vector instead. Corresponding operations will be push_back() to place element on stack top, pop_back() to extract element from stack top and back() to access top stack element.

like image 38
Rost Avatar answered Oct 05 '22 23:10

Rost