如何列出向量中存储的结构迭代次数

How to list how many iterations of a struct are stored inside a vector?

本文关键字:结构 迭代 存储 何列出 向量      更新时间:2023-10-16

我是 c++ 的新手(以及一般的编码(,最近一直在使用向量中保存的结构,在这种情况下:

struct Contact{
    string name;
    string address;
    string phone;
    string email;};
vector<Contact> contacts;

因此,我的一个函数涉及搜索每个联系人,以查找名称中存储的字符串与搜索输入匹配的联系人。为此,我做了一个 for 循环:

for(int i = 0; i < contacts.size(); i++){
    if(contacts[i].name == searchInput){
        cout << contacts[i].address << "nr" << contacts[i].phone << "nr" << contacts[i].email;

但是由于某种原因,这只能找到正确的联系人,如果它是存储在以下位置的名称:

contacts[0].name

没有其他人。因此,在试图找出问题所在时,我决定这样做

cout << contacts.size();

我认为应该输出 3,因为我只存储了三个联系人。然而,由于某种原因,它输出 7。无论如何,我是否可以准确列出存储在联系人向量中的联系人迭代次数以使我的 for 循环正常工作?

编辑我的完整代码:

#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
struct Contact
{
    string name;
    string address;
    string phone;
    string email;
};
bool go;
bool a = false;
char command;
string endL = "nr";
string tab = "t";
string line;
int i;
int counter = 0;
int contactCounter = 0;
vector<Contact> contacts;
void add(){
    contacts.push_back(Contact());
    int newcontact = contacts.size() - 1;
    string input;
    cout << "Enter the name: " << endL;
    cin >> input;
    contacts[newcontact].name = input;
    cout << "Enter the address: " << endL;
    cin >> input;
    contacts[newcontact].address = input;
    cout << "Enter the phone number: " << endL;
    cin >> input;
    contacts[newcontact].phone = input;
    cout << "Enter the email address: " << endL;
    cin >> input;
    contacts[newcontact].email = input;
}

void search(string name){
    for(int i = 0; i < contacts.size(); i++){
        if(contacts[i].name == name){
            cout << "Name: " << contacts[i].name << endL << "Address: " << contacts[i].address << endL << "Phone Number: " << contacts[i].phone << endL << "Email: " << contacts[i].email << endL << endL;
            a = true;
        }
    }
    if(a == false){
        cout << "There is no contact under that name." << endL;
    }
}
int main() {
    ifstream phonebook;
    phonebook.open("phonebook.txt");
if(phonebook.is_open()){
    while(getline(phonebook,line)){
        if(line.empty() == false){
            if(counter % 4 == 0){
                contacts.push_back(Contact());
                contacts[contactCounter].name = line;
            }else if(counter % 4 == 1){
                contacts[contactCounter].address = line;
            }else if(counter % 4 == 2){
                contacts[contactCounter].phone = line;
            }else if(counter % 4 == 3){
                contacts[contactCounter].email = line;
                contactCounter++;
            }
            counter++;
        }
    }
    }else{cout << "an error has occurred while opening the phonebook";}
   phonebook.close();
   cout << contacts.size() << endL;
    cout << "Enter a command." << endL << tab << "To add a contact, enter '+'" << endL << tab << "To search for a contact, enter 's'" << endL << tab << "To delete a contact, enter '-'" << endL << tab << "To quit the program, enter 'q'" << endL;
    cin >> command;
    while(command != 'q'){
        if(command == '+'){
            add();
            command = '/';
        }
        else if(command == 's'){
            string searched;
            cout << "Please enter who you would like to search for: ";
            cin >> searched;
            search(searched);
            command = '/';
        }
        else if(command == '-'){
            cout << "Not done." << endL;
            command = '/';
        }
        else if(command == '/'){
            cout << "Enter a command." << endL << tab << "To add a contact, enter '+'" << endL << tab << "To search for a contact, enter 's'" << endL << tab << "To delete a contact, enter '-'" << endL << tab << "To quit the program, enter 'q'" << endL;
            cin >> command;
        }
        else{
            cout << "That command is invalid." << endL;
            cout << "Enter a command." << endL << tab << "To add a contact, enter '+'" << endL << tab << "To search for a contact, enter 's'" << endL << tab << "To delete a contact, enter '-'" << endL << tab << "To quit the program, enter 'q'" << endL;
            cin >> command;
        }

    }
    ofstream newbook;
   newbook.open("phonebook.txt");
   if(newbook.is_open()){
        for(int i=0; i < contacts.size(); i++){
            newbook << contacts[i].name << endl;
            newbook << contacts[i].address << endl;
            newbook << contacts[i].phone << endl;
            newbook << contacts[i].email << endl;
            newbook << endL;
        }
   }else{cout << "there was an issue saving your contacts" << endL;}
    newbook.close();
return 0;
}

除了这一行之外,你的代码实际上没有任何问题

string endL = "nr";

这真的应该只是

string endL = "n";

n会自动转换为系统使用的行尾,传统上在Unix系统上n (0x0a),在Windows上rn (0x0d0a)

但是,这对该计划有何影响?好吧,它只有在程序末尾写入电话簿后才会生效,以便phonebook.txt包含这些在末尾(在Windows上(rnr的虚假行尾。因此,当读取文件时,它会读取直到新行rn,并将rPerson Name视为后面的行!这就解释了为什么搜索失败了。

您还可能会看到生成的一些其他虚假联系人,因为末尾可能有一些额外的r,每个读作一行。如果不看你的phonebook.txt我不能确定为什么你有额外的 4,尽管我猜额外的 r s 会是原因。

总而言之,将n用于新行。

要回答标题,vector::size()是获取向量中存储对象数量的方法。这不是在骗你。

使用基于范围的 for 循环可确保您不会击中任何不存在的联系人:

for(auto&& contact: contacts)
{
    // Contact contact is now accessible.
}

此外,将a存储为全局变量可能不是一个好主意。如果执行两次search会怎样?