簡體   English   中英

如何將空對矢量推回到另一個向量?

[英]How would one push back an empty vector of pairs to another vector?

  std::vector<std::vector< std::pair<int, int> > > offset_table;
  for (int i = 0; i < (offset.Width()*offset.Width()); ++i)
  {
    offset_table.push_back(  std::vector< std::pair<int, int> >  );
  }

這是我的代碼,但我收到錯誤:

main.cpp: In function ‘void Compress(const Image<Color>&, Image<bool>&, Image<Color>&, Image<Offset>&)’:
main.cpp:48:66: error: expected primary-expression before ‘)’ token

我不希望對中有任何值,我只想擁有一個空向量的向量。 我該怎么做?

你想構造一個傳遞給push_back的向量,你只是缺少括號:

offset_table.push_back(  std::vector< std::pair<int, int> >()  );

或者,您可以執行以下操作,而不是循環。 它更好,因為向量將在單個分配中分配適量的內存:

offset_table.resize( offset.Width()*offset.Width(), std::vector< std::pair<int, int> >() );

或者這更簡潔,因為它使用了resize的默認第二個參數:

offset_table.resize( offset.Width()*offset.Width() );
std::vector<std::vector< std::pair<int, int> > > offset_table;

這是一個二維數組,因此您需要使用嵌套數組。 僅用於獲得內部向量的長度。

for(vector< pair<int, int >> vv in offset_table)
{
    if(vv.size() == 0)
    {
        // this is your target.
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM