Parallel vectors (may be)

Pages: 12
a
Last edited on
Is there are reason you can't simply make a single vector of structs or something instead of maintaining two separate vectors?
It's easy to make a mistake when you have several vectors (though it seems like you're just not modifying name vector at all..). It is better to put name and salary into a structure.
http://www.cplusplus.com/doc/tutorial/structures/
Note that you will have to overload operator < or provide a function for comparing the objects.
You need to sort both vectors at the same time, but based only on the salaries. Or, you can also make a simple struct with the name and the salary, and define comparison operators that compare the salaries.
e
Last edited on
Your struct is named oddly, and you made a useless global variable, suggesting you don't understand how structres/classes work.

A struct/class is just the definition of a new type of variable like std::string or std::vector. You define how it works, and then you can make variables of that type. For instance, your struct would be defined like this:
1
2
3
4
5
struct Employee
{
  string Name;
  int Salary;
};


Then, if you wanted to make a vector of Employees,
vector<Employee> Company;

To add a new employee:
1
2
3
4
Employee NewEmp;
NewEmp.Name = /*Name of Employee*/;
Newmp.Salary = /*Salary of Employee*/;
Company.push_back(NewEmp);


To access or change this information later:
1
2
3
int x = /*ID of Employee*/;
Company.at(x).Name = /*New Name*/;
Company.at(x).Salary = /*New Salary*/;


This is a majorly simple way of doing things. Once you understand more about classs/structures, you can make constructors to more easily create new employees and functions to change the name and salary at the same time. But for you to be able to sort your employees, you need to add an overloaded operator < for your struct like so:
1
2
3
4
5
6
7
8
9
10
struct Employee
{
  string Name;
  int Salary;

  bool operator<(const Employee& CompareTo) const
  {
    return(Salary < CompareTo.Salary);
  }
};
Last edited on
Ok, I think I got the idea, I will try it in 5 minutes, and will post the result.I know about classes, and I noticed that its very similar to them, but I thought that its used for other purposes.

Last edited on
d
Last edited on
You'll probably need to rewrite the function to work with a vector of Employees. Mostly, you'll want to just replace the checks of the vector's elements with the member of Employee that you want to sort by.

If you know classes, structs are the same thing in C++ except their default privacy is set to public, rather than private. In C++, they are allowed to have methods since they are just another way to create a class, but they are generally used for groups of data like you have that you want to access directly.
Ok, I understand, I will try it.I will post the result :)
Last edited on
f
Last edited on
Firstly, you'll want to change all references to vector<int> to vector<Employee> since that's what you are using now. Then, in the merge set when comparing the vector values (previously ints), replace vector_name[i] to get some int with vector_name[i].Salary to get the particular Employee's salary instead.
h
Last edited on
Any Ideas?What should I replace?
@Everyone: The point of overloading the < operator in the class was to avoid changing the merge sort function. :/

@Jennifer: To answer an earlier question, classes are the same as structs. You can use either a struct or a class, they are exactly the same, but if you do not specify an access specifier like public, private, or protected, structs default to public acces and classes default to private access.

In your code, you can revert to your original merge sort in that you don't need to add .Salary to everything if you add the overloaded operator< in my post up above. The reason you're getting compile errors is because you wrote Comapny instead of Company (note the 'a' and the 'p')
k
Last edited on
1. Add this function to your struct:
1
2
3
4
bool operator<(const Employee& CompareTo) const
  {
    return(Salary < CompareTo.Salary);
  }


2. In your merge sort function, remove .Salary, as the function above does that for you; you can just directly compare two employee objects with emp1 < emp2 and it will call that function, which compares the salaries for you.
Jennifer, please don't delete and/or clear out your posts. Now somone searching for the solution to a similar problem will have no idea what this is or how it could have helped them.
Sometimes this sort of thing is to prevent classmates getting the same help and to hide from assessors the online evidence of getting help.
No excuse - people who do it should be banned .
Pages: 12