栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

笔记 黑马程序员C++教程从0到1入门编程——提高编程02

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

笔记 黑马程序员C++教程从0到1入门编程——提高编程02

目录

3 STL-常用容器

string容器

189 构造函数

190 赋值操作

191 字符串拼接

192 字符串查找和替换

193 字符串比较

194 字符存取

195 字符串插入和删除

196 子串获取

vector容器

197 构造函数

198 赋值操作

199 容量和大小

200 插入和删除

201 数据存取

202 互换容器

203 预留空间

deque容器

204 构造函数

205 赋值操作

206 大小操作

207 插入和删除

208 数据存取

209 排序操作

stack容器

211 基本概念

212 常用接口

queue容器

213 基本概念

214 常用接口

list容器

215 基本概念

216 构造函数

217 赋值和交换

218 大小操作

​编辑219 插入和删除

220 数据存取

221 翻转和排序

set/multiset容器

223 构造和赋值

224 大小和交换

225 插入和删除

226 查找和统计

227 set和multiset区别

228 pair对组的创建

229 内置类型指定排序规则

230 自定义数据类型指定排序规则

map/multimap容器

231 构造和赋值

232 大小和交换

233 插入和删除

234 查找和统计

235 排序


3 STL-常用容器

string容器

189 构造函数

	string s1;//创建一个空的字符串 默认构造
	const char *str = "hello";
	string s2(str);//使用字符串S初始化
	cout << "s2=" << s2 << endl; //s2 = hello
	string s3(s2);//使用一个string对象初始化另一个string对象
	cout << "s3=" << s3 << endl; //s3 = hello
	string s4(10, 'a');//使用n个字符c初始化
	cout << "s4=" << s4 << endl; //s4 = aaaaaaaaaa

190 赋值操作

	string str1;
	str1 = "hello";
	cout << "str1=" << str1 << endl;//str1=hello
	string str2;
	str2 = str1;
	cout << "str2=" << str2 << endl;//str1=hello
	string str3;
	str3 = 'a';
	cout << "str3=" << str3 << endl;//str3=a

	string str4;
	str4.assign("assign方式");
	cout << "str4=" << str4 << endl;//str4=assign方式

	string str5;
	str5.assign("hello sfa", 7);
	cout << "str5=" << str5 << endl;//str5=hello s

	string str6;
	str6.assign(str5);
	cout << "str6=" << str6 << endl;//str6=hello s

	string str7;
	str7.assign(10, 's');
	cout << "str7=" << str7 << endl;//str7=ssssssssss

191 字符串拼接

	string str1;
	str1 = "hello";
	str1 += " world";
	cout << "str1=" << str1 << endl;//str1=hello world
	str1 += '!';
	cout << "str1=" << str1 << endl;//str1=hello world!
	string str2 = "0123456";
	str1 += str2;
	cout << "str1=" << str1 << endl;//str1=hello world!0123456

	str1.append("append方式");
	cout << "str1=" << str1 << endl;//str1=hello world!0123456append方式
	str1.append("aaaa", 3);
	cout << "str1=" << str1 << endl;//str1=hello world!0123456append方式aaa
	str1.append(str2);
	cout << "str1=" << str1 << endl;//str1=hello world!0123456append方式aaa0123456
	str1.append(str2,3,2);//参数2 从哪个字符开始截取,参数3 截取字符个数
	cout << "str1=" << str1 << endl;//str1=hello world!0123456append方式aaa012345634

192 字符串查找和替换

void test01()
{//查找
	string str1 = "abcdefg";
	int pos=str1.find("de");
	cout << "pos=" << pos << endl;//pos=3
	pos = str1.find("deg");
	cout << "pos=" << pos << endl;//pos=-1
	if (pos!=-1)
	{
		cout << "找到字符串" << endl;
		cout << "位置为:" << pos << endl;
	}
	else
	{
		cout << "未找到字符串" << endl;
	}

	//rfind从右往左查找;find从左往右查找
	string str2 = "adsajka";
	pos = str2.rfind('a');
	cout << "pos=" << pos << endl;//pos=6
	pos = str2.find('a');
	cout << "pos=" << pos << endl;//pos=0
	
}
void test02()
{//替换
	string str1 = "sahdkfsa";
	str1.replace(1,2, "1111");//从1号位置起,2个字符替换为1111
	cout << "str1=" << str1 << endl;//str1=s1111dkfsa
}

