您的位置:首页 > 运维架构 > Linux

linux学习杂记_c到c++

2009-08-25 23:42 363 查看
一个简单的C++程序:
#include <iostream>
using namespace std;

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;

int *a = new int[10] 和 int *a = new int(10) 的区别;

C++函数允许给参数指定缺省值, 有两个限制条件:
1. 缺省值只能出现在函数申明中, 不能出现在定义体中;
2. 如果有多个参数, 只能从后往前逐个缺省;
void foo(int x=0, int y=0);
void foo(int x, int y)
{
. . .
}

函数overload:
对于功能相同或相似的函数的管理, 同一函数名, 不同的函数参数类型, C++内部有一种叫做name mangling的机制来重命名同名函数;

运算符重载:
如: complex operator +(const complex &a, const complex &b);

头文件:
C++ 和C兼容, 用C写的程序库为了能够和C++程序连接, 所提供的头文件采用这样的开头结尾:
#ifdef _cplusplus
extern "C" {
#endif
. . .
#ifdef _cplusplus
}
#endif
C++对于括在extern "C" {}之间的代码不使用name mangling, 以保证于C的函数名匹配;
C++将C标准库的头文件另外作了包转, 比如将stdio.h包装成cstdio等;
#include <cstdio>

inline函数:
C++中用inline函数来取代C中类似函数的宏定义. 在调用一个内联函数是, 编译器像处理普通函数一样首先检查调用是否正确, 如果正确, 内联函数的代码就会直接替换函数调用, 省去了调用的开销;
使用时应注意inline关键字应该和定义放在一起, 而不是申明在一起;
void foo(int x, int y);
inline void foo(int x, int y);
{
. . .
}

在C语言编程时, 根据模块化和局部化的原则, 一般把联系密切的变量和函数放在一个.c文件中, 文件内部使用的变量和函数则可申明为static的; 在面向对象的编程中, 把紧密联系的变量和函数封装在一个class里面, 称为成员变量和成员函数, 对类内部使用的变量和函数可申明为private的, 允许外界访问的变量和函数申明为public的.

类是一种类型定义, 是对C的struct的一种扩展, 定义了类型之后, 就可以定义这种类型的变量, 称为类的实例或对象; C++中, struct和class的用法类似, 区别是, struct默认是public的, 而class默认是private的.

类的成员函数可以在类的内部申明, 被认为是inline的, 也可以在外面定义, 需要使用类名称限定符, ClassA: : func( ); 用nm查看编译好的程序符号表, name mangling方式将类名, 函数名, 和参数列表合在一起;

类的成员函数有个隐含参数->this指针; 假如一个类定义了多个对象, 则每个对象各自有一份该类的数据成员, 共享位于代码段的成员函数;
类的static成员变量是全局的, 所有对象共享一份, static成员变量可以用对象名访问, 也可以直接用类名访问;
如: 定义对象A a; a.i 及A: : i都可以访问i变量.
类的static成员函数并没有隐含的参数this指针, static成员函数只能访问static成员变量;

运算符重载:
按惯例, 对于+, -这样的对称运算以非成员函数的形式重载, 对于赋值运算和单目运算符, 则以成员函数形式重载; 其中的左操作数就是隐含的this指针;

继承:
如果A是基类, B是A的派生类, 那么B将继承A的数据和函数. B包含一个A, 可以再加上自己的一些东西. B定义的A的同名函数将覆盖(override) A的函数;

多态:
成员函数可以申明为virtual虚函数的, 则可呈现多态性. (? 对于基类指针指向的虚函数, 会优先调用子类的函数) .如果一个成员函数申明为virtual并且在末尾加上=0, 称为纯虚函数, 拥有纯虚函数的类不能实列化, 只能被其他类继承;

封装, 继承, 多态是面向对象方法的三大特征.

系统会默认合成缺省的构造函数, 析构函数, 赋值函数, Big-Three:
每个类只有一个析构函数和一个赋值函数, 但可以有多个构造函数(包含一个拷贝构造函数, 其他的称为普通构造函数).
A( void); //缺省的无参数构造函数
A( const A &a); //缺省的拷贝构造函数
~A( void ); //缺省的析构函数
A &operator = (const A &a); //缺省的赋值函数
构造函数和析构函数是没有返回值的. new调用构造函数, delete调用析构函数;

