【问题标题】:How do I initialize an empty array of objects in C++?如何在 C++ 中初始化一个空的对象数组?
【发布时间】:2018-02-26 02:02:58
【问题描述】:

我正在尝试创建一个联系人类的空对象数组。从一个空数组开始,我想在 AddrBook.cpp 中创建一个函数,将 Contact 的对象添加到对象数组中,称为 addressBook。

我是否在 AddrBook.h 中正确初始化了数组?

如何检查某个联系人的对象是否存在于特定索引处?


AddrBook.cpp

#include "AddrBook.h" 
namespace address_book_test
{
    const int CAPACITY = 5;

    void AddrBook::addContact(Contact& itemToAdd) // Add Contact to the AddrBook (using Contact object)
    {
        for (int i = 0; i < CAPACITY; i++)
        {
            if (/*Contact object does not exist at i*/)
            {
            /*Add Contact object*/
            return;
            }
        }
        return;
    }
...
}

AddrBook.h

#ifndef ADDR_BOOK_H
#define ADDR_BOOK_H

#include <fstream>
using namespace std;

#include "Contact.h"

namespace address_book_test
{
    class AddrBook
    {
    public:

        static const int CAPACITY = 5;

        // CONSTRUCTOR
        AddrBook() { used = 0; }

        // Modification Member Functions
        void addContact(Contact& itemToAdd); // Add Contact to the AddrBook (using Contact object)
...
    private:
        static Contact addressBook[CAPACITY]; // The array used to store Contact objects
        int used; // How much of addressBook is used
    };
}
#endif

联系人.cpp

#ifndef CONTACT_H
#define CONTACT_H

#include <fstream>
using namespace std;

#include "Address.h"
#include "Name.h"

namespace address_book_test
{
    class Contact
    {
    public:

        // Constructor
        Contact(string inLastName = "",
            string inFirstName = "", 
            string inStreetAddress = "",
            string inCity = "",
            string inState = "",
            string inZip = "",
            string inPhone = "",
            string inEmail = "",
            string inBirthday = "",
            string inPictureFile = "")
        {
            Name(inLastName, inFirstName);
            Address(inStreetAddress, inCity, inState, inZip);
            setPhone(inPhone);
            setEmail(inEmail);
            setBirthday(inBirthday);
            setPictureFile(inPictureFile);
        }
...
        private:
        Name fullName;
        Address fullAddress;
        string phone;
        string email;
        string birthday;
        string pictureFile;
    };
}
#endif

【问题讨论】:

  • 数组有固定的大小。你不能有一个空数组。它总是有精确的CAPACITY 元素数量。如果您需要更改大小,请使用std::vector
  • i 位置已存在对象。您唯一能做的就是用副本覆盖它。

标签: c++ arrays object initialization


【解决方案1】:

不要使用数组,使用:

 std::vector<Contact> addressBook;

而不是

 static Contact addressBook[CAPACITY];

您真的需要将其定义为静态吗?

有了vector,你就不需要变量“used”了。如果你想知道你有多少联系人,你只需要写

 addressBook.size();

现在,如果要查找特定联系人,可以使用 find:

 if(find(addressBook.begin(), addressBook.end(), my_contact) != addressBook.end()){
...
}

【讨论】:

    【解决方案2】:

    你有一个名为'used'的变量,所以我想你想使用这个变量来跟踪数组中有多少位置被填充,然后你只需要在填充数组中的另一个空间时增加该变量然后您可以通过执行以下操作来检查该位置是否已被使用: if(i>=used){} 你只需要记住,数组从索引 0 开始,当它被填充时,你使用的变量是 1,所以它总是比最后填充的索引高 1。

    【讨论】:

      【解决方案3】:

      如果您想减少应用程序的内存占用,您可以在第一次向地址簿添加元素时使用 call_once 来初始化向量

      std::vector<Contact> addressBook;
      
      void AddContact(string Contact) {
          std::call_once(onceFlag, [this] { this->addressBook.reserve(1000); cout << "size Reserved" << endl; });
          addressBook.push_back(Contact);
      }
      

      “尺寸保留”只会出现一次

      【讨论】:

        猜你喜欢
        • 2022-08-12
        • 2012-02-02
        • 1970-01-01
        • 2013-10-12
        • 1970-01-01
        • 2012-05-28
        • 2011-08-18
        • 2020-07-01
        相关资源
        最近更新 更多