Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何实现该函数的while循环_C++ - Fatal编程技术网

C++ 如何实现该函数的while循环

C++ 如何实现该函数的while循环,c++,C++,我正在做一个学校项目,我们将制作一本联系簿。在其中一个函数中,它必须使用名字查找联系人 出现的错误是,它只显示向量中的一个触点。我希望显示所有同名联系人 如何实现一个循环或计数器(I++),使其包含所有同名联系人,而不仅仅是第一个索引

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

出现的错误是,它只显示向量中的一个触点。我希望显示所有同名联系人

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

<新的C++,感谢所有的帮助:

我的职能:

int findTargetFirstName(向量和bok,字符串目标){
对于(int i=0;i0){
字符串名;

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

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

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

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

不要在第一个结果出现时返回,而是将其添加到std::vector中,并在您似乎知道的for循环之后返回整个向量,因此第一步可能是使
findTargetFirstName
返回索引的
vector
,而不是仅返回第一个匹配项,或者使其开始从一些
startIndex
并重复调用它。适当的缩进会使人们更容易想帮助你。与其在第一个结果时返回,不如将其添加到std::vector中,并在你似乎知道的for循环后返回整个向量
std::vector
,因此可能的第一步是使
findTargetFirstName返回索引的
向量
,而不仅仅是第一个匹配项,或者让它开始从一些
开始搜索索引
,并重复调用它。适当的缩进将使人们更容易帮助你。