S60 на Symbian ОС имеет большой интересный случай проектов, касающийся языков рабочего стола/сервера для перемещения мобильных приложений. Некоторый интересный ones:-
Python: sourceforge
Ruby: рубин-symbian
Mozilla: mozilla
S60Webkit: S60browser
POSIX: openc_cpp
private static System.Collections.ArrayList alFileTypes =
new System.Collections.ArrayList(new string [] {"css","gif","htm","html","txt","xml"});
Не обязательно очевидно, что сортировка людей всегда будет происходить по возрасту. Если, однако, в контексте вашей программы было бы очевидно, что сортировка людей будет выполняться по возрасту, если вы явно не указали иное, тогда имеет смысл реализовать сравнение
as person::operator<
instead of in a separate comparison class the way I've done it above. Как уже отмечали другие, вам следует подумать о группировке людей и возрастов.
Если вы не можете / не хотите, вы можете создать для них «индекс» и вместо этого отсортировать этот индекс. Например:
// Warning: Not tested
struct CompareAge : std::binary_function<size_t, size_t, bool>
{
CompareAge(const std::vector<unsigned int>& Ages)
: m_Ages(Ages)
{}
bool operator()(size_t Lhs, size_t Rhs)const
{
return m_Ages[Lhs] < m_Ages[Rhs];
}
const std::vector<unsigned int>& m_Ages;
};
std::vector<std::string> people = ...;
std::vector<unsigned int> ages = ...;
// Initialize a vector of indices
assert(people.size() == ages.size());
std::vector<size_t> pos(people.size());
for (size_t i = 0; i != pos.size(); ++i){
pos[i] = i;
}
// Sort the indices
std::sort(pos.begin(), pos.end(), CompareAge(ages));
Теперь имя n-го человека человек [pos [n]]
, а его возраст составляет возрастов [pos [n]]
Generally you wouldn't put data that you want to keep together in different containers. Make a struct/class for Person and overload operator<
.
struct Person
{
std::string name;
int age;
}
bool operator< (const Person& a, const Person& b);
Or if this is some throw-away thing:
typedef std::pair<int, std::string> Person;
std::vector<Person> persons;
std::sort(persons.begin(), persons.end());
std::pair
already implement comparison operators.
Нет смысла хранить их в двух разных структурах данных: если вы переупорядочите Люди
, у вас больше не будет разумного сопоставления с Возрастами
].
template<class A, class B, class CA = std::less<A>, class CB = std::less<B> >
struct lessByPairSecond
: std::binary_function<std::pair<A, B>, std::pair<A, B>, bool>
{
bool operator()(const std::pair<A, B> &left, const std::pair<A, B> &right) {
if (CB()(left.second, right.second)) return true;
if (CB()(right.second, left.second)) return false;
return CA()(left.first, right.first);
}
};
std::vector<std::pair<std::string, int> > peopleAndAges;
peopleAndAges.push_back(std::pair<std::string, int>("Anne", 23));
peopleAndAges.push_back(std::pair<std::string, int>("Bob", 23));
peopleAndAges.push_back(std::pair<std::string, int>("Charlie", 23));
peopleAndAges.push_back(std::pair<std::string, int>("Douglas", 23));
std::sort(peopleAndAges.begin(), peopleAndAges.end(),
lessByPairSecond<std::string, int>());
I would suggest merging these two lists into a single list of structures. That way you can simply define operator <
like dirkgently said.