构造函数有一种特殊的初始化方式叫初始化表. 初始化表位于函数参数表之后, 却在函数体{}之前.
如果类存在继承关系, 派生类必须在其初始化表里调用基类的构造函数. 类成员的初始化可以在初始化列表中进行, 也可以在构造函数体中赋值; 初始化列表中效率较高. const成员必须在初始化列表中初始化;
构造时先初始化基类, 再逐层向下初始化, 析构的过程正好相反;

拷贝构造函数A(const A &a)具有构造和赋值双重作用;

const修饰一个成员函数, 表示该函数不改写当前对象的内容; ???
const修饰一个成员变量, 在对象初始化时为这个变量赋值一个常数, 此后不能更改; 不同实例该变量值可以不同;

模板template与泛形generic:
C++支持很多不同的数据类型:
内建类型: int char float double 等
指针, 数组
class和struct
为了写通用的函数使之作用于各种类型的数据, C++提供了模板机制, 可以定义模板函数和模板类: template< >;
<>里面是模板参数, 不仅可以传变量和值, 还可以传类型:
template <class T, int max> struct Buff
{
T v[max]
};
Buff <int, 10> intBuff10;

template <class T> T power (T a, int exp)
{
T ans = a;
while (--exp > 0) {
ans *= a;
}
return (ans);
}
模板本质上就是一种宏定义. 模板类和模板函数的定义都应该放在头文件中.
C++的STL标准模板库中有很多模板类, 比如vector<T>, list<T>, 称为容器container. 我们可以象使用数组一样使用容器, 而且容器可以检查访问越界, 并且长度可以动态增长.

作用范围: private public protected子类才能看到

子类构造中要先构造父类;
有一个纯虚函数的类就是抽象类;

C++错误汇集:
构造函数不需要返回类型;
类定义最后有' ; ' 结尾;
类名称定义和使用不一致, 大小写有误;
关于+重载:
友元, operator +(&m1, &m2); 需要提供2个参数;
内部, operator +(&m); 默认调用这本身this就是被加数;
内部函数可以调用类的所有成员, 友元函数可以调用部分成员// 私有成员可以吗?;
友元函数不是成员函数, 不能直接访问类成员, 但可访问所传进类参数的私有成员;
只有内部函数才能直接应用类的成员变量;
void类型的函数, 不能这样用: cout << m.transpose( );
rand( )提供的随机数太接近, sleep(1)解决;
Matrix. pout( )这是不需返回的, 却加上返回类型, 会出错, 用void即可;
Matrix c = a + b必须重载=, +;
<< 只能放在外边用友元函数定义; operator (cout, &m); 左参数不是自定义的类型;
Matrix a = b; 调用 a( b );
Matrix a; a = b; 调用 =重载;
若有Matrix( const Matrix &m), 则 Matrix operator = (const Matrix &m) 正确;
否则要用 Matrix &operator = ( const Matrix &m);
何时返回引用, 如何返回引用, 返回引用须为参数之一???
return返回引用调用了拷贝构造函数;
cout << f << e << endl 从左往右处理;
返回函数的临时变量的引用是错误的, 因为退出函数, 临时变量的生命也结束了;
返回引用必须是参数之一的引用才行, 但要被修改的参数不能用const修饰, 最好是左参数???
Matrix k = m; 调用拷贝构造函数;
Matrix k = m + n; 调用加号后返回值直接由系统赋值拷贝给k, 不调=, 也不调copy;
k = m + n; 若要考虑不同维数的赋值, 则要删除原有空间, 重开新空间; 调用+, 再调用=, 所以=中不需要删除, 也 无需创建新空间, 直接使用调用者空间;
注意: 运算符重载时, 函数定义在类内部, 则调用者本身作为左边的运算单元, 右边的部分作为参数传进. 若作为友 元函数, 则需传进左, 右参数;
返回引用必须是传进的参数, 在类的内部函数中, 调用者本身也算是传进的参数, 因而可以返回引用;
若类成员变量有指针, 则一定要写构造函数和析构函数;
若不定义拷贝构造函数, 系统默认将成员一一复制;
Stack s1 = 100; 隐式类型转换, 调用s1 ( 100 );
若函数传进类型是const char *st, 那么函数内部接受该串的串也必须是const char *类型;

