int main(void) { cout << "hello, world!" << endl; }
布尔类型: true or false; 引用类型: int m; int &n = m; n相当于m的别名, 对n的操作就是对m的操作; 引用与指针的不同: 1. 引用被创建的同时必须被初始化, 指针可以任何时候被初始化; 2. 不能有NULL引用, 引用必须与合法的存储单元关联, 指针可以是NULL; 3. 一旦引用被初始化, 就不能改变引用关系, 指针可以随时改变所指对象; 引用可以看成受控的指针; C++内部引用是用指针来实现的. 变量初始化: int i = 10; int i(10);
int *arraysize = new int; cin >> *arraysize; int *array = new int[*arraysize]; delete [] array; delete arraysize;
template T power (T a, int exp) { T ans = a; while (--exp > 0) { ans *= a; } return (ans); } 模板本质上就是一种宏定义. 模板类和模板函数的定义都应该放在头文件中. C++的STL标准模板库中有很多模板类, 比如vector, list, 称为容器container. 我们可以象使用数组一样使用容器, 而且容器可以检查访问越界, 并且长度可以动态增长.
public: Matrix(int n=10) : n_(n) { matrix_ = new double* [n_]; for (int i=0; i matrix_[i] = new double [n_]; srand(time(NULL)); for (int i=0; i for (int j=0; j matrix_[i][j] = rand()%10; } ~Matrix() { for (int i=0; i delete [] matrix_[i]; delete [] matrix_;
}
Matrix(const Matrix &m) { n_ = m.n_; matrix_ = new double*[n_]; for (int i=0; i { matrix_[i] = new double [n_]; for (int j=0; j { matrix_[i][j] = m.matrix_[i][j]; } } } Matrix operator=(const Matrix &m) { n_ = m.n_; matrix_ = new double*[n_]; for (int i=0; i { matrix_[i] = new double [n_]; for (int j=0; j { matrix_[i][j] = m.matrix_[i][j]; } } return *this; }
void transpose() { Matrix temp(n_); for (int i=0; i { for (int j=0; j { temp.matrix_[i][j] = matrix_[j][i]; } } for (int i=0; i { for (int j=0; j { matrix_[i][j] = temp.matrix_[i][j]; } }
Matrix operator*\ (const Matrix &m1, const Matrix &m2) { Matrix temp(m1.n_); for (int i=0; i { for (int j=0; j { temp.matrix_[i][j] = 0; for (int k=0; k { temp.matrix_[i][j] +=\ m1.matrix_[i][k] * m2.matrix_[k][j]; } } } return temp; }
Matrix operator+\ (const Matrix &m1, const Matrix &m2) { Matrix temp(m1.n_); for (int i=0; i { for (int j=0; j { temp.matrix_[i][j] = 0; temp.matrix_[i][j] =\ m1.matrix_[i][j] + m2.matrix_[i][j]; } } return temp; } ostream &operator<<\ (ostream &o, const Matrix &m) { for (int i=0; i { for (int j=0; j { o << m.matrix_[i][j] << " "; } o << endl; } return o; }
const int N = 5; int main() { Matrix m(N); sleep(1); Matrix n(N); Matrix k(N); k = n; cout << m << endl; cout << n << endl; cout << k << endl; cout << m + n << endl; cout << m * n << endl; m.transpose(); cout << m << endl; return 0; }
/*stack.cc*/
#include using namespace std;
class Stack { int *stack_; int n_; int top_; public: Stack(int n=1024) : n_(n) { //stack = malloc(n*sizeof(int)); stack_ = new int [n_]; top_ = 0; } ~Stack() { delete [] stack_;} Stack(const Stack &S) { n_ = S.n_; top_ = S.top_; stack_ = new int [n_]; for (int i=0; i stack_[i] = S.stack_[i]; } Stack operator=(const Stack &S) { n_ = S.n_; top_ = S.top_; stack_ = new int [n_]; for (int i=0; i stack_[i] = S.stack_[i]; return *this; } void push(int item); int pop(void); bool empty() const; }; void Stack::push(int item) { stack_[top_++] = item; } int Stack::pop(void) { return stack_[--top_]; } bool Stack::empty() const { return top_ == 0; }
const int N = 10; int main() { Stack S1; for (int i=0; i { S1.push(i); } /* for (int i=0; i { cout << S1.pop() << endl; }*/ Stack S2(S1); while(!S2.empty()) { cout << S2.pop() << endl; } return 0; }
/*tstack.cc *class stack with template */
#include #include using namespace std; template class Stack { T *stack_; int n_; int top_; public: Stack(int n=1024) : n_(n) { //stack = malloc(n*sizeof(int)); stack_ = new T [n_]; top_ = 0; } ~Stack() { delete [] stack_;} Stack(const Stack &S) { n_ = S.n_; top_ = S.top_; stack_ = new T [n_]; for (int i=0; i stack_[i] = S.stack_[i]; } Stack operator=(const Stack &S) { n_ = S.n_; top_ = S.top_; stack_ = new T [n_]; for (int i=0; i stack_[i] = S.stack_[i]; return *this; } void push(T item) { stack_[top_++] = item; } T pop(void) { return stack_[--top_]; } bool empty() const { return top_ == 0; } }; const int N = 10; int main() { Stack S1; for (int i=0; i { S1.push(i); } /* for (int i=0; i { cout << S1.pop() << endl; }*/ Stack S2(S1); while(!S2.empty()) { printf ("%c\n", S2.pop()+'0'); //cout << S2.pop()+'0' << endl; } return 0; }
#include #include using namespace std; class List { protected: int *list_; int n_;
public: List(int n=10) : n_(n) { list_ = new int [n_]; srand(time(NULL)); for (int i=0; i list_[i] = rand() % n_; } ~List() { delete [] list_; cout << "List dtor" << endl; } virtual void show() const { for (int i=0; i cout << list_[i] << ' '; cout << endl; } }; class OList : public List { private: void swap(int &a, int &b) { int c; c = a; a = b; b = c; } void Qsort(int l, int r) { if (l>=r) return; int k = partition(l, r); Qsort(l, k-1); Qsort(k+1, r); } int partition(int l, int r) { int i, j; for (i=l+1, j=l; i<=r; i++) if (list_[i] swap(list_[i], list_[++j]); swap(list_[l], list_[j]); return j; } public: OList(int n=8) : List(n) { } virtual void show() { Qsort(0, n_-1); for (int i=0; i cout << list_[i] << ' '; cout << endl; }
}; const int N = 8; int main() { List l(N); l.show(); sleep(1); OList o(N); o.show(); return 0; }
/*monitor.cpp *producer & comsumer */
#include #include #include #include
using namespace std;
#define QSIZE 8
class monitor { private: pthread_cond_t full; /* count the number of characters in the queue */ pthread_cond_t empty;/* count the number of empty slots */ pthread_mutex_t m; /* implements the critical section */ unsigned int iBuf; /* the tail of the circular queue */ unsigned int oBuf; /* the head of the circular queue */ int count; /* count characters */ char buf [QSIZE]; /* the circular queue */
public: monitor() { oBuf = 0; iBuf = 0; count=0; /* head and tail match when queue empty */ pthread_cond_init(&full, NULL); /* no characters stored yet */ pthread_cond_init(&empty, NULL); /* all character slots are empty */ pthread_mutex_init(&m, NULL); } ~monitor() { pthread_mutex_destroy(&m); } void put(char ch) /* add "ch" to circular queue; wait if full */ { pthread_mutex_lock(&m); while (count >= QSIZE) pthread_cond_wait(&empty, &m); /* is there an empty slot? */ buf[iBuf] = ch; /* store the character */ iBuf = (iBuf+1) % QSIZE; /* increment to QSIZE, then reset to 0 */ cout << "put to buf[" << iBuf << "]" << endl; count++; pthread_cond_signal(&full); /* a new character is available */ pthread_mutex_unlock(&m); } char get() /* remove "ch" from queue; wait if empty */ { char ch; pthread_mutex_lock(&m); while (count <= 0) pthread_cond_wait(&full, &m); /* is a character present? */ ch = buf[oBuf]; /* retrieve from the head of the queue */ oBuf = (oBuf+1) % QSIZE; cout << "get from buf[" << oBuf << "]" << endl; count--; pthread_cond_signal(&empty); /* signal existence of new empty slot */ pthread_mutex_unlock(&m); return ch; } };