23
#include <iostream>
#include <string>
using namespace std;

bool in_array(string value, string *array)
{
    int size = (*array).size();
    for (int i = 0; i < size; i++)
    {
        if (value == array[i])
        {
            return true;
        }
    }

    return false;
}

int main() {
    string tab[2] = {"sdasd", "sdsdasd"};
    string n;
    cin >> n;
    if (in_array(n, tab)) {

    }
    return 0;
}

I want to check in C++ if n string is in tab array, but the code return an error. What I am doing wrong? Maybe I should use the vectors?

user3050705
  • 457
  • 1
  • 5
  • 13
  • 2
    Just use `std::find`. As is, you're pretending there are five strings in the array. – chris Nov 30 '13 at 18:12
  • `(*array).size();` is `array->size()`. –  Nov 30 '13 at 18:16
  • @H2CO3 `(*array).size()` is actually `array[0].size()`. Look at the brackets – smac89 Nov 30 '13 at 18:25
  • @Smac89 I suggest you read a beginner C++ guide concerning pointers. (and not try to teach me C++ until you have no idea what you are doing.) `array[0].size()` is the same as `array->size()`. –  Nov 30 '13 at 18:26
  • @H2CO3 Thanks for the clarification but not going back to beginner c++ – smac89 Nov 30 '13 at 18:29

3 Answers3

37
int size = (*array).size();

It will not tell you the size of array, it tells you the length of first string in that array, you should pass the length of array to the function separately. The function should look like:

bool in_array(string value, string *array, int length)

 

But a better choice is using std::vector and std::find:

#include <vector>
#include <algorithm>


bool in_array(const std::string &value, const std::vector<std::string> &array)
{
    return std::find(array.begin(), array.end(), value) != array.end();
}

and then, you can use it like:

std::vector<std::string> tab {"sdasd", "sdsdasd"};

if (in_array(n, tab))
{
    ...
}
Federico Baù
  • 6,013
  • 5
  • 30
  • 38
masoud
  • 55,379
  • 16
  • 141
  • 208
  • 3
    +1 for recommending vector and using find rather than reinventing the wheel – smac89 Nov 30 '13 at 18:24
  • 1
    @user3050705: Yes, you need to `#include ` and also enable C++11 by `--std=c++11` compiler switch (in clang and gcc) – masoud Dec 01 '13 at 07:23
9

When passing an array as an argument to a function which takes only a pointer, you can't query the size of the array within the function (since it got converted to a "stupid" pointer to the first element, nothing more). You typically add a "count" parameter to your signature or an "end" iterator instead.

What you're trying to implement is basically std::find. It takes two iterators (begin and end of the sequence) and the element to be found. Simply use this function.

std::find(std::begin(tab), std::end(tab), n);

will return an iterator to the element if it was found, the end iterator otherwise. Checking for equality with the end iterator will tell you if the element was found in the array.

If you don't like the "iterator interface" of the std algorithms, you can achieve your PHP-like signature by wrapping around std::find by using a template function:

template<class Element, class Container>
bool in_array(const Element & element, const Container & container)
{
    return std::find(std::begin(container), std::end(container), element)
            != std::end(container);
}

Please note: This answer assumes C++11. If you use an older compiler, it might not work or it only works if you add -std=c++11 to the compiler flags.

leemes
  • 44,967
  • 21
  • 135
  • 183
3

masoud answer is correct but it overly complicated. all you need is this.

bool isInVect=false;
isInVect = std::find(vector.begin(), vector.end(), stringToFind) != vector.end();
        
if (isInVect == true)
{
   cout << "Found string in Vector ..." << endl;
}
Sam B
  • 27,273
  • 15
  • 84
  • 121