C++的一些例程:
/*complex.cc*/

#include <iostream>
using namespace std;

class Complex
{
private:
double r_, i_;

public:
Complex(double r = 0.0, double i = 0.0)
: r_(r), i_(i) {}
~Complex(){}
//add(Complex c);
//sub(Complex c);
friend Complex operator+\
(const Complex &c, const Complex &d);
friend Complex operator-\
(const Complex &c, const Complex &d);
friend ostream &operator<<\
(ostream &o, const Complex &c);
/*
Complex operator+(const Complex &c)
{
r_ += c.r_;
i_ += c.i_;
return *this;
}
Complex operator-(const Complex &c)
{
Complex d;
d.r_ = r_ - c.r_;
d.i_ = i_ - c.i_;
return d;
}*/
double R(void) const
{return r_;}

double I(void) const
{return i_;}

void R(double r) { r_ = r; }
void I(double i) { i_ = i; }
};
Complex operator+\
(const Complex &c, const Complex &d)
{
Complex e;
e.r_ = c.r_ + d.r_;
e.i_ = c.i_ + d.i_;
return e;
}
Complex operator-\
(const Complex &c, const Complex &d)
{
Complex e;
e.r_ = c.r_ - d.r_;
e.i_ = c.i_ - d.i_;
return e;
}
ostream &operator<<\
(ostream &o, const Complex &c)
{
return o << c.r_ << " + i" << c.i_;
}
int main()
{
Complex c(1.1, 3.5);
Complex d(2.8, 6.4);
cout << c.R() << ", " << c.I() << endl;
cout << d.R() << ", " << d.I() << endl;
Complex e = c + d;
//cout << e.R() << ", " << e.I() << endl;
cout << e << endl;
e = d - c;
//cout << e.R() << ", " << e.I() << endl;
cout << e << endl;
return 0;
}

/*matrix.cc*/

#include <iostream>
#include <time.h>

using namespace std;

