【问题标题】:Iterating through specific elements of a vector using another vector使用另一个向量迭代向量的特定元素
【发布时间】:2016-08-20 17:17:39
【问题描述】:

假设我有一个如下定义的字符串向量。

std::vector<std::string> names;
names.push_back( "Zero"  );
names.push_back( "One"   );
names.push_back( "Two"   );
names.push_back( "Three" );
names.push_back( "Four"  );
names.push_back( "Five"  );
names.push_back( "Six"   );
names.push_back( "Seven" );
names.push_back( "Eight" );
names.push_back( "Nine"  );

另外,假设我有一个向量,它定义了要循环的元素:

std::vector< int > indices;
indices.push_back( 0 );
indices.push_back( 5 );
indices.push_back( 6 );

如何根据向量indices 的元素对向量names 进行迭代,例如访问名称:"Zero""Five""Six"?我知道:

for( vector<string>::iterator it=names.begin() ; it < names.end(); it++)

遍历所有元素或我们可以找到模式的元素,例如,所有其他元素等。但是如何遍历没有模式或难以找到模式的元素呢?一个向量如何用于在另一个向量中进行迭代?比如:

for( vector<int>::iterator it=indices.begin() ; it < indices.end(); it++ )
{
     names.at( indices.at( it ) )
     ...
}

【问题讨论】:

    标签: c++ arrays loops vector iterator


    【解决方案1】:

    您的建议几乎是正确的。您应该取消引用迭代器,而不是 insdices.at(it)。但是你可以这样做:

    for(int index : indices) {
        names[index];
    }
    

    如果你不能证明names.size() > indices[i] 对所有i,你可以使用vector::at

    【讨论】:

      【解决方案2】:

      就这么简单:

      for( vector<int>::iterator it=indices.begin() ; it != indices.end(); ++it )
      {
           names.at( *it );
           names[*it]; // for faster but unvalidated access
           ...
      }
      

      注意:++it 可能更快(但不能更慢),因此通常在您不关心它是后缀形式还是前缀形式时使用。 it != container.end() 也经常被使用,因为它更通用(比随机访问迭代器工作的少,但不适用于前向迭代器)。

      【讨论】:

        【解决方案3】:

        您还可以使用带有 lambda 的 std::for_each 调用来访问索引(版本 1) 此外,您可以使用基于范围的 for 循环和 rvalues(版本 2)

        #include <vector>
        #include <algorithm>  
        
        int main()
        {
          std::vector<std::string> names;
          names.push_back("Zero");
          names.push_back("One");
          names.push_back("Two");
          names.push_back("Three");
          names.push_back("Four");
          names.push_back("Five");
          names.push_back("Six");
          names.push_back("Seven");
          names.push_back("Eight");
          names.push_back("Nine");
        
          std::vector< int > indices;
          indices.push_back(0);
          indices.push_back(5);
          indices.push_back(6);
        
          // version 1
          std::for_each(std::cbegin(indices), std::cend(indices),
            [&](auto &idx) { std::cout << names.at(idx) << "\n";});
        
          // version 2
          for (auto &&idx : indices)
            std::cout << names.at(idx) << "\n";
        
          return 0;
        }
        

        【讨论】:

        • 显然,auto 对于C+11 是有问题的。编译器抱怨auto changes meaning in C++11; please remove it
        • 这是特定于C++14 的。如果您使用的是C++11,则必须将auto 替换为特定类型。
        猜你喜欢
        • 1970-01-01
        • 2015-04-21
        • 2023-03-21
        • 1970-01-01
        • 2017-08-29
        • 1970-01-01
        • 2016-10-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多