Bạn chưa hiểu chỗ nào , phải nói ra thì người khác mới biết chứ .
Lớp Sinh Viên thì sắp xếp tăng giảm theo cái gì (Điểm , Năm sinh ....)?
" Viết hàm/ lớp template để sắp tăng/giảm một mảng với kiểu dữ liệu bất kỳ : int ,float, double, SinhVien ( Struct / Class Sinhvien ) ".
Em chưa hiểu rõ về template.
Anh (chị) có thể cho em vài ý kiến cho bài tập trên không ạ?
Em xin cám ơn.
Bạn chưa hiểu chỗ nào , phải nói ra thì người khác mới biết chứ .
Lớp Sinh Viên thì sắp xếp tăng giảm theo cái gì (Điểm , Năm sinh ....)?
Đã được chỉnh sửa lần cuối bởi Tab : 22-05-2008 lúc 06:19 PM.
Em không hiểu cách dùng class template.
Bài này tăng/giảm 1 mảng với 1 kiểu dữ liệu bất kỳ theo điểm trung bình (trong đó đặc biệt em không biết cách xài với struct)
Đây là 1 ví dụ đơn giản, trong struct thì swap cả struct tuy nhiên nếu vậy thì struct đó phải có định nghĩa lại toán tử < và >. Template cơ bản thì ok, nhưng rules của nó cũng rất nhiều ( nó khó không thua 1 ly so với OOP trong C++ ). Cậu tìm cuốn "The complete Guide Template" của David Vandevoorde và Nicolai M. Josuttis mà đọc để hiểu cơ bản trước, sau đó sẽ tìm đọc những cuốn nâng cao hơn.
Đây là ví dụ sort :
Và đây là struct :C++ Code:
#include <iostream> template< typename T > void swap2Item( T& lhs, T& rhs ) { T temp = lhs; lhs = rhs; rhs = temp; } template< typename T > void selectionSort( T* ary, unsigned size ) { int outer, inner, min_index; T min_value; for( outer = 0; outer < size - 1; ++outer ) { min_index = outer; min_value = ary[ outer ]; for( inner = outer + 1; inner < size; ++inner ) { if( ary[ inner ] < min_value ) { min_value = ary[ inner ]; min_index = inner; } } swap2Item< T >( ary[ min_index ], ary[ outer ] ); } } template< typename T > void showAry( T* ary, int size ) { int index; for( index = 0; index < size; ++index ) { } } int main() { int ary[ 4 ] = { 3, 2, 5, 1 }; selectionSort< int >( ary, 4 ); showAry< int >( ary, 4 ); return 0; }
C++ Code:
#include <iostream> #include <string> struct StudentType { std::string name; double average; bool operator <( const StudentType& rhs) { return average < rhs.average; } }; template< typename T > void swap2Item( T& lhs, T& rhs ) { T temp = lhs; lhs = rhs; rhs = temp; } template< typename T > void selectionSort( T* ary, unsigned size ) { int outer, inner, min_index; T min_value; for( outer = 0; outer < size - 1; ++outer ) { min_index = outer; min_value = ary[ outer ]; for( inner = outer + 1; inner < size; ++inner ) { if( ary[ inner ] < min_value ) { min_value = ary[ inner ]; min_index = inner; } } swap2Item< T >( ary[ min_index ], ary[ outer ] ); } } template< typename T > void showAry( T* ary, int size ) { int index; for( index = 0; index < size; ++index ) { } } int main() { StudentType ary_of_student[ 3 ]; ary_of_student[ 0 ].name = "A"; ary_of_student[ 0 ].average = 0.3; ary_of_student[ 1 ].name = "B"; ary_of_student[ 1 ].average = 0.5; ary_of_student[ 2 ].name = "C"; ary_of_student[ 2 ].average = 0.1; selectionSort< StudentType >( ary_of_student, 3 ); for( int x = 0; x < 3; ++x ) { } return 0; }
Dạ, em cám ơn. Em sẽ tìm hiểu kỹ vấn đề này. Vì cuối kỳ, đề của bọn em là tối ưu mã nguồn. Template là 1 trong những phần đó.