193 字符串比较

	string str1 = "hellob";
	string str2 = "helloa";
	if (str1.compare(str2) == 0)
	{
		cout << "str1==str2" << endl;
	}
	else if(str1.compare(str2) > 0)
	{
		cout << "str1>str2" << endl;
	}
	else
	{
		cout << "str1 

194 字符存取

	string str = "hello";
	//通过[]访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i]<<" ";//h e l l o
	}
	cout << endl;
	//通过at访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << " ";//h e l l o
	}
	cout << endl;
	//修改单个字符
	str[0] = 'g';
	cout << "str=" << str << endl;//str=gello
	str.at(3) = 'g';
	cout << "str=" << str << endl;//str=gelgo

195 字符串插入和删除

	string str = "hello";
	//插入
	str.insert(1, "555");//从第1个位置,插入"111"
	cout << "str=" << str << endl;//str=h555ello
	//删除
	str.erase(1, 3);//从第1个位置,删除3个元素
	cout << "str=" << str << endl;//str=h555ello

196 子串获取

    string str = "abcdefg";
	string substr = str.substr(2,3);
	cout << "str=" << str << endl;//str=abcdefg
	cout << "substr=" << substr << endl;//substr=cde

    string email = "skyesun@cool.com";
	int pos = email.find("@");
	cout << pos << endl;//7
	string username = email.substr(0, pos);
	cout << username << endl;//skyesun

vector容器

197 构造函数

	//默认构造 无参构造
	vectorv1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1); //0 1 2 3 4 5 6 7 8 9

	//通过区间方式进行构造
	vectorv2(v1.begin(), v1.end());
	printVector(v2); //0 1 2 3 4 5 6 7 8 9

	//n个elem方式构造
	vectorv3(10, 100); 
	printVector(v3); //100 100 100 100 100 100 100 100 100 100

	//拷贝构造
	vectorv4(v3);
	printVector(v4); //100 100 100 100 100 100 100 100 100 100

198 赋值操作

    vectorv1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1); //0 1 2 3 4 5 6 7 8 9

	//赋值 operator=
	vectorv2;
	v2 = v1;
	printVector(v2);//0 1 2 3 4 5 6 7 8 9

	//赋值 assign
	vectorv3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);//0 1 2 3 4 5 6 7 8 9

	//赋值 n个elem方式
	vectorv4;
	v4.assign(10, 100);
	printVector(v4);//100 100 100 100 100 100 100 100 100 100

199 容量和大小

    vectorv1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	if (v1.empty())
	{
		cout << "v1为空" << endl;
	}
	else
	{
		cout << "v1不为空" << endl; //v1不为空
		cout << "v1的容量为:" << v1.capacity() << endl;//v1的容量为:13
		cout << "v1的大小为:" << v1.size() << endl;//	v1的大小为:10
	}

	//重新指定大小
	v1.resize(15);//如果指定的比原来的长,默认使用0重新填充新位置
	printVector(v1);//	0 1 2 3 4 5 6 7 8 9 0 0 0 0 0
	v1.resize(20,100);	//可用参数2指定默认填充值
	printVector(v1);// 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 100 100 100 100 100
	v1.resize(5);
	printVector(v1);//0 1 2 3 4

200 插入和删除

	printVector(v1);//0 1 2 3 4

	//尾删
	v1.pop_back();
	printVector(v1);//0 1 2 3
	//插入 第一个参数是迭代器
	v1.insert(v1.begin(), 100);//100 0 1 2 3
	printVector(v1);

	v1.insert(v1.begin(), 2, 1000);//1000 1000 100 0 1 2 3
	printVector(v1);

	//删除
	v1.erase(v1.begin());
	printVector(v1);//1000 100 0 1 2 3

	v1.erase(v1.begin(),v1.end());
	printVector(v1);//

	//清空
	v1.clear();
	printVector(v1);//

201 数据存取

    vectorv1;
	for (int i = 0; i < 5; i++)
	{
		v1.push_back(i);
	}
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] <<" ";
	}
	cout << endl;
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1.at(i) << " ";
	}
	cout << endl;
	cout << "第一个元素为:" << v1.front() << endl;
	cout << "最后一个元素为:" << v1.back() << endl;
		//0 1 2 3 4
		//0 1 2 3 4
		//第一个元素为:0
		//最后一个元素为:4

202 互换容器

    //巧用swap可以收缩内存
	vectorv;
	for (int i = 0; i < 10000; i++)
	{
		v.push_back(i);
	}
	cout << "v容量:" << v.capacity() << endl;//v容量:12138
	cout << "v大小:" << v.size() << endl;//v大小:10000

	v.resize(3);
	cout << "v容量:" << v.capacity() << endl;//v容量:12138
	cout << "v大小:" << v.size() << endl;//v大小:3

	//swap可以收缩内存
	vector(v).swap(v);
	cout << "v容量:" << v.capacity() << endl;//v容量:3
	cout << "v大小:" << v.size() << endl;//	v大小:3
	//vector(v)匿名对象
	//(用拷贝构造函数创造了一个新的对象,会用目前v的元素个数来初始化匿名对象大小)

203 预留空间

    vectorv;
	//利用reserve预留空间
	v.reserve(10000);
	int num = 0;//统计开辟内存的次数
	int* p = NULL;
	for (int i = 0; i < 10000; i++)
	{
		v.push_back(i);
		if (p != &v[0])
		{
			p = &v[0];
			num++;
		}
	}
	cout << num << endl;//24 用了reserve后变为1

deque容器

204 构造函数

void printDeque(const deque& d)
{
	for (deque::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	dequed1;
	for (int i = 0; i < 10; i++)
	{
		d1.push_back(i);
	}
	printDeque(d1);

	dequed2(d1.begin(), d1.end());
	printDeque(d2);

	dequed3(10,200);
	printDeque(d3);

	dequed4(d3);
	printDeque(d4);
}

205 赋值操作

	dequed1;
	for (int i = 0; i < 6; i++)
	{
		d1.push_back(i);
	}
	printDeque(d1);

	dequed2;
	d2 = d1;
	printDeque(d2);

	dequed3;
	d3.assign(d1.begin(), d1.end());
	printDeque(d3);

	dequed4;
	d4.assign(2,10);
	printDeque(d4);

206 大小操作

	dequed1;
	if (d1.empty())
		cout << "空" << endl;
	else
		cout << "不空" << endl;
	for (int i = 0; i < 6; i++)
	{
		d1.push_back(i);
	}
	printDeque(d1);
	if (d1.empty())
		cout << "空" << endl;
	else
	{
		cout << "不空" << endl;
		cout << "大小为:"< 

207 插入和删除

	dequed1;
	d1.push_back(1);//尾插
	d1.push_back(2);//尾插
	d1.push_front(3);//头插
	d1.push_front(4);//头插
	printDeque(d1);//4 3 1 2
	d1.pop_back();//尾删
	printDeque(d1);//4 3 1 
	d1.pop_front();//头删
	printDeque(d1);//3 1 

	//指定位置插入删除
	d1.insert(d1.begin(), 1000);
	printDeque(d1);//1000 3 1 
	d1.insert(d1.begin(), 2,1);
	printDeque(d1);//1 1 1000 3 1 
	d1.insert(d1.begin(), d1.begin(), d1.end());
	printDeque(d1);//1 1 1000 3 1 1 1 1000 3 1

	d1.erase(d1.begin());
	printDeque(d1);//1 1000 3 1 1 1 1000 3 1
	deque::iterator it = d1.begin();
	it++;
	d1.erase(it);
	printDeque(d1);//1 3 1 1 1 1000 3 1

	d1.erase(d1.begin(), d1.end());
	printDeque(d1);//

	d1.clear();
	printDeque(d1);//

208 数据存取

	dequed1;
	for (int i = 0; i < 6; i++)
	{
		d1.push_back(i);
	}
	for (int i = 0; i < d1.size(); i++)
	{
		cout << d1[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < d1.size(); i++)
	{
		cout << d1.at(i) << " ";
	}
	cout << endl;
	cout << d1.front() << endl;//首元素
	cout << d1.back() << endl;//尾元素

209 排序操作

 对于支持随机访问的迭代器,都可以利用sort算法直接对其进行排序(vector)

#include

	dequed1;
	for (int i = 0; i < 6; i++)
	{
		d1.push_back(i);
	}
	for (int i = 6; i >1 ; i--)
	{
		d1.push_back(i);
	}
	printDeque(d1); //0 1 2 3 4 5 6 5 4 3 2
	sort(d1.begin(), d1.end());
	printDeque(d1);//0 1 2 2 3 3 4 4 5 5 6

stack容器

211 基本概念

212 常用接口

	stacks;
	s.push(1);
	s.push(2);
	s.push(3);
	s.push(4);
	s.push(5);

	cout << s.size();
	cout << endl;

	while (!s.empty())
	{
		cout<<"栈顶元素:"< 

queue容器

213 基本概念

214 常用接口

list容器

215 基本概念

216 构造函数

	listL1;
	for (int i = 0; i < 6; i++)
	{
		L1.push_back(i);
	}
	printList(L1);

	listL2(L1.begin(),L1.end());
	printList(L2);

	listL3(L1);
	printList(L3);

	listL4(2,5);
	printList(L4);

217 赋值和交换

	//赋值
	listL1;
	for (int i = 0; i < 6; i++)
	{
		L1.push_back(i);
	}
	printList(L1);

	listL2;
	L2 = L1;
	printList(L2);

	listL3;
	L3.assign(L2.begin(),L2.end());
	printList(L3);

	listL4;
	L4.assign(3, 5);
	printList(L4);

	//交换
	listL5;
	L5.assign(10, 100);
	cout << "swap前" << endl;
	printList(L4);
	printList(L5);
	cout << "swap后" << endl;
	L4.swap(L5);
	printList(L4);
	printList(L5);

218 大小操作

219 插入和删除

220 数据存取

221 翻转和排序

set/multiset容器

223 构造和赋值

	set s1;
	for (int i = 0; i < 6; i++)
	{
		s1.insert(i);
	}
	for (int i = 6; i < 2; i--)
	{
		s1.insert(i);
	}
	printSet(s1);//0 1 2 3 4 5

	set s2(s1);
	printSet(s2);//0 1 2 3 4 5

	set s3;
	s3 = s2;
	printSet(s3);//0 1 2 3 4 5

224 大小和交换

225 插入和删除

sets1;
	s1.insert(6);
	s1.insert(5);
	s1.insert(2);
	s1.insert(7);
	s1.insert(3);
	printSet(s1);//2 3 5 6 7
	s1.erase(s1.begin());
	printSet(s1);//3 5 6 7
	s1.erase(6);
	printSet(s1);//3 5 7
	s1.erase(s1.begin(), s1.end());//
	s1.clear();//

226 查找和统计

	set::iterator pos = s1.find(3);
	if (pos != s1.end())
	{
		cout << "找到了" <<*pos<< " ";
		cout << "一共" << s1.count(*pos) << "个" << endl;
	}
	else
	{
		cout << "没找到" << endl;
	}

227 set和multiset区别

	sets1;
	pair::iterator, bool>ret = s1.insert(10);

	if (ret.second)
		cout << "第一次插入成功" << endl;
	else
		cout << "失败" << endl;

	ret = s1.insert(10);
	if (ret.second)
		cout << "第二次插入成功" << endl;
	else
		cout << "第二次失败" << endl;
	//第一次插入成功
	//第二次失败

	multisets2;
	s2.insert(10);
	s2.insert(10);
	for (multiset::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;//10 10

228 pair对组的创建

	pairp("Tom", 20);
	cout << p.first << " " << p.second << endl;//Tom 20
	pairp2 = make_pair("Skye", 30);
	cout << p2.first << " " << p2.second << endl;//Skye 30

229 内置类型指定排序规则

class MyCompare
{
public:
	bool operator()(int a, int b)const
	{
		return a > b;//降序
	}
};

void test01()
{
	sets1;
	s1.insert(2);
	s1.insert(6);
	s1.insert(1);
	for (set::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";//6 2 1
	}
	cout << endl;
}

230 自定义数据类型指定排序规则
class Person
{
public:
	Person(string name, int num)
	{
		this->m_Name = name;
		this->m_Num = num;
	}
	string m_Name;
	int m_Num;
};

class MyComparePerson
{
public:
	bool operator()(const Person &p1,const Person &p2)const
	{
		return p1.m_Num>p2.m_Num;
	}
};

void test01()
{
	sets;

	Person p1("sa", 10);
	Person p2("asd", 20);
	Person p3("sd", 15);
	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	for (set::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << it->m_Name << " " << it->m_Num << endl;
	}

}

map/multimap容器

231 构造和赋值

void printMap(const map& m)
{
	for (map::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key="<first<<"value="<second << endl;
	}
}

void test01()
{
	mapm;//创建
	m.insert(pair(1, 10));
	m.insert(pair(3, 20));
	m.insert(pair(2, 30));

	printMap(m);//key=1value=10
				//key = 2value = 30
				//key = 3value = 20

	mapm2(m);
	printMap(m2);

	mapm3;
	m3 = m2;
	printMap(m3);
}

232 大小和交换

233 插入和删除

	mapm;
	m.insert(pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(map::value_type(3, 30));
	m[4] = 40;//不建议用[]插入,可以利用key访问到value
	printMap(m);

	m.erase(m.begin());
	printMap(m);
	m.erase(3);//按照key删除
	printMap(m);

234 查找和统计

235 排序

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/892561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号