繁体   English   中英

如何为此 function 实现 while 循环

[英]How do I implement a while loop to this function

我正在做一个学校项目,我们将在其中制作一本通讯录。 在其中一个函数中,它必须使用名字查找联系人。

发生的错误是它只显示向量中的一个联系人。 我希望显示所有同名的联系人。

如何实现循环或计数器 (i++),使其包含所有同名联系人,而不仅仅是获取第一个索引?

我是 C++ 的新手,感谢所有帮助:)

我的 function:

int findTargetFirstName(vector<Kontaktbok>& bok, string target) {
  for (int i = 0; i < bok.size(); i++)
    if (bok[i].fnamn == target) return i;
  return -1;

void search(vector<Kontaktbok>& bok) {
  if (bok.size() > 0) {
    string firstname;
    cout << "First name of the contact: ";
    getline(cin, firstname);
    int pos = findTargetFirstName(bok, firstname); 
    cout << "Firstname, Lastname, Address, Pnummer, E-post, Telefonnummer" << endl;
    if (pos>=0) {
      cout << left;
      cout << setw(10) << bok[pos].fnamn << left
           << setw(15) << bok[pos].enamn << left
           << setw(20) << bok[pos].adress << left
           << setw(15) << bok[pos].pnummer <<left
           << setw(25) << bok[pos].epost <<left
           << setw(15) << bok[pos].telnummer << left << endl;
    }
    cout << "********************************************************************************" << endl;
  }
}

一种方法是添加一个参数来确定从哪个元素开始。

int findTargetFirstName(vector<Kontaktbok>& bok, string target, int start = 0) {
    for (int i = start; i < bok.size(); i++)
        if (bok[i].fnamn == target) return i;
    return -1;
}


void search(vector<Kontaktbok>& bok) {
    if(bok.size() > 0) {
        string firstname;
        cout << "First name of the contact: ";
        getline(cin, firstname);
        int pos = findTargetFirstName(bok, firstname);
        cout << "Firstname, Lastname, Address, Pnummer, E-post, Telefonnummer" << endl;
        while (pos>=0){ // change if to while
            cout << left;
            cout << setw(10) << bok[pos].fnamn << left
            << setw(15) << bok[pos].enamn << left
            << setw(20) << bok[pos].adress << left
            << setw(15) << bok[pos].pnummer <<left
            << setw(25) << bok[pos].epost <<left
            << setw(15) << bok[pos].telnummer << left << endl;
            pos = findTargetFirstName(bok, firstname, pos + 1); // search for next contact
        }
        cout << "********************************************************************************" << endl;
    }
}

另一种方法是让 function findTargetFirstName返回满足条件的联系人向量。

vector<int> findTargetFirstName(vector<Kontaktbok>& bok, string target) {
    vector<int> ret;
    for (int i = start; i < bok.size(); i++)
        if (bok[i].fnamn == target) ret.push_back(i);
    return ret;
}


void search(vector<Kontaktbok>& bok) {
    if(bok.size() > 0) {
        string firstname;
        cout << "First name of the contact: ";
        getline(cin, firstname);
        vector<int> poses = findTargetFirstName(bok, firstname); // change type and variable
        cout << "Firstname, Lastname, Address, Pnummer, E-post, Telefonnummer" << endl;
        for (int pos : poses){ // change if to range-based for
            cout << left;
            cout << setw(10) << bok[pos].fnamn << left
            << setw(15) << bok[pos].enamn << left
            << setw(20) << bok[pos].adress << left
            << setw(15) << bok[pos].pnummer <<left
            << setw(25) << bok[pos].epost <<left
            << setw(15) << bok[pos].telnummer << left << endl;
        }
        cout << "********************************************************************************" << endl;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM