简体   繁体   English

将向量初始化为零

[英]Initialize vector to zero

Initially the vector "perm" is null. 最初,向量“ perm”为空。 I want to set it to zero based on the index of the loop. 我想根据循环的索引将其设置为零。 But i have encounter error "vector subscript out of range" while doing the below code. 但是在执行以下代码时遇到错误“向量下标超出范围”。

I placed "perm(charLength, 0)" in the "Start()" method outside of any loop so that it will not be reset, it's used for accumulating values. 我在任何循环之外的“ Start()”方法中都将“ perm(charLength,0)”放置在任何循环之外,以便不会被重置,它用于累积值。

DecodeEngine.h 解码引擎

class DecodeEngine
{

public:

    vector<int> temp;
    vector<int> perm;

    //Default constructor
    DecodeEngine();


    //Declare a virtual destructor:
    virtual ~DecodeEngine();

    //Methods
    string GetFilePath();
    Mat Start();
    void FrameTo8by8();         
};

DecodeEngine.cpp DecodeEngine.cpp

Mat DecodeEngine::Start()
{
  charLength = 160;

  //Initialize perm to zero
  perm(charLength, 0);

  //Loop 1st 100 frame of header
  while(true)
  {
     if(frame_count <= 100)
     {
        FrameTo8by8();                  //Proccess and algorithm

        namedWindow("dctBlockImage"); 
        imshow("dctBlockImage", dctImage);      //display watermarked image

        if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
        {
            cout << "esc key is pressed by user" << endl; 
            break; 
        }

        frame_count++;
     }

     else
     {
        cout << endl;
        cout << "End of video" << endl;
        cout << endl;

        destroyWindow("Original Video");
        destroyWindow("dctBlockImage");
        break;
     }
}




void DecodeEngine::FrameTo8by8()
{
  for(int i = 0; i < height-16; i += 16)
  {
    for(int j = 0 ; j < width-16; j += 16)
    {
       if(j > 112)
       {
           if(((sum4 / 4) - avg) > 0)
           {
              value = 0;
              temp.push_back(value);
           }

           else
           {
              value = 1;
              temp.push_back(value);
           }
       }

       if(temp.size() == charLength)
       {
          for(int a = 0; a <= temp.size(); a ++)
          {
             //Initialize perm to zero
             perm[a] = 0;

             if(temp[a] == 1)
             {
                perm[a]++;
             }

             //Reset temp for next frame to use
             temp[a] = 0;
          }
       }                

    }
  }
}

This line: 这行:

perm(charLength, 0);

Does not call the constructor std::vector(size_t, const T&) and initialize the vector with 160 elements of value 10 . 不调用构造函数std::vector(size_t, const T&)并使用值10 160个元素初始化向量。 In fact, I don't see how that compiled at all since the vector class doesn't have an operator() . 实际上,由于向量类没有operator() ,我根本看不到该如何编译。

std::vector contains a member function assign() that you can instead: std::vector包含一个成员函数assign() ,您可以改为:

perm.assign(charLength, 0);

In the following piece of code, you go out of range: 在下面的代码中,您超出范围:

   ...
   if(temp.size() == charLength)   // ok!  temp.size() is same as perm.size() 
   {
      for(int a = 0; a <= temp.size(); a ++)   // !!! loop will execute with a=temp.size() and stop afterwards
      {
         //Initialize perm to zero
         perm[a] = 0;                    // !!! but subscripts start with 0 and go to temp.size()-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM