学习方法
第一遍力求看懂,
第二遍开始抄代码,练习,
第三遍开始总结写博客。
C++学习参考路线:
- 小白 C++ 入门并发疯学习路线(书单)(简单的C++学习经验)
- 如何学好 Linux、C++,并搞定 BAT 面试(高屋建瓴,C++学习之路)
个人笔记
推荐书目:
- C++学习书籍推荐《Accelerated C++中文版》下载
- Five Popular Myths about C++, Part 1
- 《Unix/Linux编程实践教程》
- 《Linux/UNIX系统编程手册》
- 《C++ primer》(不是《C++ primer plus》)
- 《Linux C++服务端编程》
学习教程
#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;
}
}
参考