试图访问 std::stack 的索引

Trying to access an index of an std::stack(试图访问 std::stack 的索引)
本文介绍了试图访问 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;
}
}

我收到这些错误

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)]â

对于这些行

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

我查找了 std::stack 并发现默认情况下,如果没有为特定堆栈类指定容器类,则使用标准容器类模板双端队列."当我查看双端队列时,我发现它支持 operator[].这就是我声明我的堆栈的方式.在这个源文件对应的头文件中.

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

在我看来,在 std::stack 上使用 operator[] 应该可以正常工作.有什么想法吗?

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

推荐答案

std::stack 不继承底层容器类型,而是将其适配为全新的接口.底层容器未暴露.这基本上就是适配器 std::stackstd::queue 的意义所在:它们确保您使用的是更有限的接口,无论底层结构.

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.

也就是说,您可以从 std::stack 继承并从子类访问底层容器.它是一个名为 cprotected 成员.

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

这篇关于试图访问 std::stack 的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)