Hello!
Ill just start out by showing some code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Person {
private:
string _name;
int _age;
int _id;
public:
Person(string name, int age, int id) { _name = name; _age = age; _id = id; }
void showPerson(){
cout << "Persons name:\t" << _name << endl;
cout << "Persons age: \t" << _age << endl;
cout << "Persons id: \t" << _id << endl;
}
};
int main() {
vector<Person> Store;
int id = 100;
bool rProgram = true;
do
{
string userInput;
getline(cin, userInput);
if(userInput == "1"){
string name1 = "Boris";
int age1 = 23;
string name2 = "Selma";
int age2 = 28;
Person person1(name1, age1, id);
Store.push_back(person1);
id++;
Person person2(name2, age2, id);
Store.push_back(person2);
}
else if(userInput == "2"){
for (int i = 0; i < Store.size(); ++i) {
Store[i].showPerson();
cout << endl;
}
}
else if(userInput == "3"){
cout << Store.size() << endl;
for (int i = 0; i < Store.size(); ++i) {
cout << Store[i] << endl;
}
}
else if(userInput == "4"){
rProgram = false;
}
}while(rProgram == true);
return 0;
}
[I save me objects(person) in to the vector named Store]
My first question is, is it possible to show the content in my vector some how?
I did this: " cout << Store.size() << endl; " and that worked. But i want to be able to do something like this (for learning):
" for (int i = 0; i < Store.size(); ++i) {
cout << Store[i] << endl; "
which is not possible?
So my next question is, what is actually stored in my vector. Is it the address to my objects? Nope? Its the objects that is stored in it and each object is taking up 8 bites+ the string…?
ill try it to explain why i might want to do this. If i want to move the objects around in my vector, say for sorting. it could be nice to see what what they are named? So i can move forward and tackle the next problem, for example sort them buy the persons name or age or id.
I’m not sure if any of this makes sense. Apologies in advance!
Best regards!