int main(int argc, char **argv) { QApplication app( argc, argv ); typedef QMap<QString, Employee> EmployeeMap; EmployeeMap map; map["JD001"] = Employee("John", "Doe", 50000); map["JD002"] = Employee("Jane", "Williams", 80000); map["TJ001"] = Employee("Tom", "Jones", 60000); Employee sasha( "Sasha", "Hind", 50000 ); map["SH001"] = sasha; sasha.setSalary( 40000 ); EmployeeMap::Iterator it; for ( it = map.begin(); it != map.end(); ++it ) { printf( "%s: %s, %s earns %d\n",it.key().latin1(),it.data().surname().latin1(),it.data().forename().latin1(),it.data().salary() ); } // 透過find去尋找相對應的值 it = map.find("JD001"); printf( "%s: %s, %s earns %d\n",it.key().latin1(),it.data().surname().latin1(),it.data().forename().latin1(),it.data().salary() ); // 透過find去尋找, 若找不到會回傳值為end() it = map.find("Hello"); if (it == map.end()) { qWarning("do not find the element"); } return 0; }
int main() { Employee emp[5] = { Employee("张三","平安大街3号", "北京", "100000"), Employee("李四","王府井大街20号", "北京", "100000"), Employee("赵刚","中山路112号", "重庆", "400000"), Employee("陈芳","南京路234号", "上海", "200000"), Employee("周欣","人民东路476号", "重庆", "400000")}; for(int i=0;i<5;i++) emp[i].display(); }
int main() { std::vector<Employee> v = { Employee(108, "Zaphod"), Employee(32, "Arthur"), Employee(108, "Ford"), }; std::stable_sort(v.begin(), v.end()); for (const Employee &e : v) { std::cout << e.age << ", " << e.name << '\n'; } }
void TestSTL::predicate_example() { // unary predicate example int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sizeArray = sizeof(array)/sizeof(array[0]); std::vector<int> myVector(array, array + sizeArray); //fill a vector /** * std::countf_if algorithm: counts the number of elements that satisfy the * condition we supply (e.g isEven, isNegative) */ int res = std::count_if(myVector.begin(), myVector.end(), NumericUtilities::isEven); //returns the how many numbers are even TESTIFY_ASSERT(res == 6); res = std::count_if(myVector.begin(), myVector.end(), NumericUtilities::isNegative); //returns how many numbers are negative TESTIFY_ASSERT( res == 0); //binary predicates: Employee staff[] = { Employee(3, "Jonny"), Employee(2, "Bob"), Employee(20, "Filomela")}; int staffSize = sizeof(staff)/sizeof(staff[0]); std::vector<Employee> myStaff( staff, staff + staffSize); //fill a vector with these employees /** * std::sort algorithm: sorts the elements, from a range, into ascending order */ //sort by experience std::sort(myStaff.begin(), myStaff.end(), lessExperience); //supply a binary predicate for comparing employees TESTIFY_ASSERT( myStaff[0].getExperience() == 2); TESTIFY_ASSERT( myStaff[1].getExperience() == 3); TESTIFY_ASSERT( myStaff[2].getExperience() == 20); }
QString forename() const { return fn; } QString surname() const { return sn; } int salary() const { return sal; } void setSalary( int salary ) { sal = salary; } private: QString fn; QString sn; int sal; }; typedef Q3ValueList<Employee> EmployeeList; EmployeeList list; list.append( Employee("John", "Doe", 50000) ); list.append( Employee("Jane", "Williams", 80000) ); list.append( Employee("Tom", "Jones", 60000) ); Employee mary( "Mary", "Hawthorne", 90000 ); list.append( mary ); mary.setSalary( 100000 ); EmployeeList::iterator it; for ( it = list.begin(); it != list.end(); ++it ) cout << (*it).surname().latin1() << ", " << (*it).forename().latin1() << " earns " << (*it).salary() << endl; // Output: // Doe, John earns 50000
/** * Function for illustrating predicates */ void STLMoblet::predicates_explained() { LOG("\n"); LOG("========================= about predicates ======================================================================"); LOG("/**"); LOG("* A predicate is a function that returns a boolean value based on some"); LOG("* comparison criterion."); LOG("* Predicates are used usually with STL algorithms."); LOG("* Binary predicates - predicates that take two arguments."); LOG("* Unary predicates - predicates taking one argument."); LOG("* Usually binary predicates are used with algorithms that need to compare two"); LOG("* elements (e.g sorting algorithms)."); LOG("*/"); LOG("\n"); LOG(" Example using predicates: "); LOG("\n Unary predicate example..."); int array[] = { 0, 1, 2, 3, 4}; int sizeArray = sizeof(array)/sizeof(array[0]); std::vector<int> myVector(array, array + sizeArray); //fill a vector log_to_console(myVector, "myVector contains: "); LOG("/**"); LOG("* std::countf_if algorithm: counts the number of elements that satisfy the"); LOG("* condition we supply (e.g isEven, isNegative)"); LOG("*/"); log_to_console("Calling: std::count_if(myVector.begin(), myVector.end(), " "isEven);"); int res = std::count_if(myVector.begin(), myVector.end(), NumericUtilities::isEven); //returns the how many numbers are even log_to_console(res, "int res = std::count_if(myVector.begin(), myVector.end(), " "isEven); //res = "); log_to_console("\n Binary predicate example..."); Employee staff[] = { Employee(3, "Jonny"), Employee(2, "Bob"), Employee(20, "Filomela")}; int staffSize = sizeof(staff)/sizeof(staff[0]); std::vector<Employee> myStaff( staff, staff + staffSize); log_to_console(myStaff, "myStaff contains: "); LOG("\n"); LOG("/**"); LOG("* std::sort algorithm: sorts the elements, from a range, into ascending"); LOG("* order"); LOG("* lessExperience - a binary predicate for comparing employees"); LOG("*/"); LOG("\n"); TRACE(std::sort(myStaff.begin(), myStaff.end(), lessExperiencePredicate));; log_to_console(myStaff, "myStaff contains now: "); LOG("\n"); }
void initialize(EMPLOYEELIST& Employees) { // Initialize our employee list data source. Employees.push_back(Employee("Tyler Bennett", "E10297", 32000, "D101")); Employees.push_back(Employee("John Rappl", "E21437", 47000, "D050")); Employees.push_back(Employee("George Woltman", "E21437", 53500, "D101")); Employees.push_back(Employee("Adam Smith", "E21437", 18000, "D202")); Employees.push_back(Employee("Claire Buckman", "E39876", 27800, "D202")); Employees.push_back(Employee("David McClellan", "E04242", 41500, "D101")); Employees.push_back(Employee("Rich Holcomb", "E01234", 49500, "D202")); Employees.push_back(Employee("Nathan Adams", "E41298", 21900, "D050")); Employees.push_back(Employee("Richard Potter", "E43128", 15900, "D101")); Employees.push_back(Employee("David Motsinger", "E27002", 19250, "D202")); Employees.push_back(Employee("Tim Sampair", "E03033", 27000, "D101")); Employees.push_back(Employee("Kim Arlich", "E10001", 57000, "D190")); Employees.push_back(Employee("Timothy Grove", "E16398", 29900, "D190")); }