« Home « Chủ đề lập trình c/c++ nâng cao

Chủ đề : lập trình c/c++ nâng cao


Có 18+ tài liệu thuộc chủ đề "lập trình c/c++ nâng cao"

LẬP TRÌNH C nâng cao - bài 1 - nhắc lại về c-c++

tailieu.vn

//định nghĩa kiểu dữ liệu const float PI=3.14. //có thể nhập khoảng trắng cout<<a;. cout<<p<<endl. //cai gi do bat ki, dia chi cua a cout<<&p<<endl. cout<<*p<<endl. Nhập xuất dữ liệu với kiểu mảng số nguyên CODE. Truyền dữ liệu trực tiếp theo kiểu C, cách 1 CODE. Truyền dữ liệu trực tiếp theo kiểu C, cách 2 CODE. Truyền dữ...

LẬP TRÌNH C nâng cao - bài 2 - nhắc lại về c-c++ part2

tailieu.vn

Con trỏ cấu trúc (struct pointer) CODE. //means (*s).id cout<<m.id;. cin>>temp;. add(m[0].name,&m[0].id);. cin>>name;. cin>>*place;. add(a.name,&a.id);. cin>>s.name;. cin>>s.id;. cin>>(*s).name;. cin>>(*s).id;. cin>>s->name;. cin>>s->id;. Con trỏ (pointer). cout<<m<<endl;cout<<n<<endl;. cout<<s<<endl;. outf<<"Next is"<<setw(4)<<number<<endl;

LẬP TRÌNH C nâng cao - bài 3 - nhắc lại về lớp

tailieu.vn

Ðiều gì sẽ xảy ra khi chúng ta không thể cấp phát bộ nhớ ? Ví dụ chúng ta viết 1 game RTS mà mỗi phe tham chiến có 10 tỉ. Giải quyết khi không thể cấp phát bộ nhớ thành công Chúng ta vẫn thường cấp phát bộ nhớ như sau. cout<<"number of element u want:";. Nếu chúng ta...

LẬP TRÌNH C nâng cao - bài 3 - nhắc lại về lớp part 2

tailieu.vn

