C++11 搜索函数返回错误的结果

C++11 搜索函数返回错误的结果,c++11,C++11,我为我的程序做了一个函数,从文本文件中读取内容,将内容添加到向量中,然后在该向量中搜索。问题是,即使文件是空的,它也表明它找到了什么,另一方面,如果我将返回值更改为0,它根本不会给出结果 #include <iostream> #include <string> #include <vector> #include <fstream> #include <algorithm> using namespace std; vecto

我为我的程序做了一个函数,从文本文件中读取内容,将内容添加到向量中,然后在该向量中搜索。问题是,即使文件是空的,它也表明它找到了什么,另一方面,如果我将返回值更改为0,它根本不会给出结果

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>

 using namespace std;

 vector<string> contacts;


 //This function returns at what index the name is found
  int searchContact(string contactToSearch) 
  {


   string entry;
   ifstream input;
   input.open("contacts.txt");

     while (input.good()) 
         {
             while (getline(input, entry))
                 {
                     contacts.push_back(entry);

                 }
             input.close();
         }

   for(int i = 0; i < contacts.size(); i++) 
     {

         if(contactToSearch == contacts[i]) 
             {
         //Found => Returning index rest of program can see index
         return i;
            }
      }

      return 1;
     }
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
矢量接触;
//此函数返回找到名称的索引
int searchContact(字符串contactToSearch)
{
字符串输入;
ifstream输入;
打开(“contacts.txt”);
while(input.good())
{
while(getline(输入,条目))
{
触点。推回(输入);
}
input.close();
}
对于(int i=0;i返回索引程序的其余部分可以看到索引
返回i;
}
}
返回1;
}

我刚刚对您的代码进行了一点重构。进一步的改进是可能的,但首先

1) 你不需要一段时间来输入。好()

2) 您试图返回0和1,这两个位置实际上是字符串可能出现在向量中的有效位置

除此之外,我仍然认为您的代码可能没有正确填充数组。原因可能是:-区分大小写的比较,读取不正确的文件,二进制文件。。等等

下面是一个重构代码,您可以使用

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>

using namespace std;

void readContacts(const string &fileName inputFileName, vector<string> &contacts){
    string entry;
    ifstream input;
    input.open(inputFileName);
    if (input.good())
    {
        while (getline(input, entry))
            contacts.push_back(entry);
        input.close();
    }
}

int searchContact(const string &contactToSearch, vector<string> &contacts)
{
    for (int i = 0; i < contacts.size(); i++)
    {
        if (contactToSearch == contacts[i])
            return i;       
    }
    return -1;
}

int main(){
    vector<string> contacts;
    // This needs to be filled in with the contact name u want to search
    string contactToSearch;
    readContacts("contacts.txt", contacts);
    int index = searchContact(contactToSearch, contacts)
        if (index != -1)
            cout << "Found Contact " << contactToSearch" at location " << index << endl;
        else
            cout << "Could Not find contact " << contactToSearch << endl;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void readContacts(常量字符串和文件名输入文件名、向量和联系人){
字符串输入;
ifstream输入;
打开(inputFileName);
if(input.good())
{
while(getline(输入,条目))
触点。推回(输入);
input.close();
}
}
int searchContact(常量字符串和contactToSearch、vector和contacts)
{
对于(int i=0;i我刚刚对你的代码进行了一点重构。进一步的改进是可能的,但首先

1) 你不需要一段时间来输入。好()

2) 您试图返回0和1,这两个位置实际上是字符串可能出现在向量中的有效位置

除此之外,我仍然认为您的代码可能没有正确填充数组。原因可能是:-区分大小写的比较,读取不正确的文件,二进制文件..等等

下面是一个重构代码,您可以使用

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>

using namespace std;

void readContacts(const string &fileName inputFileName, vector<string> &contacts){
    string entry;
    ifstream input;
    input.open(inputFileName);
    if (input.good())
    {
        while (getline(input, entry))
            contacts.push_back(entry);
        input.close();
    }
}

int searchContact(const string &contactToSearch, vector<string> &contacts)
{
    for (int i = 0; i < contacts.size(); i++)
    {
        if (contactToSearch == contacts[i])
            return i;       
    }
    return -1;
}

int main(){
    vector<string> contacts;
    // This needs to be filled in with the contact name u want to search
    string contactToSearch;
    readContacts("contacts.txt", contacts);
    int index = searchContact(contactToSearch, contacts)
        if (index != -1)
            cout << "Found Contact " << contactToSearch" at location " << index << endl;
        else
            cout << "Could Not find contact " << contactToSearch << endl;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void readContacts(常量字符串和文件名输入文件名、向量和联系人){
字符串输入;
ifstream输入;
打开(inputFileName);
if(input.good())
{
while(getline(输入,条目))
触点。推回(输入);
input.close();
}
}
int searchContact(常量字符串和contactToSearch、vector和contacts)
{
对于(int i=0;icout在读取文件内容的循环中,不需要外部循环。顺便问一下,在没有找到您搜索的项目之间有何区别(在循环后返回
1
),并且当您搜索的项目在索引
1
中找到时?如果没有找到该项目,您可能应该返回类似
-1
的内容。还要注意,当您使用
=
比较字符串时,这是一个精确的比较,并且区分大小写。如果我将return更改为-1,我得到的结果是在-1索引中找到的。这意味着您搜索的内容没有找到,您需要在调用函数中实际检查这种可能性。另外,请重新阅读我的上一条评论。在您读取文件内容的循环中,不需要外部循环。顺便问一下,您搜索的未找到的项(当您在循环后返回
1
时)之间有何区别,并且当您搜索的项目在索引
1
中找到时?如果没有找到该项目,您可能应该返回类似
-1
的内容。还要注意,当您使用
=
比较字符串时,这是一个精确的比较,并且区分大小写。如果我将return更改为-1,我得到的结果是在-1索引中找到的。这意味着您搜索的内容没有找到,您需要在调用函数中实际检查这种可能性。另外,请重新阅读我的上一条评论。