Acessing an item of a vector

Let's say I have a class like this:
1
2
3
4
5
class person
{
   string address;
   int age;
};

I could put them in a map.
1
2
3
map<string, person> persons;
persons.insert(make_pair("Anne", person));
persons.insert(make_pair("Bob", person));

Then I could access them like this:
 
persons["Bob"].setAge(37);


It would be better to have the name in the person class alongside age and address. Then the person objects would be put in a vector. How could I then access a person by his/her name?
You would search the vector for the right item.

Things to think about while doing this:

Searching a vector can be very, very fast (thanks to on CPU cache) if the objects are small and the vector isn't huge, so having to search through the vector is usually really not a serious performance consideration.

If it is a consideration, the vector can be in a sorted order to make a search easier

The std library comes with some helpful algorithms for searching ( http://en.cppreference.com/w/cpp/algorithm/find ) that are probably a bit hard to understand for a beginner, but it's important to know what facilities already exist; there will come a day when you spend ages writing something that could have been made by chaining together a few functions that were already provided for you.

A manual search is as easy as:

1
2
3
4
5
6
7
8
9
10
11
12
int indexOfInterest = -1;
for (int i = 0; i < vectorOfPerson.size() ; ++i)
{
  if (vectorOfPerson[i].name == search_name)
  {
     indexOfInterest  = i;
     break;
  }
}

// Now, indexOfInterest gives you the index containing the person you were looking for,
//   or -1 if it wasn't found 



To search the vector you can use find_if
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
45
46
47
48
49
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

class Person
{
public:
  Person(const string& name, const string& address, int age):
    mName(name), mAddress(address), mAge(age)
  {}
  const string& getName()const {return mName;}
  const string& getAddress() const {return mName;}
  int getAge() const {return mAge;}
private:
  string mName;
  string mAddress;
  int mAge;
};

int main()
{
  vector<Person> people = 
  {
    Person("Anne", "some address", 22),
    Person("Lisa", "some address", 33),
    Person("Anja", "some address", 44)
  };
  string name;
  cout << "Enter name of person to find: ";
  getline(cin, name);
  auto it = find_if(people.begin(), people.end(), 
                    [&name](const Person& p)
                    { 
                      return name == p.getName();
                    });

  if (it != people.end()) // found
  {
    cout << "found";
  }
  else
  {
    cout << "not found";
  }
}

Could there be a function which looks for the person in the vector and then returns a reference to it. It could be used like this:
 
persons("Bob").setAge(37);

Would that be doable?
Last edited on
How would that function know which vector to look in? You'd have to also feed it the vector to look in.

persons(vector_of_people, "Bob").setAge(37);

You can certainly have a function return a reference to a person. What happens if the search term doesn't exist? The function must still return a reference to a person. Map does this by creating a whole new object, storing it in the map with the key that was being searched for, and returning it:

1
2
3
map<string, person> a_map;  // the map is empty

int age_of_bob = map["Bob"].age; // the map now contains person "Bob", even though we didn't explicitly insert that person 
Last edited on
Topic archived. No new replies allowed.