-1

I have an assignment where I need to go through a vector of strings. However my function is not working correctly. When I am trying to compile it, I am getting an error "no member named 'find' in"std::__vector........

I'll include my header file as well as this function from my implementation file. Can someone help me track down the issue? Not sure why this won't compile with these find functions...

#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <sstream>
#include "DictionarySet.h"
#include <cstdlib>
#include <vector>
using namespace std;


bool DictionarySet::Contains(string word){

  if(Standard.find(word) != Standard.end())
    return true;
  if(User.find(word) != User.end())
    return true;
  if(Temporary.find(word) != Temporary.end())
    return true;

  if(isupper(word[0]))
  {
    word[0] = tolower(word[0]);

    if(Standard.find(word) != Standard.end())
        return true;
    if(User.find(word) != User.end())
        return true;
    if(Temporary.find(word) != Temporary.end())
        return true;
   }

    return false;
}

here is the header file if you need it

#ifndef _DICTIONARYSET_H
#define _DICTIONARYSET_H
#include <vector>
#include <string>
//*** include suitable STL container
using namespace std;
//*** Deal with namespace



class DictionarySet
{
    const char * const StandardDictionary;
    char * UserDictionary;
    string DictionaryPath;

    void ReadDictionary(const char * const DictionaryName, vector<string> &Set);

public:
    /*** STL container ***/ std::vector<std::string> Standard;
    /*** STL container ***/ std::vector<std::string> User;
    /*** STL container ***/ std::vector<std::string> Temporary;

    bool Contains(string word);
    void Add(string word);
    void Ignore(string word);
    void Initialize();

    DictionarySet(const char * const SD = "/usr/share/dict/words", const char * const UD =     "_Dictionary");
    ~DictionarySet();
};


#endif

2 Answers2

4

For std::vector, you have to use std::find from <algorithm> :

if (std::find(Standard.begin(), Standard.end(), item) != Standard.end()) {
    return true;
}

using std::set may be a better option btw.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
2

Std::vector doesn't define find function. It is presented in <algorithm> library and you use it this way:

#include <algorithm>

std::vector<item_type>::iterator it;
if ( ( it = std::find( vector.begin(), vector.end(), item)) != vector.end())
{
    // found, use iterator it
} else {
    // not found, it == vector.end()
}
4pie0
  • 29,204
  • 9
  • 82
  • 118