How to check whether element of vector is same?
Jul 5, 2014 at 11:55pm UTC
How to check if a container of a vector have same element?
for example: a vector is contained with employee and task. i want to sort all the task by the name of employee.
Jul 6, 2014 at 12:27am UTC
I'm not entirely sure what you mean, but you could use the standard algorithms:
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
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
struct Data {
std::string employee;
std::string task;
};
int main() {
std::vector<Data> employees {
{ "Bob" , "Nothing" },
{ "James" , "Sleep" },
{ "Alice" , "Eat" },
{ "Fred" , "Roll on floor" }
};
std::sort(employees.begin(), employees.end(),
[](const Data& a, const Data& b){ return a.employee < b.employee; });
for (const auto & x : employees)
std::cout << x.employee << "\t: " << x.task << "\n" ;
return 0;
}
Aug 17, 2014 at 3:34pm UTC
thanks man.
Topic archived. No new replies allowed.