class Matrix
{
double **matrix_;
int n_;

public:
Matrix(int n=10) : n_(n)
{
matrix_ = new double* [n_];
for (int i=0; i<n_; i++)
matrix_[i] = new double [n_];
srand(time(NULL));
for (int i=0; i<n_; i++)
for (int j=0; j<n_; j++)
matrix_[i][j] = rand()%10;
}
~Matrix()
{
for (int i=0; i<n_; i++)
delete [] matrix_[i];
delete [] matrix_;

}

Matrix(const Matrix &m)
{
n_ = m.n_;
matrix_ = new double*[n_];
for (int i=0; i<n_; i++)
{
matrix_[i] = new double [n_];
for (int j=0; j<n_; 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<n_; i++)
{
matrix_[i] = new double [n_];
for (int j=0; j<n_; j++)
{
matrix_[i][j] = m.matrix_[i][j];
}
}
return *this;
}

void transpose()
{
Matrix temp(n_);
for (int i=0; i<n_; i++)
{
for (int j=0; j<n_; j++)
{
temp.matrix_[i][j] = matrix_[j][i];
}
}
for (int i=0; i<n_; i++)
{
for (int j=0; j<n_; j++)
{
matrix_[i][j] = temp.matrix_[i][j];
}
}

}
friend Matrix operator+\
(const Matrix &m1, const Matrix &m2);
friend Matrix operator*\
(const Matrix &m1, const Matrix &m2);
friend ostream &operator<<\
(ostream &o, const Matrix &m);
};

Matrix operator*\
(const Matrix &m1, const Matrix &m2)
{
Matrix temp(m1.n_);
for (int i=0; i<m1.n_; i++)
{
for (int j=0; j<m1.n_; j++)
{
temp.matrix_[i][j] = 0;
for (int k=0; k<m1.n_; 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<m1.n_; i++)
{
for (int j=0; j<m1.n_; 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<m.n_; i++)
{
for (int j=0; j<m.n_; 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 <iostream>
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<top_; 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<top_; 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<N; i++)
{
S1.push(i);
}
/*
for (int i=0; i<N/2; i++)
{
cout << S1.pop() << endl;
}*/
Stack S2(S1);
while(!S2.empty())
{
cout << S2.pop() << endl;
}
return 0;
}

/*tstack.cc
*class stack with template
*/

#include <iostream>
#include <stdio.h>
using namespace std;
template <class T>
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<top_; 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<top_; 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<char> S1;
for (int i=0; i<N; i++)
{
S1.push(i);
}
/*
for (int i=0; i<N/2; i++)
{
cout << S1.pop() << endl;
}*/
Stack<char> S2(S1);
while(!S2.empty())
{
printf ("%c\n", S2.pop()+'0');
//cout << S2.pop()+'0' << endl;
}
return 0;
}

mystring : main.cpp mystring.h
g++ -o mystring main.cpp
clean:
rm -f mystring

//main.cpp

#include "mystring.h"

int main()
{
MyString str("hello");
MyString str1 = str;
cout<<"Length of str1: "<<str1.length()<<endl;
cout<<"Value of str1: "<<str1<<endl;

MyString str2 = "";
cout<<"Is str2 empty? "<<(str2.isEmpty()?"true":"false")<<endl;

str2 = " world";
cout<<"Now value of str2: "<<str2<<endl;

cout<<"Now value of str: "<<str1+str2<<endl;
cout<<"Value of another expression: "<<str1 + "_world"<<endl;
cout<<"Value of yet another expression: "<<"Hello," + str2<<endl;

MyString str3 = str1 + str2;
cout<<"Value of str3: "<<str3<<endl;

return 0;
}

//mystring.h
#include <iostream>
#include <cstring>
using namespace std;

class MyString
{
friend ostream& operator<<(ostream&, const MyString&);
friend MyString operator+(const MyString&, const MyString&);
public:
MyString(const char*);
MyString(const MyString&);
~MyString(void);
MyString& operator=(const MyString&);
int length(void) const;
bool isEmpty(void) const;
private:
char *data;
int len; //don't include '\0'
};
MyString::MyString(const char* str="")
{
char* cp;
const char* cs;
len = strlen(str);
data = new char [len + 1];
cp = data;
cs = str;
while (*cs)
*cp++ = *cs++;
*cp = '\0';
}
MyString::MyString(const MyString& mstr)
{
char* cp;
char* cs;
len = mstr.len;
data = new char [len + 1];
cp = data;
cs = mstr.data;
while (*cs)
*cp++ = *cs++;
*cp = '\0';
}
MyString::~MyString(void)
{
delete [] data;
}

MyString& MyString::operator=(const MyString& mstr)
{
char* cp;
char* cs;
if (this == &mstr) return *this;
delete [] data;
len = mstr.len;
data = new char [len + 1];
cp = data;
cs = mstr.data;
while (*cs)
*cp++ = *cs++;
*cp = '\0';
return *this;
}
int MyString::length(void) const
{
return len;
}
bool MyString::isEmpty(void) const
{
if (len == 0)
return true;
else
return false;
}
ostream& operator<<(ostream& o, const MyString& mstr)
{
return o << mstr.data;
}
MyString operator+(const MyString& ma, const MyString& mb)
{
char *cp, *cs;
MyString addrs;
addrs.len = ma.len + mb.len;
delete [] addrs.data;
addrs.data = new char [addrs.len+1];
cp = addrs.data;
cs = ma.data;
while (*cs)
*cp++ = *cs++;
cs = mb.data;
while (*cs)
*cp++ = *cs++;
*cp = '\0';
return addrs;
}

/*list.cc*/

#include <iostream>
#include <cstdlib>
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<n_; i++)
list_[i] = rand() % n_;
}
~List()
{
delete [] list_;
cout << "List dtor" << endl;
}
virtual void show() const
{
for (int i=0; i<n_; 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]<list_[l])
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<n_; 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 <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

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;
}
};

