使用堆栈添加2个带有组件的向量组件

时间:2018-03-31 18:17:09

标签: c++ c++11 stack queue

我的C ++代码有问题。我需要逐个组件地计算向量的总和。例如,如果我有A(2,1)和B(3,3),结果应为(5,4)。我试图做一些事情,但显然我有问题,我不知道该怎么做。错误是:,,没有成员在std :: _ 1vector中调用push“我的代码是:

#include <iostream>
#include "stack_base.h"
#include <vector>

using namespace std;

template<typename T>
class App {
    public:
        Stack<T>  * stack;
        App(Stack<T> &stack) {
    this->stack = &stack;
}
      T sum(){
    Stack<T> *tempStack = stack;
    T sum=0;
    int size = stack->getTopLevel();
    for(int i=0; i<=size; i++) {
        sum+=tempStack->peek();
        tempStack->pop();
        }
    return sum;
}
      T substract(){
          Stack<T> tempStack = *stack;
          T substr=0;
          for(int i=0; i<=stack->getTopLevel(); i++) {
              substr-=tempStack.peek();
              tempStack.pop();
          }
          return substr;
      }
};
void display(vector<int> &v)
{
    for(int i = 0; i < v.size(); i++)
    {
        cout << v[i] << " ";
    }
    cout << "\n" << endl;
}
int main(){
    using namespace std;
    Stack<int> myStack;
    App<int> a(myStack);
    unsigned int i = 0;
    std::vector<int> v1;
    std::vector<int> v2;

    // Populate v1 and v2 here

    // Check that v1 and v2 have the same length
    if (v1.size() != v2.size())
    {
        // error here
    }

    std::vector<int> v3; // This will hold the resulting vector
    // Preallocate the necessary memory if you like here, but it
    // isn't necessary and doesn't gain you much.
    for (auto i = 0ul; i < v1.size(); ++i)
    {
        v3.push_back(v1[i] + v2[i]);
    }
    int x;
        cout << "Enter five integer values for v1" << endl;
        for(int i; i<5; i++)
        {
            cin >> x;
            v1.push_back(x);
        }
            cout << "Enter five integer values for v2" << endl;
            for(int i; i<5; i++)
            {
                cin >> x;
                v2.push_back(x);
            }
    cout << "Size of Vector= " << v2.size() << endl;
    display(v3);
        return 0;
    }

这是Stack:

#include <iostream>
using namespace std;
#define NMAX 30 // pre-processing directive

template<typename T>
class Stack {
    private:
        // an array of NMAX dimension
        T stackArray[NMAX];
        /* the top of the stack, representing the INDEX of last element of the
        stackArray:0, 1, 2,....*/
        int topLevel;
    public:
        void push(T x) {
            //puts an element in the stack array

            //check if the stack array has the maximum dimension
            if (topLevel >= NMAX - 1)
            {
                cout<<"The stack is full: we have already NMAX elements!\n";
                //exit the function without making anything
                return;
            }
            /*add an element=> the index of the last element of the stack Array
            increases and put the value of the new element in the stack array*/
            stackArray[++topLevel] = x;
        }

        int isEmpty() {
            //returns 1, if topLevel>=0, meaning the stack array has elements
            // returns 0, otherwise
            return (topLevel < 0);
        }

        T pop() {
            // extracts and element from the stack array and returns the new top
            if (isEmpty()) {
                // the extraction is made only if the array is not empty
                cout<<"The stack is empty! \n";
                T x;
                return x;
            }
            // the topLevel decreases and the new top is changed
            return stackArray[--topLevel];
        }

        T peek() {
            // returns the top of the stack
           if (isEmpty()) {
                    // the extraction is made only if the array is not empty
                    cout<<"The stack is empty! \n";
                    T x;
                    return x;
            }
            return stackArray[topLevel];
        }
        void display(){
  for(int i=0;i<=topLevel;i++)
  cout<<stackArray[i];
  }
  bool search(T num){
  for(int i=0; i<=topLevel;i++)
    if(num==stackArray[i])
    return true;
   return false;
  }
  int getTopLevel(){
      return topLevel;
    }
        Stack() { // constructor
            topLevel = -1; // the stack is empty in the beginning
        }

    void sort(T s){
      if (!isEmpty()) {
        T x = Pop(s);
        sort(s);
        push(s, x);
    }
  }

        ~Stack() { // destructor
        }
};

1 个答案:

答案 0 :(得分:0)

您永远不会向向量添加任何元素。在前面三行中,您尝试访问不存在的元素,但是如果您调用&#39; at&#39;那么向量只会检查边界。方法而不是使用括号表示法。你想在那个for循环中完成什么?

要添加两个相同长度的向量,这是我想你想要的,你可以这样做:

std::vector<int> v1;
std::vector<int> v2;

// Populate v1 and v2 here

// Check that v1 and v2 have the same length
if (v1.size() != v2.size())
{
    // error here
}

std::vector<int> v3; // This will hold the resulting vector
// Preallocate the necessary memory if you like here, but it
// isn't necessary and doesn't gain you much.
for (auto i = 0ul; i < v1.size(); ++i)
{
    v3.push_back(v1[i] + v2[i]);
    // Debugging statement
    std::cout << "Added " << v1[i] << " and " << v2[i] << " to get " << v3[i] << " for index " << i << std::endl;
}