【问题标题】:How do I get the first layer index of 2D vector?如何获得二维向量的第一层索引?
【发布时间】:2020-07-16 10:22:04
【问题描述】:

我需要帮助获取 2D 矢量的第一层索引。每个元素都是唯一的,因此元素没有重复。这就是我的意思。

我有一个向量定义为:

vector<vector<int>> vec { {0,1} , 
                          {2} , 
                          {3,4}, 
                          {5,6} }

然后,我想在“第一”层获取任何数字所在位置的索引。 我的意思是,如果我说 index of 4,它应该返回2。 如果我说index of 6,它应该返回3

提前谢谢你!

【问题讨论】:

  • 如果我们可以分配额外的空间,我们可以使用unordered_map&lt;int, int&gt; 来存储它们所属的向量的数字和索引。如果没有,那么我们将不得不遍历整个 2D 数组,除非您没有提供更多的 2D 数组结构。
  • @Someprogrammerdude 看起来他们的意思是“找到 4 并在子向量中返回它的索引”
  • 如果你的数字从0开始是连续的,那么你最好创建一个vector&lt;int&gt;,其中第一个元素对应于0层,第二个元素对应于1层,依此类推。 O(N) 空间,O(1) 访问和更改。

标签: c++ algorithm vector stl


【解决方案1】:

您可以使用std::findstd::find_if

#include <vector>
#include <algorithm>
#include <iostream>

int main()
{
    std::vector<std::vector<int>> vec { {0,1} , 
                              {2} , 
                              {3,4}, 
                              {5,6} };

    // Search for the 4
    auto iter = std::find_if(vec.begin(), vec.end(), [](const std::vector<int>& v)
                            {return std::find(v.begin(), v.end(), 4) != v.end();});

    // Output the distance between the item and the beginning of the vector
    std::cout << std::distance(vec.begin(), iter);
}

输出:

2 

外部std::find_if 搜索std::vector&lt;vector&lt;int&gt;&gt;,lambda 的参数将是对每个内部向量的引用。内部 std::find 会在该内部向量中搜索值。

【讨论】:

    【解决方案2】:

    您可以编写一个计算索引的函数,例如:

    int findIndex(const std::vector<std::vector<int>> &vec, int val)
    {
        auto it = std::find_if(vec.cbegin(), vec.cend(), [val](const std::vector<int> &v) {
            return std::find(v.cbegin(), v.cend(), val) != v.cend();
        });
    
        return it != vec.cend() ? std::distance(vec.cbegin(), it) : -1;
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用标准算法std::find_if 和算法std::find

      这是一个演示程序。

      #include <iostream>
      #include <vector>
      #include <iterator>
      #include <algorithm>
      
      int main() 
      {
          std::vector<std::vector<int>> v =
          { 
              { 0, 1 }, { 2 }, { 3, 4 }, { 5, 6 } 
          };
      
          auto present = []( const auto &v, const auto &value ) 
          { 
              return std::find( std::begin( v ), std::end( v ), value ) != std::end( v );
          };
          
          int value = 4;
          
          size_t i = std::distance( std::begin( v ), 
                                    std::find_if( std::begin( v ), std::end( v ),
                                                  [&, present, value]( const auto &item )
                                                  {
                                                      return present( item, value );
                                                  } ) );
      
          if ( i != v.size() ) std::cout << value << ": " << i << '\n';
          
          value = 6;
          
          i = std::distance( std::begin( v ), 
                             std::find_if( std::begin( v ), std::end( v ),
                                           [&, present, value]( const auto &item )
                                           {
                                                  return present( item, value );
                                           } ) );
      
          if ( i != v.size() ) std::cout << value << ": " << i << '\n';
          
          return 0;
      }
      

      程序输出是

      4: 2
      6: 3
      

      【讨论】:

        【解决方案4】:

        您可以使用像 unordered_map 这样的哈希表数据结构在 O(1) 时间内完成此操作。

        unordered_map <int,int> m;
        for(int i=0;i<vec.size();i++){
          for(int j=0;j<vec[i].size();j++){
            m[vec[i][j]] = i;
          }
        }
        
        

        【讨论】:

          猜你喜欢
          • 2020-11-27
          • 2017-12-20
          • 2021-11-03
          • 2017-11-09
          • 2023-02-23
          • 2019-09-15
          • 2023-01-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多