CppPrimer自学(1)

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.这样更安全更保险.因为不会去修改数据.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LabVIEW自学网是一个为学习和使用LabVIEW软件的人提供资源和支持的网站。它提供了丰富的教程、视频和文档,帮助初学者快速上手LabVIEW,并提供进阶的教程和案例,帮助用户深入学习和掌握LabVIEW的各种功能和应用。 LabVIEW自学网的主要特点如下: 1. 多样化的学习资源:LabVIEW自学网提供了多种形式的学习资源,包括文字教程、视频教程和实例,以满足不同学习风格的人的需求。 2. 详细的教程和实例:LabVIEW自学网提供了详细的教程和实例,通过逐步引导和演示,帮助学习者了解LabVIEW的基本概念、编程技巧和常见应用场景。 3. 丰富的案例和项目:LabVIEW自学网提供了大量的案例和项目,涵盖了各个领域的应用,如控制系统、数据采集、图像处理等,帮助学习者将LabVIEW应用到实际项目中。 4. 提供帮助和支持:LabVIEW自学网设有在线论坛和问答平台,用户可以在这里提问、交流和解决问题,得到其他用户或专业人士的帮助和支持。 5. 更新和维护及时:LabVIEW自学网会持续更新和维护其内容,以保证学习者可以获取到最新的LabVIEW知识和信息。 通过LabVIEW自学网,用户可以高效学习和使用LabVIEW软件,从而在科研、工程和教学等领域中应用LabVIEW实现各种功能和项目。无论是初学者还是有一定经验的用户,LabVIEW自学网都是一个值得信赖的资源和社区,可以帮助用户快速提升LabVIEW的应用能力和解决实际问题的能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值