gpt4 book ai didi

c++ - 如何列出存储在 vector 中的结构的迭代次数?

转载 作者:行者123 更新时间:2023-11-30 05:27:40 24 4
gpt4 key购买 nike

我是 C++ 的新手(和一般的编码),最近一直在使用一个保存在 vector 中的结构,在这种情况下:

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 << "\n\r" << contacts[i].phone << "\n\r" << contacts[i].email;

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

contacts[0].name

没有其他人。所以在试图找出问题所在的同时,我决定做

cout << contacts.size();

我认为应该输出 3,因为我只存储了三个联系人。然而由于某种原因,它输出 7。我是否可以准确列出联系人 vector 中存储的联系人的迭代次数,以便让我的 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 = "\n\r";
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 = "\n\r";

这真的应该只是

string endL = "\n";

\n 会自动转换为系统使用的行尾,在 unix 系统上传统上是 \n (0x0a)\r\n (0x0d0a) 在 Windows 上。

但是,这对程序有如此大的影响吗?好吧,只有在程序末尾写入电话簿后,它才会生效,因此 phonebook.txt 包含这些伪造的行结尾,这些结尾处有 \r\n\r结束(在 Windows 上)。因此,当文件被读取时,它会一直读取到新行 \r\n 并且在后面的行中看到 \rPerson Name !这解释了为什么搜索失败。

您还可能会看到生成了一些额外的虚假联系人,因为末尾可能有一些额外的 \r,每个都读作一行。如果不查看您的 phonebook.txt,我无法确定为什么您有额外的 4 个,但我猜额外的 \r 是原因。

总而言之,换行使用\n

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

关于c++ - 如何列出存储在 vector 中的结构的迭代次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37151787/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com