如何从数组执行索引搜索

发布于 2024-12-12 04:46:12 字数 1098 浏览 0 评论 0原文

我试图对字符串执行索引搜索,它运行但问题是它只打印出 i[0] 这是我的第一个条目。如果我查找另一个条目,它不起作用。请帮忙.. void clist(字符串 fn[],字符串 ln[], int 大小);

int search_list(const string fn[],const string ln[], int size, string find);

int main(){

    string search;

    cout << "This program searches a list .\n";

    const int total = 3;

    string fn[total];
    string ln[total];

    clist(fn,ln, total);

    cout << "Search contact:____  ";

    cin >> search;

    search_list(fn,ln, total, search);

  return 0;

}

void clist(string fn[],string ln[], int size){

    cout << "Enter " << size << " contact.\n";

    for (int index = 0; index < size; index++)
     cin >> fn[index] >> ln[index] ;

}

int search_list(const string fn[], const string ln[],int size, string search){

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

     if((fn[i] == search)&& (i < size)){

       cout<<"Result found "<<fn[i]<<" "<<ln[i]<<endl;

      break;

              }

    cout<<"no record found"<<endl;

     break;

    }

}

im trying to perform an index search for string, it runs but the problem is that it only prints out i[0] which is my first entry. if i lookup another entry it doesn't work. Please help..
void clist(string fn[],string ln[], int size);

int search_list(const string fn[],const string ln[], int size, string find);

int main(){

    string search;

    cout << "This program searches a list .\n";

    const int total = 3;

    string fn[total];
    string ln[total];

    clist(fn,ln, total);

    cout << "Search contact:____  ";

    cin >> search;

    search_list(fn,ln, total, search);

  return 0;

}

void clist(string fn[],string ln[], int size){

    cout << "Enter " << size << " contact.\n";

    for (int index = 0; index < size; index++)
     cin >> fn[index] >> ln[index] ;

}

int search_list(const string fn[], const string ln[],int size, string search){

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

     if((fn[i] == search)&& (i < size)){

       cout<<"Result found "<<fn[i]<<" "<<ln[i]<<endl;

      break;

              }

    cout<<"no record found"<<endl;

     break;

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韵柒 2024-12-19 04:46:12

您正在创建一个循环,并明确告诉它在第一次迭代后break退出循环。尝试这样写:

bool found = false;
for(int i=0;i<size;i++){
 if(fn[i] == search){ // No need to check for(i < size)
   found = true;
   cout<<"Result found "<<fn[i]<<" "<<ln[i]<<endl;
   break;
 }
}

if(!found)
  cout<<"no record found"<<endl;

You are making a loop and explicitly telling it to break out of it after the first iteration. Try writting it like this:

bool found = false;
for(int i=0;i<size;i++){
 if(fn[i] == search){ // No need to check for(i < size)
   found = true;
   cout<<"Result found "<<fn[i]<<" "<<ln[i]<<endl;
   break;
 }
}

if(!found)
  cout<<"no record found"<<endl;
℉服软 2024-12-19 04:46:12

您需要一个函数来给出要搜索的元素,找到该元素的索引。不要自己写,在数组上使用 std::find

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
   std::string xx[3];
   xx[0] = "zero";
   xx[1] = "one";
   xx[2] = "two";
   int index = std::find(xx, xx+3, "two") - xx;
   if (index < 3) std::cout << "found in position: " << index << endl;
   else std::cout << "not found" << endl;
}

我认为在你的情况下最好至少使用 std::vectorstruct 现在将您的信息存储在两个数组中

You need a function that given an element to search, find the index of that element. Don't write it by yourself, use std::find on array

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
   std::string xx[3];
   xx[0] = "zero";
   xx[1] = "one";
   xx[2] = "two";
   int index = std::find(xx, xx+3, "two") - xx;
   if (index < 3) std::cout << "found in position: " << index << endl;
   else std::cout << "not found" << endl;
}

I think in your case it's better to use at least std::vector or a struct to store your information now in the two arrays

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文