【问题标题】:Adding 2 vectors component with component using stack使用堆栈添加 2 个向量组件和组件
【发布时间】:2018-09-10 11:16:57
【问题描述】:

我的 C++ 代码有问题。我需要逐个分量地计算向量的总和。例如,如果我有 A(2,1) 和 B(3,3),结果应该是 (5,4)。我试图做某事,但显然我遇到了问题,我不知道该怎么做。错误是:,,No member called push in std::_1vector" 我的代码是:

#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;
    }

这是堆栈:

#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
        }
};

【问题讨论】:

  • 你的向量是空的。 Stack 是什么?
  • 我编辑了它,这是一个我存储元素的类,我添加到堆栈,我删除它等等
  • 我试图用 v1.insert(1,2,4) 添加一些东西,但它不起作用,我使用它不正确吗?谢谢!或者也可以使用 v1.push

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


【解决方案1】:

你永远不会在你的向量中添加任何元素。在前面的三行中,您尝试访问不存在的元素,但是如果您调用 'at' 方法而不是使用方括号表示法,向量只会进行边界检查。你想在那个 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;
}

【讨论】:

  • 那么如何向向量中添加元素?
  • 现在如果我尝试显示 v1 和 v2 的分量之和,我会得到无效的二进制表达式操作数
  • cout
  • push_back 函数没有返回值,所以不能打印出来。文档是here
猜你喜欢
  • 2020-05-18
  • 2017-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
相关资源
最近更新 更多