查找不使用字符串向量的函数

时间:2014-09-11 21:08:03

标签: c++

我有一个任务,我需要通过一个字符串向量。但是我的功能无法正常工作。当我尝试编译它时,我收到一个错误“std :: __ vector ........

中没有名为'find'的成员

我将从我的实现文件中包含我的头文件以及此函数。有人可以帮我追查这个问题吗?不知道为什么这不会用这些查找函数编译......

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

如果需要,这里是头文件

#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 个答案:

答案 0 :(得分:2)

对于std::vector,您必须使用std::find中的<algorithm>

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

使用std::set可能是更好的选择。

答案 1 :(得分:2)

Std :: vector没有定义查找功能。它显示在<algorithm>库中,您可以这样使用它:

#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()
}