我试图创建一个函数来按名称或姓氏对地址簿中的联系人列表进行排序。按C++语言按字母顺序排列对象列表
void sortList (list<Contact> & address_book){
//define two iterators - first to point to the first element from the list, second to the second element
list<Contact>::iterator it = address_book.begin();
list<Contact>::iterator it2 = address_book.begin();
it2++;
//get the last name for the first 2 contacts
string last_name1 = it->get_last_name();
string last_name2 = it2->get_last_name();
int i = 0;
while (i < last_name1.length() && i < last_name2.length()){
if (last_name1[i] < last_name2[i]){
swap(it, it2);
break;
}
}
}
我确定我没有正确地做,但是我对这些迭代器有点失落。我也知道我应该有另一个while循环来遍历我的所有联系人,直到所有的联系人被排序,但是,老实说,我不知道如何实现它。
这些更简单的功能就像一个魅力。没有错误,所以永远。谢谢! – Adrian 2012-02-24 00:48:48