struct address{
string firstName;
string lastName;
string email;
string phoneNumber;
};
Contacts.push_back(&address);//Compile error
Contacts[0].firstName = str;
Contacts[0].lastName = str;
Contacts[0].email = str;
Contacts[0].phoneNumber = str;
Vector of a Structure
I need to know how to create a vector of a structure. This is what I have so far, but it is not working...
str being a string value from earlier.
When you call push_back() you need to supply an address object to copy.
std::vector<address> Contacts; Contacts.push_back(address()); Contacts[0].firstName = str; Contacts[0].lastName = str; Contacts[0].email = str; Contacts[0].phoneNumber = str;
Given that address is the name of a type, and not of a variable, your code reads a bit as if you had written Contacts.push_back(&int);, which you will agree doesn't mean much...
There are two approaches you can take. The first is to create a variable outside the vector and then push it in - you can either modify that original or later modify the one in the vector. The other solution is to just pass a temporary variable to push_back:
or
or
The first and third solution are probably the most preferable.
Also note how I used Contacts.back() instead of Contacts[0]. Since you're using push_back, the new element gets added to the back of the vector. Contacts[0] always refer to the first element (as Contacts.front() would). If you have more than one element in the vector, that probably won't do what you want. [smile] Contacts.back() will always be the last element in the vector.
There are two approaches you can take. The first is to create a variable outside the vector and then push it in - you can either modify that original or later modify the one in the vector. The other solution is to just pass a temporary variable to push_back:
address contact;contact.firstName = str;contact.lastName = str;contact.email = str;contact.phoneNumber = str;Contacts.push_back(contact);
or
address contact;Contacts.push_back(contact);Contacts.back().firstName = str;Contacts.back().lastName = str;Contacts.back().email = str;Contacts.back().phoneNumber = str;
or
Contacts.push_back(address());Contacts.back().firstName = str;Contacts.back().lastName = str;Contacts.back().email = str;Contacts.back().phoneNumber = str;
The first and third solution are probably the most preferable.
Also note how I used Contacts.back() instead of Contacts[0]. Since you're using push_back, the new element gets added to the back of the vector. Contacts[0] always refer to the first element (as Contacts.front() would). If you have more than one element in the vector, that probably won't do what you want. [smile] Contacts.back() will always be the last element in the vector.
On the other hand, it's not unreasonable to add a constructor to the struct (it will still qualify as a struct, and not require copy ctor or anything else complicated - since each piece of data is an actual object that already handles that stuff, treating the whole struct memberwise is ok):
struct address { string firstName; string lastName; string email; string phoneNumber; address(string firstName, string lastName, string email, string phoneNumber) : firstName(firstName), lastName(lastName), email(email), phoneNumber(phoneNumber) {}};vector<address> Contacts;// Do it all in one breath, yet readably :)Contacts.push_back(address(str, str, str, str));
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement