重要知识点总结,复习的时候用。
#include <iostream>
using namespace std;
int main()
{int a;cin >> a; //标准输入流cout << "hello ---cout" << " a的值:" << a << endl ; //标准输出流cerr << "hello ---cerr" << endl ; //标准错误流clog << "hello ---clog" << endl; //输出运行时的一般性消息cout << "66555 3 //右斜杠徐行作用。
ffffffffffffffffff
55" << endl;cout << "66555 3" //自动拼接"ffffffffffffffffff""55" << endl;return 0;
}
类型 | 最小尺寸 |
---|---|
bool | 未定义 |
char | 8位 |
wchar_t | 16位 |
char16_t | 16位 |
char32_t | 32位 |
short | 16位 |
int | 16位 |
long | 32位(win) |
long long | 64位 |
float | 6位有效数字 |
double | 10位有效数字 |
long double | * |
#include <iostream>
#include <wchar.h>
#include <locale.h>
#include <stdlib.h>
using namespace std;
int main()
{wchar_t c = L'在';setlocale(LC_ALL, "chs"); //设置中文wprintf(L"%lc", c);return 0;
}
#include <iostream>
using namespace std;
int main()
{//引用即别名int iValue = 1024;int &refValue = iValue;int &refValue2;//报错,引用必须初始化int i = 1024, i2 = 1024;//i和i2都是intint &r = i, r2 = i2;//r是引用,和i绑定在一起,r2是intint &iValue2 = 1024;//报错,引用类型的初始值必须是一个对象double d = 2.12;int &iValue3 = d; //iValue3初始值必须是int类型的对象//----------------------------指针练习-----------------------------------------------int *p1 = nullptr;int *p2 = 0;//初始化为字面常量0int *p3 = NULL;//可能需要引入 cstdlibcout << "这三个等价:" << p1 << "t" << p2 << "t" << p3 << endl;int iValue = 1;int *iPointer = &iValue;//iPointer是指向变量iValue的指针,iPointer的值是iValue的地址。//*iPointer才是iValue的值。*解引用。&是取地址。int *iPointer2 = iPointer;//指针之间赋值double *pi = 0;//pi的值暂理解为0地址。pi = iPointer2;//报错,类型不匹配//易混点---->取地址和引用。int obj = 0;void *v = &obj;//void *指向任意类型的指针,但不能直接操作它所指向的对象。如:cout << *v << endl;int *p3,p4;//p3是指针,p4是int。int *p5,*p6;//p5、p6都是指针。return 0;
}
#include <iostream>
using namespace std;
int main()
{//const引用int &r2 = 3;//报错,引用类型的初始值必须是对象。const int &r2 = 3;//加上const 允许这样做。int i = 1;const int &r1 = i;//允许将常量引用绑定到一个普通对象或常量对象上。//反之,不允许普通引用绑定常量对象。//const 指针const int i = 1;常量int j = 1;int *const iPointer2 = &j; //顶层const,iPointer2值不能改变,常量指针const int *const iPointer2 = &i;//左面的底层const,右面的顶层const,指向常量的常量指针const int p = 77;//顶层constconst int *iPointer3;//指向常量的指针,iPointer3值可以改变。iPointer3指向的变量的值不能改变。//赋值规则,和const引用思路差不多,自己体会。//总结:普通的*可以赋值给常量的*,常量的*不能赋值给普通的*,常量的*可以赋值给常量的*。return 0;
}
迭代器
vector<int> v = {1,2,3};
//begin()返回迭代器,指向第一个元素。
vector<int>::iterator iter = v.begin();
//迭代器 实际上是一个指针。
cout << *iter; //解引用,返回引用
string s("ddas");
for (string::iterator is = s.begin();is != s.end(); is++)
{cout << *is << endl;
}
vector<int> v(5,1);
for (vector<int>::iterator iter = v.begin(); iter != v.end(); iter++)
{cout << *iter << endl;//*iter 可以为左值。
}
for (vector<int>::const_iterator iter = v.begin(); iter != v.end(); iter++)
{cout << *iter << endl;//常量迭代器,*iter不可以为左值。
}
bitset
#include <bitset>
bitset<32> a;
cout << a << endl;
bitset<32> b(0xff);
cout << b << endl;
bitset<32> c(2);
cout << c << endl;
string str("1101111111111110101");
bitset<16> d(str,2,4);//初始化,从索引2开始,取4位。
cout << d << endl;
bitset<16> e(str,str.size() - 4);//取最后四位初始化
cout << e << endl;
a.any();//是否有1?
a.none();//是否没有1?
a.count();//1个数
a.size();//位数
bitset<16> e;
for(int iIndex = 0; iIndex != 16; iIndex += 2)
{e.set(iIndex);//某一位置1e[iIndex] = 1;//某一位置1
}
cout << e << endl;
e.set();//所有位置1
cout << e << endl;
e.reset(1);//索引0 置0
cout << e << endl;
e.flip();//全部反转。
e.flip(1);//某一位反转。bitset<16> e;
e.set();
bitset<16> a(e);
cout << (e & a) << endl;//按位运算
类型转换
static_cast //用于良性转换,一般不会导致意外发生,风险很低。
const_cast //用于 const 与非 const、volatile 与非 volatile 之间的转换。
reinterpret_cast//高度危险的转换,这种转换仅仅是对二进制位的重新解释
dynamic_cast//借助 RTTI,用于类型安全的向下转型(Downcasting)。
语法格式: xxx_cast<newType>(data)
语句&运算符
; // 空语句
{}//复合语句
//if 语句;switch 语句;while(condition);do while (condition);
//for (initializar; condition; expression);for (declaration : expression)
//break;continue;goto;return
异常&预处理
throw表达式语句,终止函数的执行。抛出一个异常,并把控制权转移到能处理该异常的最近的catch字句。
try和catch:try可能出现异常的语句块,catch捕获异常。
//预定义常量
cout << "文件:" << __FILE__
<< "n行号:" << __LINE__
<< "n日期:" << __LINE__
<< "n时间" << __DATE__
<< endl;
//调试信息输出
#ifndef NDEBUGcerr << " << endl;
#endif
附加选项:(项目-右键属性-C/C++-命令行-其他选项),添加 /DNDEBUG,屏蔽调试信息。
//断言
#include<cassert>
int i = 9;
assert(i == 9);
函数
非引用形参(传递的是copy)1、普通形参。2、指针形参。
void fun1(int x);
void fun1(int *x);
引用形参(传的是它自己)---->推荐
void fun1(int &x);
int v = 1;
int *p = &v;'
int *&rp = p;
void fun1(int *&rp);指针的引用
int v = 1;
int &ri = v;// 整型变量v的引用
int *p = &ri;//普通指针,并不是指向引用的指针。
注意点:不要使用普通的非引用vector形参。
推荐引用形参
void print(vector<string> &v);
但更好的是传递迭代器
void print(vector<string>::const_iterator beg,vector<string>::const_iterator end);
非静态全局变量的作用域是整个源程序,静态全局变量则限制了其作用域,即只在定义该变量的源文件内有效。
把局部变量改变为静态变量后是改变了它的存储方式即改变了它的生存期。
static函数与普通函数作用域不同。static函数仅在本文件中使用。
static全局变量与普通的全局变量有什么区别:static全局变量只初始化一次,防止在其他文件单元中被引用;
static局部变量和普通局部变量有什么区别:static局部变量只被初始化一次,下一次依据上一次结果值;
static函数与普通函数有什么区别:static函数在内存中只有一份(本文件中),普通函数在每个被调用中维持一份拷贝。
内联函数
inline int Max(int x, int y);
内联函数是通常与类一起使用。如果一个函数是内联的,那么在编译时,编译器会把该函数的代码副本放置在每个调用该函数的地方。
inline只是对编译器的建议,如果编译器发现指定的函数不适合内联就不会内联。
内联不适合的情况:递归、非常大的函数。
普通的函数在编译时被单独编译一个对象,包含在相应的目标文件中。目标文件链接时,函数调用被链接到该对象上。
内联函数没有内联会导致什么?
在调用该内联函数的目标文件中为该内联函数编译一个对象。若在多个文件调用了内联失败的函数,其中每个文件对应的目标文件中都会包含一份该内联函数的目标代码。目标代码的体积膨胀得与成功内联的目标代码一样,但目标代码的效率确和没内联一样。
重载函数:同一作用域内的几个函数名字相同但形参列表不通,我们称之为重载函数。
变量屏蔽&函数屏蔽
局部声明的变量名或函数名 和全局的一样时,会把全局的隐藏。
函数可以局部声明,但尽量不要这么做。
void a(int a = 9, int b = 9);//声明
void a(int a, int b) //定义
{cout << a;
}
a();//调用
void f(int *const p); 1
void f(int *p); 2
void f(const int *p); 3
void f(const int *const p); 4
1和2,3和4无法构成重载。
指向函数的指针
bool *pf(const string &, const string &);//pf是函数,返回值是指向bool的指针。
bool (*pf) (const string &, const string &);//pf是指向函数的指针。
----------------------------------------------------
typedef bool (*funType) (const string &, const string &);
bool copareLength(const string &a, const string &b)
{return a.size() == b.size();
}
void funParam(const string &a, const string &b, funType pf)
{cout << pf(a, b) << endl;
}
int main(int argc, char **argv)
{bool (*pf) (const string &, const string &);funType pf1;//pf1 和 pf一致。pf = &copareLength;cout << copareLength("33", "56") << endl;cout << pf("33", "56") << endl;cout << (*pf)("303", "56") << endl;funParam("4", "76", &copareLength);pf1 = &copareLength;funParam("49", "76", pf1);return 0;
}
--------------------返回函数指针--------------------
int f(int *a, int b)
{return 10;
}
//ff是一个函数,有一个形参x,返回一个函数指针int (*)(int *, int)
int (*ff(int x))(int *, int)
{cout << x << endl;return f;
}
int main(int argc, char **argv)
{int i = 9;cout << ff(5)(&i,9);return 0;
}
在c++中有三种的IO操作:标准IO、文件IO、串IO。
标准IO:兼容了C中的标准的输入输出,从键盘输入或从屏幕输出。
文件IO:以磁盘文件为输入输出的对象,数据在磁盘中写入和读出。
串IO:对内存中指定的空间进行输入和输出。通常指定一个字符数组作为存储空间。
流分类:输入/输出流。字符流/字节流。
IO对象无拷贝或赋值,所以也无法放入vector等容器里。
std::ofstream out1, out2;
std::ofstream out3 = out1;//错误,无法使用拷贝初始化
out1 = out2;//错误,无法使用拷贝赋值运算符
传参时只能传引用,返回引用。
io对象状态
函数 | 描述 |
---|---|
eof() | 若流的eofbit置位,则返回true,表示流到达了文件结束 |
fail() | 若流的failbit或badbit置位,则返回true,表示IO操作失败了 |
bad() | 若流的badbit置位,则返回true,表示发生了系统级错误,无法恢复 |
good() | 若流处于有效状态,则返回true |
clear() | 将流的所有状态位复位,将流的状态设置为有效 |
clear(flags) | 根据给定的标志位,将流对应的状态位复位 |
setstate(flags) | 根据给定的标志位,将流对应的状态位置位 |
rdstate() | 返回流当前的状态 |
int main(int argc, char **argv)
{int i;while (cin >> i, !f()){if (cin.bad()) {throw runtime_error("IO system error");}if (cin.fail()){cerr << "error data ,try again" << endl;cin.clear();cin.ignore(1024,'n');//清除1024个字符,或遇到回车符结束。continue;}cout << i << ends;}return 0;
}
std::endl;刷新缓存区并插入换行符。
std::flush;刷新流缓存区。
std::ends;插入空字符。
文件流
ifstream 支持从磁盘文件输入。
ofstream 支持向磁盘文件输出。
fstream 支持从磁盘文件输入和输出。
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char **argv)
{// ofstream outFile("D://1.txt");// outFile << "He";// outFile.close();//ifstream inFile("D://1.txt"); 写法1string filePath = "D://1.txt";//ifstream inFile(filePath.c_str());//这里必须是c风格字符串 写法2ifstream inFile;inFile.open(filePath.c_str()); //写法3if (!inFile){cerr << "open file error:" << filePath << endl;}string s;while (inFile >> s){cout << s << endl;}inFile.close();//inFile此时状态是 eofcout << f() << endl;//判断是否是eof状态inFile.clear();//恢复流的状态,才能正常继续open其他文件return 0;
}
标志 | 释义 | |
---|---|---|
in | input | 打开文件读,内部流缓冲区支持输入操作 |
out | output | 文件打开供写入,内部流缓冲区支持输出操作 |
binary | binary | 执行二进制操作 |
app | append | 追加,所有的操作都发生在文件的末尾 |
ate | at end | 输出位置在文件的末尾 |
trunc | trucate | 打开文件之前存于文件之中的内容都会被丢弃 |
string filePath = "D://1.txt";ifstream inFile(filePath.c_str(), ifstream::in);//输入ofstream outFile(filePath.c_str(), ofstream::out);//输出 + 清空ofstream outFile(filePath.c_str(), ofstream::out | ofstream::app);//输出 + 追加fstream fFile(filePath.c_str());//输入 + 输出 默认有in 和 out 和 在文件头追加//fstream 想在文件尾追加fstream fFile(filePath.c_str(), fstream::in | fstream::out | fstream::ate);
//案例
ifstream& open_file(ifstream &in, const string &file)
{in.close();in.clear();in.open(file.c_str());return in;
}
字符串流
#include <sstream>
//分割字符串算法
string s;
getline(cin, s);
stringstream ss(s); //字符串流
while (getline(ss, s, ' ')) {cout << s << endl;
}
.............................................
input_s不接受 空格、换行,起到了分割作用。
ostringstream format_str;
format_str << "姓名: " << "千任雪" << "n" << "年龄: " << 18;
//cout << "信息:" << format_str.str() << endl;
istringstream input_s(format_str.str());
string name, throw_away, age;
input_s >> throw_away;//抛弃 姓名:
input_s >> name;
input_s >> throw_away;//抛弃 年龄:
input_s >> age;
cout << "result:" << name << "t" << age << endl;
STL基础语法
下一篇-类基础语法
本文发布于:2024-02-03 03:00:09,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170690040948207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |