学习目标:
- 掌握 c++入门知识
学习内容:
基本语法
学习时间:
6.25 周日早上
学习记录:
借助了c++的一些STL容器和一些库函数,scanf printf换成cin cout等等,更加省时省力....
一、基本语法
1.标准库
C库 | C++库 |
---|---|
stdio.h | cstdio |
math.h | cmath |
string.h | cstring |
malloc.h(特别的) | malloc.h |
2.基本格式
#include <iostream>//其实就是c的#include <stdio.h>
#include <bits/stdc++.h>//万能头文件,强烈建议必写
using namespace std;//必写,声明使用std名称空间,不用理解
int main()//主框架和c一样
{
return 0;
}
3.输入输出、变量声明
输入:cin 和 >>(cin 不可以读空格 cin >> s,输入a b,只能录入a)
输出:cout 和 <<
变量声明:可以直接在for 循环中定义
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;//输入,就是这个格式 cin和>>
cout<<a<<b;//输出,就是这个格式 cout和<<
int c[10];
for(int i=1;i<=10;i++)//变量声明也很简单,可以在for前面定义i,
//也可以直接在for循环后面定义i,和c就这点不一样
{
cin>>c[i];//输入
}
for(int i=1;i<=10;i++)
{
cout<<c[i]<<endl;//输出,endl就是c中的\n,换行用的
}
return 0;
}
4.bool变量和const定义变量
bool变量:0为false , 其余为true
const :类似于c中的宏定义#define
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
const int MAX=100;//相当于#define int MAX 100
cout<<MAX;
return 0;
}
5. string 类:字符串
主要操作:
字符串合并 s=s1+s2
整行读取:getline(cin,s)
字符串长度:s.length()
字符串截取 s.substr(n,m) , s.substr(n)
.......
6.引用(&):C++中的&与取地址无关
引用会改变变量的值
原文链接: