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

时间:2016-05-11 01:52:32

标签: c++

我是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 << "\n\r" << contacts[i].phone << "\n\r" << 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 = "\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;
}

2 个答案:

答案 0 :(得分:1)

除了这一行

之外,您的代码实际上没有任何问题
string endL = "\n\r";

哪个应该只是

string endL = "\n";

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

但是,这对程序有多大影响呢?好吧,只有在程序结束时写入电话簿后才会生效,以便phonebook.txt包含最后有\r\n\r的这些虚假行结尾(在Windows上)。因此,当文件被读取时,它会一直读到新行\r\n并看到\rPerson Name后面的行!这解释了搜索失败的原因。

您还可能会看到一些额外的虚假联系人生成,因为最后可能会有一些额外的\r个,每个都是一行。不看你的phonebook.txt我不能肯定地说为什么你有额外的4,虽然我猜是额外的\r s将是原因。

总而言之,使用\n表示新行。

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

答案 1 :(得分:0)

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

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

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