iaminsensible
iaminsensible

Reputation: 123

How to check if a vector contains a specific character within specified indices?

I would like to know how (if it is even possible) to search from the beginning of a vector up to a specified index if a character exists.

Pseudocode:

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

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

What is the most efficient way to do it?

Upvotes: 1

Views: 4553

Answers (5)

R2RT
R2RT

Reputation: 2146

As long as you operate on non-sorted container (std::vector is one) I'd go for std::any_of to express your intention. It will have same efficiency as 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;
}

Upvotes: 0

Christophe
Christophe

Reputation: 73376

With these definitions:

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

You can easily go for the following, starting after the first char (+1) and ending ignoring the two last elements (-2):

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

You can also use the more classical std::find() in the <algorithm> library:

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

If you want to start at a given offset or end ignoring some trailing elements, you can go iterator math on the start or end iterator:

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

If you do this often, and you're not intereste where in the vector the element is found, you could consider the following function:

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;
}

If you can go for std::string rather than std::vector, you could use the member function find() which can take an offset as argument.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

The others are right that, since algorithms take iterators, you have plenty of flexibility here.

Here's my variant.

I'm sticking with std::find because std::any_of adds complexity without adding clarity.

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; fall back on old-school syntax if you're pre-C++17.)

Upvotes: -1

brightprogrammer
brightprogrammer

Reputation: 89

#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;
         }
     }
}

Upvotes: 0

Jarod42
Jarod42

Reputation: 217235

Algorithms from standard take iterators, not container, so you can use them:

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

Upvotes: 5

Related Questions