this->day=day;. this->month=month;. this->age = n;. cout<<equal(s1,s2);. cout<<((s1==s2)?"equal":"unequal");. Overload toán tử nhập và xuất (input >>. và output <<). Mọi người đều biết cin>>a là gọi toán tử nhập cin.operator>>(a) hoặc operator>>(cin,a) Overload 2 toán tử nhập và xuất này hết. operator>>(istream&,Date&);. operator<<(ostream&,const Date&);. operator>>(istream&. ins>>d.day;. ins>>d.month;. operator<<(ostream&. outs<<d.day<< lt;<d.month;. cin>>d;cout<<d;. //phải tạo object pointer, cấp phát bộ nhớ cin>>*dt;cout<<*dt;. this->day=day;this->month=month;this->special=special;....

LẬP TRÌNH C nâng cao - BÀI 4 - TEMPLATE

tailieu.vn

template<class T>T maximum(T a,T b). Cứu tinh xuất hiện, đó là một tham chiếu mà tham chiếu đến một con trỏ (a reference which refers to a pointer). Đây là dạng đau đầu nhất của tham chiếu.. //tham chiếu r là nickname mới của p. Lưu ý là chỉ có "một tham chiếu mà tham chiếu đến một con trỏ"....

LẬP TRÌNH C nâng cao -BÀI 5 - TEMPLATE (TIẾP) part 1

tailieu.vn

template<class T>int search(T a[],int n,T key). template<class T>class Array. template<typename T>Array<T>::Array(int n). template<typename T>Array<T>::~Array(). template<typename T>void Array<T>::setValue(const T&. template<typename T>T&. Array<T>::getValue(int n). template<typename T>void Array<T>::makeArray(T *&arr,int n). Array<T>::operator[](int i). template<typename T>int Array<T>::seek(const T&. template<typename T>int Array<T>::search(const T* list,int size,const T key). operator<<(ostream&. os<<p.getAge()<<endl;. Array<Person>. cout<<a[2];. cout<<a.seek(Person(5))<<endl;. cout<<a.search(b,4,Person(4))<<endl;. Bạn hãy thử viết toán tử output <<. Chuẩn bị một tập tin...

LẬP TRÌNH C nâng cao -BÀI 5 - TEMPLATE (TIẾP) part 2

tailieu.vn

#include "array.h". operator<<(ostream&. os<<p.getAge()<<endl;. Array<Person>. cout<<a;. cout<<equal(a,b)<<endl;. Giải thích: equal và operator<<. khi khai báo lại prototype của chúng lần thứ hai trong một class template (ở đây là class Array) ta phải có cái kí hiệu này <>. Array<T>&. Array<T>::Array(int size). void Array<T>::setValue(const T&. Array<T>::getValue(int i). Array<T>::Array(const Array<T>*&. Array<T>::operator=(const Array<T>*&. bool operator!=(const Array<T>&. a1,const Array<T>&. Array<string>. cout<<b.getValue(0)<<endl;. cout<<b.getValue(1)<<endl;

LẬP TRÌNH C nâng cao -BÀI 6 - TEMPLATE (TIẾP)

tailieu.vn

Trong bài trước chúng ta thấy một điều hơi là lạ, đó là file header array.h có chỉ thị #include file source array.cpp. Ví dụ nó gặp template<class T>. nó không thể biên dịch vì nó không biết kiểu dữ liệu của T.. Khi nó gặp instance đầu tiên của template, ví dụ template<int>. nó biên dịch và chúng ta...

LẬP TRÌNH C nâng cao -BÀI 8 _ STL - SEQUENTIAL CONTAINER

tailieu.vn

*Các iterator (các con trỏ dữ liệu) là các con trỏ để trỏ đến các phần tử trong các bộ lưu trữ. *Các algorithm (các thuật toán lưu trữ dữ liệu) là các hàm phổ biến để làm việc với các bộ lưu trữ như thêm, xóa, sửa, truy xuất,. *Các function object (các đối tượng hàm) là các hàm...

LẬP TRÌNH C nâng cao -BÀI 9 - ĐỐI TƯỢNG HÀM

tailieu.vn

Function object. Một function object (đối tượng hàm) là một object (đối tượng) được sử dụng như một function (hàm). -phải là một hàm thành viên, không phải là một hàm friend -không phải là một hàm static. cout<<i<<endl;. Instance của lớp này là một object được gọi là function object, là một object được sử dụng như một function....

LẬP TRÌNH C nâng cao -BÀI 10 - THƯ VIỆN FUNCTIONAL

tailieu.vn

(functor), được gọi là hạng (arity) của biểu thức hay đối tượng hàm đó Ví dụ. Unary (đơn nguyên, đơn phân, một toán hạng, một ngôi) n! (giai thừa của n) là một unary operator. n! là một unary expression, chỉ bao gồm một unary operator int giaithua(int n) là một unary function. là một unary functor Binary (nhị nguyên,...

LẬP TRÌNH C nâng cao -BÀI 11 – CÁC BỘ LƯU TRỮ LIÊN KẾT

tailieu.vn

map<char*,int>. ánh xạ từ một char* đến một int map<char*,int>. cout<<mapInt["one"];. operator<<(ostream&. os<<p.name;return os;. typedef map<string,Person>. for(MPCI i=mp.begin();i!=mp.end();++i) cout<<(*i).first<<". "<<(*i).second<<endl;. mapPerson.insert(MPVT("one",p));. mapPerson.insert(pair<string,Person>("two",Person("Nam")));. mapPerson.insert(pr);. như map<key K,value V>. thì có 3 argument là map<key K,value V,comparator C>. typedef map<Person,int, comparePerson>. if(i==pMap.end. pMap.insert(MAP::value_type(d,1));. typedef map<string,Person,comparePerson>. mapPerson.insert(pair<string,Person>("one",Person("Nam")));. mapPerson.insert(pair<string,Person>("two",Person("Viet")));. mapPerson.insert(pair<string,Person>("three",Person("An")));. map<char*,int,less<char*>. typedef map<char*,int>. typedef map<char*,int>::iterator MII;. for(MII i=m.begin();i!=m.end();++i) cout<<(*i).first<<". return (*a).age>(*b).age;. for(LP::const_iterator i=p.begin();i!=p.end();++i) cout<<(**i).age;....

LẬP TRÌNH C nâng cao -BÀI 12 - CÁC BỘ TƯƠNG THÍCH VÀ CÁC THƯ VIỆN KHÁC

tailieu.vn

Yêu cầu trước khi đọc: học xong Lập trình C/C++ căn bản BÀI 12: CÁC BỘ TƯƠNG THÍCH VÀ CÁC THƯ VIỆN KHÁC. Các bộ tương thích lưu trữ, dưới đây gọi là các bộ tương thích, làm các bộ lưu trữ khác trở nên tương thích với nó bằng cách đóng. gói (encapsulate) các bộ lưu trữ khác trở...

LẬP TRÌNH C nâng cao -BÀI 13 - RTTI, EXTERN VÀ PREPROCESSOR DIRECTIVE part 2

tailieu.vn

#define: định nghĩa một macro (quá dễ rồi). #undef: hủy bỏ định nghĩa một macro, macro đó có thể định nghĩa lại bằng. #error: định nghĩa câu thông báo khi gặp lỗi, ví dụ CODE. Câu thông báo lỗi sẽ là câu ta đã định nghĩa. #if MAX_WIDTH>10. #undef MAX_WIDTH. #define MAX_WIDTH 10. #elsif MAX_WIDTH<1. #defines MAX_WIDTH 1. #defines MAX_WIDTH...

LẬP TRÌNH C nâng cao -BÀI 13 - RTTI, EXTERN VÀ PREPROCESSOR DIRECTIVE part 1

tailieu.vn

nếu ta muốn overload toán tử xuất <<. tử nhập >>. os) const{os<<*this;}. is){is>>*this;}. operator<<(ostream&. p.printToFile(dynamic_cast<ofstream&>(os));//downcast else os<<p.getName()<<endl;. operator<<(ofstream&. ofs<<p.getName()<<endl;. operator>>(istream&. operator>>(ifstream&. ifs>>temp;. cin>>a;. ofstream ofs("a.txt");. ofs<<a;. cout<<a;. ifstream ifs("a.txt");. ifs>>b;. ofs.open("b.txt");. ofs<<b;. cout<<b;. Đọc toàn bộ tập tin vào một chuỗi, sử dụng filebuf trong <fstream>. filebuf (file buffer) bộ đệm tập tin. ostringstream output;output<<s<<d<<n;. istringstream values(input);values>>s1>>s2>>d>>n;. istringstream values(s);values>>s1>>s2>>s3>>...;....

LẬP TRÌNH C nâng cao -BÀI 14 - DESTRUCTOR, CONSTRUCTOR, CONVERSION VÀ DEBUG part 2

tailieu.vn

void* operator new(unsigned int size). cout<<"new"<<endl;. cout<<"delete"<<endl;. void* operator new[](unsigned int size). cout<<"new[]"<<endl;. cout<<"delete[]"<<endl;. cout<<(*a).data<,endl;. cout<<b[i].data;. void* operator new(unsigned int size,char* file,int line). memInfo.address = ptr;. memInfo.size = size;. strcpy(memInfo.file,file);. memInfo.line = line;. void* operator new[](unsigned int size,char* file,int line). cout<<"Address:"<<memInfo.address<<endl;. cout<<"Size:"<<memInfo.size<<endl;. cout<<"File:"<<memInfo.file<<endl;. cout<<"Line:"<<memInfo.line<<endl;

LẬP TRÌNH C nâng cao -BÀI 14 - DESTRUCTOR, CONSTRUCTOR, CONVERSION VÀ DEBUG part 1

tailieu.vn

operator<<(ostream&. t){os<<t.num;}. t){cout<<t<<endl;}. cout<<v<<endl;//v trả về char*. v(4);//v là functor cout<<i<<endl;. cout<<"Bugs in "<<__FILE__<<". at line "<<__LINE__<<endl;. #define BUG cout<<"Bugs in "<<__FILE__<<". at line "<<__LINE__<<endl. int** pp = &p;cout<<**pp;. if(p!=NULL){int** pp = &p;cout<<**pp;}. r = *p;cout<<r;. r = *p;cout<<r;}. cout<<a[6]<<endl;. cerr<<ex.what();. cout<<"Address:"<<memInfo.address<<endl;. cout<<"Size:"<<memInfo.size<<endl;. cout<<"File:"<<memInfo.file<<endl;. cout<<"Line:"<<memInfo.line<<endl;

LẬP TRÌNH C nâng cao -BÀI 15 - AUTO_PTR, MUTABLE, VOLATILE VÀ ĐÁNH GIÁ TỐC ĐỘ CHƯƠNG TRÌNH

tailieu.vn

BÀI 15: AUTO_PTR, MUTABLE, VOLATILE VÀ ĐÁNH GIÁ TỐC ĐỘ CHƯƠNG TRÌNH. auto_ptr. có định nghĩa lớp auto_ptr (nghĩa là con trỏ cấp phát và hủy bỏ vùng nhớ tự động) để giải quyết vấn đề. Trong ví dụ dưới đây, p trỏ đến a (gọi là p sở hữu a) Bạn không cần gọi delete a Khi chương trình...