void *producer(void *arg)
{
monitor *mon = (class monitor *)arg;
while (1) { mon->put('a'); sleep(1); }
return NULL;
}
void *consumer(void *arg)
{
monitor *mon = (class monitor *)arg;
while (1) { mon->get(); sleep(1); }
return NULL;
}

int main()
{
monitor *mon = new monitor;
pthread_t tid[3];
void *retval;

pthread_create(&tid[0], NULL, producer, (void*)mon);
pthread_create(&tid[1], NULL, producer, (void*)mon);
pthread_create(&tid[2], NULL, consumer, (void*)mon);

pthread_join(tid[0], &retval);
pthread_join(tid[1], &retval);
pthread_join(tid[2], &retval);

delete mon;
return 0;
}

/*stack.h*/

#include <iostream>
using namespace std;

template<class T> class Node
{
public:
Node(T invalue):m_Value(invalue), m_Next(0) {}
~Node();

T getValue() const {return m_Value;}
void setValue(T value) {m_Value = value;}
Node<T>* getNext() const {return m_Next;}
void setNext(Node<T>* next) {m_Next = next;}
private:
T m_Value;
Node<T>* m_Next;
};

template<class T>Node<T>::~Node()
{
cout << m_Value << "deleted" << endl;
if (m_Next)
{
delete m_Next;
}
}

template<class T> class Stack
{
public:
Stack():m_Head(0), m_Count(0) {}
~Stack<T>() {delete m_Head;}
void push(const T& t);
T pop();
T top() const;
int count() const;

private:
Node<T> *m_Head;
int m_Count;

};

template <class T> void Stack<T>::push(const T& value)
{
Node<T> *newNode = new Node<T>(value);
newNode->setNext(m_Head);
m_Head = newNode;
++m_Count;
}

template <class T> T Stack<T>::pop()
{
Node<T> *popped = m_Head;
if (m_Head != 0)
{
m_Head = m_Head->getNext();
T retval = popped->getValue();
popped->setNext(0);
delete popped;
--m_Count;
return retval;
}
return 0;

}

template <class T> inline T Stack<T>::top() const
{
return m_Head->getValue();
}

template <class T> inline int Stack<T>::count() const
{
return m_Count;
}

/*main.cpp*/

#include "stack.h"

int main()
{
Stack<int> intstack1, intstack2;
int val;
for (val = 0; val < 4; ++val)
{
intstack1.push(val);
intstack2.push(2 * val);
}
while (intstack1.count())
{
val = intstack1.pop();
cout << val << endl;
}
Stack<char> stringstack;
stringstack.push('A');
stringstack.push('B');
stringstack.push('C');
char val2;
while (stringstack.count())
{
// sleep(1);
val2 = stringstack.pop();
cout << val2 << " count=" << stringstack.count()<< endl;
}
cout << "Now intstack2 will self destruct." << endl;
return 0;
}

信息点滴:
? 如没有namespace, 就必须这么使用, 如cout: : cout ;
const stack &s 引用只传个地址, 不复制值, 而且希望值不被改变;
C++中一般不用define, 用const int N = 10之类的语句;
矩阵的乘法, A * B, 要求A的列数与B的行数相等;
Ostream &operator << (ostream &o, const Complex &c)
{
return o << c. r_ << " +i" << c. i_;
}
----------------------------------------
网络参考书: TCP/IP steven的书
arp -a 获取ip的mac地址
netstat -rn
tcpdump看网络传送包的信息;
float类型数据用%d显示, 结果是乱码;
vi中用shell命令: ! ls -l;
3.14 ->double 3.14f->float
nm *.o 查看符号表
栈里面top = 0表示没有元素, 有一个元素时top =1 ;
字符串比较不能直接用==, 用strcmp, 赋值用strcpy;
vi里面要将{}里面内容对齐->
可以将光标移到左括号的行, 命令模式下, 输入100 (只要大于括号直接行数即可), ==, 完成对齐!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: