What is the best List/Collections for objects

Hello guys,

I'm new on the C++. But I have to use some kind of list that must contain the objects. I know it is a college project but I don't want to copy/paste codes, I know the rules :). I just want you to help me guys.

What I'm looking for is certainly the person Array. Person is my object and I will have 10 person end of the project. And this list/array must have them.

Here are my files;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//person.h
#ifndef person_h
#define person_h
#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

enum personTypes {person, user, admin};

class person {
public:
    person(string, string, int, personTypes);
    void setName(string);
    string getName(void);
    void setSurname(string);
    string getSurname(void);
    void setTcNo(int);
    int getTcNo(void);
    void setPersonType(personTypes);
    personTypes getPersonType(void);
    static person personArray[10];
private:
    string name;
    string surname;
    int tcNo;
    personTypes personType;
};

#endif /* person_h */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//person.cpp
#include <iostream>
#include <string.h>
#include "person.h"

using namespace std;

person::person(string n, string s, int tc, personTypes pt){
    name = n;
    surname = s;
    tcNo = tc;
    personType = pt;
}
void person::setName(string n){
    name = n;
}

string person::getName(void){
    return name;
}

void person::setSurname(string s){
    surname = s;
}

string person::getSurname(void){
    return surname;
}

void person::setTcNo(int t){
    tcNo = t;
}

int person::getTcNo(void){
    return tcNo;
}

void person::setPersonType(personTypes p){
    personType = p;
}

personTypes person::getPersonType(void){
    return personType;
}


1
2
3
4
5
6
7
8
9
10
11
12
//test.cpp
#include <iostream>
#include <string>
#include "person.h"
#include "admin.h"
using namespace std;
int main(int argc, const char * argv[]) {
    // insert code here...
    person::person p2 ("Brian", "Oconner", 123, admin);
    person::person p3 ("Shan", "Stone", 456, user);
    person::personArray[0] = p2;
}


Thanks to all...

Last edited on
There is a specific data structure called a "linked list". Is that what you mean, or can it simply be any kind of collection?
Yes I know that, but I just want to use like ArrayList that contains object types in JAVA. :)
Sounds like you're looking for vector.

http://www.cplusplus.com/reference/vector/vector/
I guess I fixed this with your answer. I think what I need is "store class objects in vector." Bcoz I'll have the person's informations from the user(keyboard) then I'll set all of the informations to necessary variables of person.

I found this article and I think it's OK for me. I'll try this one.
http://www.dreamincode.net/forums/topic/63358-store-class-objects-in-vector/

By the way, I am still open for any suggestions.
Thank you so much for your help again.

Last edited on
Apart from using new to create the object and the subsequent memory leak, that seems fine.
Yeah, that's the another advantages but it's not important too much it won't be an enterprise project. :) But it's OK. I am going to use this.
Yeah, use a vector<person>. Life becomes super easy:

1
2
3
4
5
6
7
8
  vector<person> people;

  people.push_back( person( "Amat", "Haiek", 786, person::user ) );

  // Print all users
  for (auto p : people)
    if (p.getPersonType() == person::user)
      cout << p.getSurname() << ", " << p.getName() << "\n";

No more dealing with memory management stuff. The vector does it all for you.


Here are a couple of modifications to your type just to help out. The header/class prototype is what people look at when they want to understand what a thing does. It should be designed to clearly document what things are. So don’t leave out information. Also, keep related stuff encapsulated together. (The code example above assumes, for example, that the person type enum is declared inside the person class.)

1
2
3
4
5
6
7
8
9
10
11
class person {
  public:
    enum personTypes {person, user, admin};

    person(string givenName, string surname, int tcNo, personTypes type);

    void setName(string givenName);
    string getName();

    void setSurname(string surname);
    string getSurname();

I know some schools still want to use things like myfunc(void). Please don’t. Even Stroustrup considers that an abomination.

Hope this helps.
Thanks Duthombas, it was really helpful. I'll consider all these.
Collections.sort can be called with a custom comparator. And that comparator can be implemented to allow sorting in different sort orders. Here's an example (for your Person model - with age as an Integer):
public class FlexiblePersonComparator implements Comparator<Person> {
public enum Order {Name, Age, Country}

private Order sortingBy = Name;

@Override
public int compare(Person person1, Person person2) {
switch(sortingBy) {
case Name: return person1.name.compareTo(person2.name);
case Age: return person1.age.compareTo(person2.age);
case Country: return person1.country.compareTo(person2.country);
}
throw new RuntimeException("Practically unreachable code, can't be thrown");
}

public void setSortingBy(Order sortBy) {
this.sortingBy = sortingBy;
}
}

http://www.cetpainfotech.com/technology/c-language-training
Um, that’s Java — not even C as per your commercial. How is a different language supposed to help OP?
Registered users can post here. Sign in or register to post.