11月14:
33章节类模板和泛型
#include <iostream>
#include <iomanip>
using namespace std;
template<class T>
class Queue
{
struct Node
{
T a;
Node * next;
};
public:
Queue();
void push(T b);
void pop();
T getlength();
virtual void print();
private:
Node * head;
Node * rear;
};
template<class T>
void Queue<T>::push(T b)
{
Node *p1 = new Node;
p1->a = b;
p1->next = NULL;
rear->next = p1;
rear = p1;
head->a++;
cout << setw(2) << b << setw(2) << " 进入队列 " << endl;
}
template<class T>
void Queue<T>::pop()
{
Node *p;
p = head->next;
cout << " " << setw(2) << p->a << setw(2) << "出队" << endl;
head->next = p->next;
delete p;
head->a--;
}
template<class T>
T Queue<T>::getlength()
{
return head->a;
}
template<class T>
void Queue<T>::print()
{
Node *p;
p = head->next;
cout << "队列中的元素是" << endl;
while (p)
{
cout << p->a << " -> ";
p = p->next;
}
cout << "NULL" << endl;
}
template<class T>
Queue<T>::Queue()
{
rear = head = new Node();
}
#include <iostream>
#include "q.h"
using namespace std;
int main()
{
Queue<int> q;
q.push(10);
q.push(20);
q.push(30);
q.print();
system("pause");
}
34章节:顺序队列
#ifndef _顺序队列_H
#define _顺序队列_H
template<class T>
class Queue
{
public:
Queue(int queueCapacity = 10);
bool IsEmpty() const;
T& Front() const;//读取队首的数据 函数的引用
T& Rear() const;
void Push(const T& item);
void Pop();
private:
T *queue;
int front;
int rear;
int capacity;
};
template<class T>
Queue<T>::Queue(int queueCapacity)// :capacity(queueCapacity)//初始化构造函数
{
capacity = queueCapacity;
if (capacity < 1)
throw "Queue capacity must be > 0";
queue = new T[capacity];
front = rear = 0;
}
template<class T>
inline bool Queue<T>::IsEmpty() const
{
return front == rear;
}
template<class T>
void Queue<T>::Push(const T &item)
{
/*if (rear == capacity - 1)
rear = 0;
else
rear++;
*/
if ((rear + 1) % capacity == front)//队列满
{
//加倍
}
rear = (rear + 1) % capacity;
queue[rear] = item;
}
template<class T>
inline T& Queue<T>::Front() const
{
if (IsEmpty())
throw "Queue is empty. No front element.";
return queue[front + 1 % capacity];
}
template<class T>
inline T&Queue<T>::Rear() const
{
if (IsEmpty()) throw "Queue is empty. No rear element.";
return queue[rear];
}
#endif
1114cppprimer限定符const
在一个cpp里有一个全局变量的话,那么在新建的第二个cpp文件里要调用此变量,则要加extern const int bufSize;在第一个和第二个cpp里都要加,但是普通的全局变量在定义里不需要加extern。
1114枚举
定义枚举:定义一个枚举变量比如 aa bb; 则bb只能初始化为枚举类型aa里面的三个值分别是bb=1,bb=w和bb=猪。
enum aa {1, w, 猪};
等价于enum aa
{
1,
w,
猪
};
1114引用(别名)
1. 定义引用的时候,必须初始化。因为要定义是谁的别名。
2. 定义引用的时候不能引用一个常量 比如:int &a = 0;
3. 可以在同一行定义多个引用 int &b = i, &c = i2;和 int i3=10;&ri=i3;
4. const的引用只能引用const的常量 const int ival = 1024;const int &refval = ival;
5. const引用可以直接被初始化一个常量,两个变量,不同类型 比如const int &r = 42; const int &r2 = r + i; double dval = 3.14; const int &r3 = dval;
1115文件流
ofstream outfile("text.txt");//写文件 流对象outfile和这个txt文件绑定了
outfile << "Hello File!";
outfile.close();
string file("one.txt");
//ifstream infile(file.c_str());
ifstream infile; //流对象infile没有和一个文件绑定 就要用open绑定
infile.open(file.c_str());
//if (infile)//检查打开文件成功
if (!infile)
{
cerr << "error: unable to open input file."
<< file << endl;
return -1;
}
string s;
while (infile >> s)
cout << "读到的内容是:" << s << endl; //读到的内容
infile.close();
infile.open("two.txt");
if (!infile)
{
cerr << "error: unable to open input file."
<< file << endl;
return -1;
}
while (infile >> s)
cout << s << endl;
infile.close();
1115cpp的string类
①4种初始化方法
string s1; string s2(“help”); string s3(s2); string s4(10, ‘a’);
string s5 = “hello”; 在c++里这样写是不好的方法 速度慢 先初始化s5再给s5赋值。 cout << s2;与cout <<”help”;不一样 一个是c++string类型 一个是c语言风格。
②string s; 用cin(开头所有的空白字符,如换行空格,会自动忽略,读取完一个字符串到最后的空格为止,空格之后的字符不读)写入的时候,写入bill gate, 那么只能读入bill, 因为遇到空格默认读完. 如果要整行读入,要写两个变量s1和s2.也可以连续输入
③如何读取一整行?
除了以上用几个变量表示,还可以用getline(cin, name);代替cin>>name;
1123this指针(指向当前的对象)
Person p("zhangfei", "street 1");
public:
Person(const string &nm, const string &addr)
{
this->name = nm; //this->name = name; 写上this才能这么写 否则系统认不出
this->address = addr; //this->address = addrsss;
}
string getName() const
{
return this->name; //this指向当前的对象 p是张飞 这个this就指向p
}
1123定义类(默认私有量)
使用类的时候,先定义对象,比如Person a; a.name;
一般只读的函数要加const.这样更安全更保险.因为不会去修改数据.