【问题标题】:How to check if a vector contains a specific character within specified indices?如何检查向量是否包含指定索引内的特定字符?
【发布时间】:2019-07-25 00:15:23
【问题描述】:

如果存在字符,我想知道如何(如果可能的话)从向量的开头搜索到指定的索引。

伪代码:

vector<char> a{a, b, c, d, e, f, /*...*/};

if (from a.begin() to a[2] b exists) {
... }

最有效的方法是什么?

【问题讨论】:

  • 以上似乎不是有效的 c++ 代码,因此任何人都很难提供帮助
  • this 可能对你有用。
  • “最快和最简单”很少能很好地结合在一起。你知道这样做的任何(不是最快或最简单的)方法吗?也许甚至是一种不太有效的方法?如果是这样,请将其添加到问题中。出于某种原因,人们更愿意响应“修复我的代码”请求而不是“为我编写代码”请求。
  • 我不知道该怎么做,而且 Google 似乎也没有对我的问题有任何答案。这就是我在这里问的原因。另外,我删除了最简单的方法,因为我对学习最简单的代码编写方法不感兴趣,但最有效的方法。

标签: c++ algorithm search vector


【解决方案1】:

只要您在未排序的容器上操作(std::vector 是一个),我会去std::any_of 表达您的意图。效率和std::find一样

#include <vector>
#include <algorithm>

int main()
{
    std::vector<char> a{'a', 'b', 'c', 'd', 'e', 'f'};
    // Checks range between 'b'...'d'
    if (std::any_of(a.begin() + 1, a.end() - 2, [](char c){return c == 'b';})) {
      // do stuff
    }
    return 0;
}

【讨论】:

  • OP 想要 0⇒2,而不是 0⇒(n-2)
  • return 10 是关于什么的?
  • 嗯,这真的很重要吗?这只是any_of 的示例用法。我相信 OP 可以根据他的需要进行调整。
  • 嗯,是的,这很重要。你在教书。为什么要把没有任何意义或错误的东西放在教学示例中?
  • contains a specific character within specified indices - 这就是问题所在。不是从伪代码中读取的任何特定用例。 PS。出于好奇,我使用return 10 来检查 gcc 是否会优化整个调用,确实如此。
【解决方案2】:

有了这些定义:

vector<char> a = {'a', 'b', 'c', 'd', 'e', 'f' /*...*/};
char ch = 'x';  

您可以轻松地执行以下操作,从第一个字符 (+1) 开始,忽略最后两个元素 (-2) 结束:

if (any_of(a.cbegin()+1, a.cend()-2, [&ch](auto &x){return x==ch; }))
    cout << "found !"<<endl; 
else cout << "not found! "<<endl; 

你也可以使用&lt;algorithm&gt;库中更经典的std::find()

if (std::find(a.cbegin(), a.cend(), ch) !=a.cend()) 
   ...

如果你想从给定的偏移量开始或结束忽略一些尾随元素,你可以在开始或结束迭代器上进行迭代器数学:

if (std::find(a.cbegin()+3, a.cend(), ch) != a.cend()) 
    ...

如果您经常这样做,并且您对在向量中的哪个位置找到元素不感兴趣,您可以考虑以下函数:

template <class U, class W> 
bool myfind (const  U &v, size_t starto, size_t endo, const W& x) {
    if (starto>=endo || endo>=v.size())
        return false; 
    return std::find(v.begin()+starto, v.begin()+endo, x) != v.begin()+endo;
}

如果您可以选择std::string 而不是std::vector,则可以使用成员函数find(),它可以将偏移量作为参数。

【讨论】:

  • @JesperJuhl 我只是在这个方向编辑 ;-)
【解决方案3】:

其他人是对的,因为算法采用迭代器,所以你在这里有很大的灵活性。

这是我的变种。

我坚持使用std::find,因为std::any_of 增加了复杂性而没有增加清晰度。

std::vector<char> a{'a', 'b', 'c', 'd', 'e', 'f', /*...*/};

assert(a.size() >= 2);
const auto start  = a.cbegin();
const auto end    = a.cbegin() + 2;

const char search = 'b';

if (auto it = std::find(start, end, search); it != end)
{
   std::cout << "Found '" << search << "'!\n";
}

Here's more on the new if construction;如果您是 C++17 之前的版本,请使用老式语法。)

【讨论】:

  • 有什么问题吗?
【解决方案4】:
#include<vector>
#include<iostream>
using namespace std;
int main(){
     vector<char> A;
     //initialize your vector here
     unsigned i;
     unsigned start, stop;
     //initialize the start and stop here
     for(i=start; i<stop; i++){
         if(A[i]=='b'){
             cout<<"character found!"<<endl;
             break;
         }
     }
}

【讨论】:

  • 我不是要查找元素的位置,而是要查找指定索引中是否存在元素。
【解决方案5】:

来自标准迭代器的算法,而不是容器,所以你可以使用它们:

auto it = std::find(a.begin(), a.begin() + 2, 'b');
if (it != a.begin() + 2) {
    // found.
}

【讨论】:

  • 我根本没想过!我会尝试在我的代码中实现它,如果它不起作用,我会回复。
  • 只要坚持std::vectorstd::any_of 效率一样,而且更明确
猜你喜欢
  • 2014-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 2011-05-20
相关资源
最近更新 更多