1.容器算法迭代器基本思路
无额外说明
#include <iostream>
using namespace std;
//算法,负责统计某个元素的个数
int mycount(int* start,int* end,int val){
int num = 0;
while (start != end){
if (*start == val){
num++;
}
start++;
}
return num;
}
int main(void){
//数组 容器
int arr[] = { 0, 7, 5, 4, 9, 2, 0 };
int* pBegin = arr; //指向容器的第一个位置
int* pEnd = &(arr[sizeof(arr) / sizeof(int)]);
int num = mycount(pBegin, pEnd, 0);
cout << "num :" << num << endl;
return 0;
}
2.STL_hello_world_程序
无额外说明
#include <iostream>
#include<vector> //动态数组 可变数组
#include<algorithm> //算法
using namespace std;
//面向行读取
#if 0
cin.get(buf, 256); //不会读最后换行
cin.getline() //最后换行丢弃
#endif
void PrintVector(int v){
cout << v << " ";
}
//STL基本语法
void test01(){
//定义一个容器,并且制指定这个容器存放的元素类型是Int
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
//通过STL提供的for_each算法
//容器提供的迭代器
//vector<int>::iterator 迭代器类型
vector<int>::iterator pBegin = v.begin();
vector<int>::iterator pEnd = v.end();
//容器中可能存放基础的数据类型,也可能存放自定义数据类型
for_each(pBegin, pEnd, PrintVector);
}
//容器也可以存放自定义数据类型
class Person{
public:
Person(int age,int id):age(age),id(id){}
public:
int age;
int id;
};
void test02(){
//创建容器,并且指定容器的元素类型是Person
vector<Person*> v;
Person p1(10, 20), p2(30, 40), p3(50, 60);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
//遍历
for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++){
cout << (**it).age << " " << (**it).id << endl;
}
}
int main(void) {
test01();
test02();
return 0;
}
3.string容器
具体API可点击Cpp参考
#include <iostream>
#include<string>
using namespace std;
//初始化
void test01(){
string s1; //调用无参构造
string s2(10, 'a');
string s3("abcdefg");
string s4(s3); //拷贝构造
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
}
//赋值操作
void test02(){
string s1;
string s2("appp");
s1 = "abcdef";
cout << s1 << endl;
s1 = s2;
cout << s1 << endl;
s1 = 'a';
cout << s1 << endl;
//成员方法assign
s1.assign("jkl");
cout << s1 << endl;
}
//取值操作
void test03(){
string s1 = "abcdefg";
//重载[]操作符
for (int i = 0; i < s1.size();i++){
cout << s1[i] << " ";
}
cout << endl;
//at成员函数
for (int i = 0; i < s1.size(); i++){
cout << s1.at(i) << " ";
}
cout << endl;
//区别:[]方式 如果访问越界,直接挂了
//at方式 访问越界 抛异常out_of_range
try{
//cout << s1[100] << endl;
cout << s1.at(100) << endl;
}
catch (...){
cout << "越界!" << endl;
}
}
//拼接操作
void test04(){
string s = "abcd";
string s2 = "1111";
s += "abcd";
s += s2;
cout << s << endl;
string s3 = "2222";
s2.append(s3);
cout << s2 << endl;
string s4 = s2 + s3;
cout << s4 << endl;
}
//查找操作
void test05(){
string s = "abcdefghjfgkl";
//查找第一次出现的位置
int pos = s.find("fg");
cout << "pos:" << pos << endl;
//查找最后一次出现的位置
pos = s.rfind("fg");
cout << "pos:" << pos << endl;
}
//string替换
void test06(){
string s = "abcdefg";
s.replace(0,2,"111");
cout << s << endl;
}
//string比较
void test07(){
string s1 = "abcd";
string s2 = "abce";
if (s1.compare(s2) == 0){
cout << "字符串相等!" << endl;
}
else{
cout << "字符串不相等!" << endl;
}
}
//子串操作
void test08(){
string s = "abcdefg";
string mysubstr = s.substr(1, 3);
cout << mysubstr << endl;
}
//插入和删除
void test09(){
string s = "abcdefg";
s.insert(3,"111");
cout << s << endl;
s.erase(0, 2);
cout << s << endl;
}
/*
// string 转 char*
string str = "itcast";
const char* cstr = str.c_str();
// char* 转 string
char* s = "itcast";
string sstr = sstr(s);
*/
int main(void){
//test01();
//test02();
//test03();
//test04();
//test05();
//test06();
//test07();
//test08();
test09();
return 0;
}
4.vector容器
具体API可点击Cpp参考
#include <iostream>
#include<vector>
using namespace std;
void printVector(vector<int>& v){
for (vector<int>::iterator it = v.begin(); it != v.end(); it++){
cout << *it << " ";
}
cout << endl;
}
//初始化
void test01(){
vector<int> vl;//默认构造
int arr[] = { 10, 20, 30, 40 };
vector<int> v2(arr,arr + sizeof(arr)/sizeof(int));
vector<int> v3(v2.begin(), v2.end());
vector<int> v4(v3);
printVector(v2);
printVector(v3);
printVector(v4);
}
//常用赋值操作
void test02(){
int arr[] = { 10, 20, 30, 40 };
vector<int> vl1(arr,arr+sizeof(arr)/sizeof(int));//默认构造
//成员方法
vector<int> v2;
v2.assign(vl1.begin(), vl1.end());
//重载=
vector<int> v3;
v3 = v2;
int arr1[] = { 100, 200, 300, 400 };
vector<int> v4(arr1, arr1 + sizeof(arr) / sizeof(int));//默认构造
printVector(vl1);
printVector(v2);
printVector(v3);
printVector(v4);
cout << "------------------" << endl;
v4.swap(vl1);
printVector(vl1);
printVector(v2);
printVector(v3);
printVector(v4);
}
//大小操作
void test03(){
int arr1[] = { 100, 200, 300, 400 };
vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));//默认构造
cout << "size:" << v4.size() << endl;
if (v4.empty() == true){
cout << "空!" << endl;
}
else{
cout << "不空!" << endl;
}
printVector(v4);
v4.resize(2);
printVector(v4);
//v4.resize(6);
printVector(v4);
v4.resize(6,1);
printVector(v4);
for (int i = 0; i < 10000; i++){
v4.push_back(i);
}
cout << "size:" << v4.size() << endl; //元素的个数 20
cout << "容量:" << v4.capacity() << endl; //容量 100
}
//vector存取数据
void test04(){
int arr1[] = { 100, 200, 300, 400 };
vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));//默认构造
for (int i = 0; i < v4.size();i++){
cout << v4[i] << " ";
}
cout << endl;
for (int i = 0; i < v4.size(); i++){
cout << v4.at(i) << " ";
}
cout << endl;
//区别: at抛异常 []不抛异常
cout << "front:" << v4.front() << endl;
cout << "back:" << v4.back() << endl;
}
//插入和删除
void test05(){
vector<int> v;
v.push_back(10);
v.push_back(20);
//头插法
v.insert(v.begin(),30);
v.insert(v.end(),40);
v.insert(v.begin() + 2, 100); //vector支持随机访问
//支持数组下标,一般都支持随机访问
//迭代器可以直接+2 +3 -2 -5操作
printVector(v);
//删除
v.erase(v.begin());
printVector(v);
v.erase(v.begin() + 1, v.end());
printVector(v);
v.clear();
cout << "size:" << v.size() << endl;
}
//巧用swap缩减空间
void test06(){
//vector添加元素(超过容量时),容量会自动增长 你删除元素时候,容量不会自动减少
vector<int> v;
for (int i = 0; i < 100000; i ++){
v.push_back(i);
}
cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
v.resize(10);
cout << "--------------" << endl;
cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
//收缩空间
vector<int>(v).swap(v); //v初始化匿名对象,然后匿名对象与v交换
cout << "--------------" << endl;
cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
}
void test07(){
//reserve 预留空间 resize区别
/*
reserve 与 resize 的区别
reserve是容器预留空间,但在空间内不真正创建元素对象,所以在没有添加新的对象之前,不能引用容器内 的元素
resize是改变容器的大小,且在创建对象,因此,调用这个函数之后,就可以引用容器内的对象了
*/
int num = 0;
int* address = NULL;
vector<int> v;
v.reserve(100000);
for (int i = 0; i < 100000;i++){
v.push_back(i);
if (address != &(v[0])){
address = &(v[0]);
num++;
}
}
cout << "num:" << num << endl;
//如果你知道容器大概要存储的元素个数,那么你可以用reserve预留空间
}
int main(void){
//test01();
//test02();
//test03();
//test04();
//test05();
//test06();
test07();
return 0;
}
5.Cpp输入输出流
具体函数用法可参考Cpp参考
#include <iostream>
using namespace std;
#include<iomanip>
#if 0
cout; //全局流对象 输出数据到显示器
cin;
//cerr没有缓冲区 clog有缓冲区
cerr; //标准错误 输出数据到显示器
clog; //标准日志 输出数据到显示器
#endif
//标准输入流 cin.get()
void test01(){
char ch;
//while ((ch = cin.get()) != EOF){
// cout << ch << endl;
//}
//cin.get
char ch2;
//cin.get(ch2); //读取一个字符
char buf[256] = { 0 };
//cin.get(buf, 256); //从缓冲区读一个字符串
cin.getline(buf,256); //读取一行数据 不读换行符
cout << buf;
}
//cin.ignore 忽略当前的字符
void test02(){
char ch;
cin.get(ch); //从缓冲区要数据 阻塞
cout << ch << endl;
cin.ignore(10); //请参考Cppreference
cin.get(ch);
cout << ch << endl;
}
void test03(){
cout << "请输入数组或者字符串:" << endl;
char ch;
ch = cin.peek(); //偷窥一下缓冲区,返回第一个字符
if (ch >= '0' && ch <= '9'){
int number;
cin >> number;
cout << "您输入的是数字:" << number << endl;
}
else{
char buf[256] = { 0 };
cin >> buf;
cout << "您输入的是字符串:" << buf << endl;
}
}
//cin.putback cin.get
void test04(){
cout << "请输入字符串或者数字:" << endl;
char ch;
cin.get(ch); //从缓冲区取走一个字符
if (ch >= '0' && ch <= '9'){
//ch放回到缓冲区
cin.putback(ch);
int number;
cin >> number;
cout << "您输入的是数字:" << number << endl;
}
else{
cin.putback(ch);
char buf[256] = { 0 };
cin >> buf;
cout << buf << endl;
}
}
//标准输出流
void test05(){
cout << "hello world" << endl;
//cout.flush();
cout.put('h').put('e').put('l') << endl;
cout.write("hello Zhaosi!", strlen("hello Zhaosi!"));
}
//格式化输出
void test06(){
//成员方法的方式
int number = 10;
cout << number << endl;
cout.unsetf(ios::dec); //卸载当前默认的的10进制输出方式
cout.setf(ios::oct); //八进制输出
cout.setf(ios::showbase);
cout << number << endl;
cout.unsetf(ios::oct); //卸载8进制
cout.setf(ios::hex);
cout << number << endl;
cout.width(10);
cout.fill('*');
cout.setf(ios::left);
cout << number << endl;
//通过控制符
int number2 = 10;
cout << hex
<< setiosflags(ios::showbase)
<< setw(10)
<< setfill('*')
<< setiosflags(ios::left)
<< number2
<< endl;
}
int main(void) {
//test01();
//test02();
//test03();
test04();
//test05();
//test06();
return 0;
}
6.文件读写
可以在编译器中运行体会,具体的API建议查Cpp参考
#include <iostream>
using namespace std;
#include<fstream> //文件读写
//文本文件读写
void test01(){
char* fileName = "D:/source.txt";
char* TagetName = "D:/target.txt";
ifstream ism(fileName, ios::in); //只读方式打开文件
ofstream osm(TagetName, ios::out | ios::app); // ios::app表示每次运行都会再输入一次
//ifstream ism;
//ism.open(fileName,ios::in);
if (!ism){
cout << "打开文件失败!" << endl;
return;
}
//读文件
char ch;
while (ism.get(ch)){
cout << ch;
osm.put(ch);
}
//关闭文件
ism.close();
osm.close();
}
//二进制文件操作 对象序列化
class Person{
public:
Person(){}
Person(int age,int id):age(age),id(id){}
void Show(){
cout << "Age:" << age << " Id:" << id << endl;
}
public:
int age;
int id;
};
void test02(){
//文本模式读的是文本文件吗?
//二进制模式读的是二进制文件吗
#if 0
Person p1(10, 20), p2(30, 40); //二进制
//把p1 p2写进文件里
ofstream osm(TagetName, ios::out | ios::binary);
osm.write((char*)&p1, sizeof(Person)); //二进制方式写文件
osm.write((char*)&p2, sizeof(Person));
osm.close();
#endif
char* TagetName = "C:\\Users\\apple\\Desktop\\target.txt";
ifstream ism(TagetName,ios::in | ios::binary);
Person p1,p2;
ism.read((char*)&p1, sizeof(Person)); //从文件读取数据
ism.read((char*)&p2, sizeof(Person)); //从文件读取数据
p1.Show();
p2.Show();
}
int main(void){
test01();
//test02();
return 0;
}