Jayden Collis
Jayden Collis

Reputation: 155

C++ How do I check if an object is in an array?

I'm new to c++ and I'm making my first project with it. I have an array with some strings in it and I need my code to check if an input is in that array, store it and continue with the code but I don't know how.

Code:

#include <iostream>
using namespace std;


int battleCapacity;
string T1P1;
string pokemonlist[] = {"Bulbasaur", "Charmander", "Squirtle", "Pikachu"};

int main() {
    cout << "Pokemon Battle Buddy\nHP and Damage Calculator\nBattle Capacity: ";
    cin >> battleCapacity;
    if (battleCapacity <= 6) {
        cout << "Trainer 1; Pokemon 1: ";
        cin >> T1P1;
    } else {
        cout << "Not Working";
    }
}

I know in Python it is

if x in y:
    return

I'm looking for something similar to this. Thanks

Upvotes: 0

Views: 715

Answers (4)

Kostiantyn Kurbatskyi
Kostiantyn Kurbatskyi

Reputation: 364

If You want to add something to the array it is much more prefarable to use vector instead of a regular array, as are of a fixed size.

that said, here is a simple function to find a string in the vector:

bool stringInVector(vector<string> list, string str)
{
    for(int i = 0; i < list.size(); i++)
    {
        if(list[i] == str)return true;
    }
    return false;
}

You could also use the already existing function find as other users had already mentioned.

Here is what the complete code would look like:

#include <iostream>
#include <vector>
using namespace std;


int battleCapacity;
string T1P1;
vector<string> pokemonlist= {"Bulbasaur", "Charmander", "Squirtle", "Pikachu"};

bool stringInVector(vector<string> list, string str)
{
    for(int i = 0; i < list.size(); i++)
    {
        if(list[i] == str)return true;
    }
    return false;
}

int main() {
    cout << "Pokemon Battle Buddy\nHP and Damage Calculator\nBattle Capacity: ";
    cin >> battleCapacity;
    if (battleCapacity <= 6) {
        cout << "Trainer 1; Pokemon 1: ";
        cin >> T1P1;
        if(stringInVector(pokemonlist, T1P1)) cout << "Exists";
    } else {
        cout << "Not Working";
    }
}

Upvotes: 1

mgonnav
mgonnav

Reputation: 178

If you just want an already implemented function like in Python, eerorika's answer will suffice. However, if you want to understand this process, in the case of an unordered array you would visit each element in the array and if you reach the end without finding it, then it isn't there.

string value = "example";
for (int i = 0; i < arrSize; i++)
  if (arr[i] == value)
    cout << value << " is in the array.\n";

Upvotes: 0

tdao
tdao

Reputation: 17668

I have an array with some strings in it and I need my code to check if an input is in that array

Instead of array you can use vector, then you can use find standard algorithm:

std::vector<std::string> pokemonlist = {"Bulbasaur", "Charmander", "Squirtle", "Pikachu"};

if(pokemonlist.end() != std::find(pokemonlist.begin(), pokemonlist.end(), "Charmander"))
{
    std::cout << "found Charmander\n";
}
else
{
    // not found
}

Upvotes: 0

eerorika
eerorika

Reputation: 238351

C++ How do I check if an object is in an array?

This can be done with the following algorithm:

for each element in array
    if element is the one we are searching, then
       return true
return false

This is called a linear search. More general version of this algorithm can iterate over any range and returns iterator to the found element. There is no need to implement this because the standard library has got it covered already: std::find.

Upvotes: 2

Related Questions