C++

在这里插入图片描述

学习方法

第一遍力求看懂,
第二遍开始抄代码,练习,
第三遍开始总结写博客。

C++学习参考路线:

个人笔记

C++ learning note

推荐书目:

学习教程

 
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<typename T>
//模板
T add(T x, T y) {
 
	return x + y;
}
//类
class MyStruct
{
public:
	
	//构造函数
	MyStruct(string n, double s) {
		name = n;
		score = s;
	}
	
	void show() {
 
		cout << name << "  "<<score << endl;
	}//成员函数类内部实现
	void ex_show();
	
private:
	double score;
	string name;
};
void MyStruct::ex_show() {
	//::域名操作符(域:简单理解类MyStruct的域)
	printf("类外部实现成员函数\n");
}
 
double f(const double& d) {
	//引用传递,比值传递更高效,少了内存
 
	return 2 * d;
}
 
int main() {
 
	int a = 9;
	int& b = a;//引用类型
 
	int m = 99;
	int* n;
	n = &m;//指针
	string s = "hello";//字符对象
 
	vector <int> array = { 2,3,4,5 };
/*向量,类似数组
	(数组不可动态添加),可以动态添加
*/
	int* k = new int;
	//堆区,动态分内存类似malloc
	*k = 100;
 
	double D=78.3;//引用测试
 
	//模板解决数据类型问题
	printf("%f\n", add(3.2, 4.3));
	printf("%d\n", add(2, 4));
 
 
	printf("%d %d\n", a, b);
	printf("hello\n");
 
	cout << s.size() << endl;
 
	cout << array.size() << endl;
 
	printf("%d\n", *n);
 
	cout << *k << endl;
 
	delete k;
	//释放内存
 
	
	MyStruct OBJ("guo", 99.9);
	OBJ.show();
	OBJ.ex_show();
	
 
	cout << f(D) << endl;
	return 0;
}
 
//泛型 容器 迭代器
void MainWindow::on_pushButton_clicked(bool checked)
{
    int i=0,j=0;
    QString str;
    if(!checked){
        //容器
 
        QList<QString> obj;//顺序容器
 
        QMap<QString,int> map;//关联容器
/*****************************************************/
        //迭代器例子
        QList<QString> list;//容器
        list<<"a"<<"b"<<"c"<<"d";//添加数据
        QListIterator<QString> n (list);//顺序迭代器遍历
        while (n.hasNext()) {
            qDebug()<<n.next();
        }
        qDebug()<<"**********************";
        QMap<QString,QString> Map;
        Map.insert("1","1");
        Map.insert("2","3");
        Map.insert("3","3");
        Map.insert("4","3");
        QMutableMapIterator<QString,QString> m (Map);//关联迭代器遍历
 
        while (m.hasNext()) {
            if(m.next().key().endsWith("1"))
                m.remove();//key值与指定的value值相同删除
 
        }
        qDebug()<<Map;
        qDebug()<<"**********************";
/*****************************************************/
        obj.append("hello");
        obj.append("hello");//追加数据
        obj.insert(0,"world");
 
        map["one"]=1;
        map["two"]=9;
 
        j=map["two"];
        qDebug()<<j;
        map.insert("88",0);
 
        qDebug()<<map;
/*****************************************************/
        i=obj.size();
        str=obj.at(0);
        qDebug()<<obj;
        qDebug()<<i;
        qDebug()<<str;
    }
}

参考

【C++】一文入门C++

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值