目录

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 = hellostring s3(s2);//使用一个string对象初始化另一个string对象cout << "s3=" << s3 << endl; //s3 = hellostring s4(10, 'a');//使用n个字符c初始化cout << "s4=" << s4 << endl; //s4 = aaaaaaaaaa

190 赋值操作

 string str1;str1 = "hello";cout << "str1=" << str1 << endl;//str1=hellostring str2;str2 = str1;cout << "str2=" << str2 << endl;//str1=hellostring str3;str3 = 'a';cout << "str3=" << str3 << endl;//str3=astring str4;str4.assign("assign方式");cout << "str4=" << str4 << endl;//str4=assign方式string str5;str5.assign("hello sfa", 7);cout << "str5=" << str5 << endl;//str5=hello sstring str6;str6.assign(str5);cout << "str6=" << str6 << endl;//str6=hello sstring str7;str7.assign(10, 's');cout << "str7=" << str7 << endl;//str7=ssssssssss

191 字符串拼接

 string str1;str1 = "hello";str1 += " world";cout << "str1=" << str1 << endl;//str1=hello worldstr1 += '!';cout << "str1=" << str1 << endl;//str1=hello world!string str2 = "0123456";str1 += str2;cout << "str1=" << str1 << endl;//str1=hello world!0123456str1.append("append方式");cout << "str1=" << str1 << endl;//str1=hello world!0123456append方式str1.append("aaaa", 3);cout << "str1=" << str1 << endl;//str1=hello world!0123456append方式aaastr1.append(str2);cout << "str1=" << str1 << endl;//str1=hello world!0123456append方式aaa0123456str1.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=3pos = str1.find("deg");cout << "pos=" << pos << endl;//pos=-1if (pos!=-1){cout << "找到字符串" << endl;cout << "位置为:" << pos << endl;}else{cout << "未找到字符串" << endl;}//rfind从右往左查找;find从左往右查找string str2 = "adsajka";pos = str2.rfind('a');cout << "pos=" << pos << endl;//pos=6pos = str2.find('a');cout << "pos=" << pos << endl;//pos=0}
void test02()
{//替换string str1 = "sahdkfsa";str1.replace(1,2, "1111");//从1号位置起,2个字符替换为1111cout << "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<str2" << endl;}

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=gellostr.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=abcdefgcout << "substr=" << substr << endl;//substr=cdestring email = "skyesun@cool.com";int pos = email.find("@");cout << pos << endl;//7string username = email.substr(0, pos);cout << username << endl;//skyesun

vector容器

197 构造函数

 //默认构造 无参构造vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}printVector(v1); //0 1 2 3 4 5 6 7 8 9//通过区间方式进行构造vector<int>v2(v1.begin(), v1.end());printVector(v2); //0 1 2 3 4 5 6 7 8 9//n个elem方式构造vector<int>v3(10, 100); printVector(v3); //100 100 100 100 100 100 100 100 100 100//拷贝构造vector<int>v4(v3);printVector(v4); //100 100 100 100 100 100 100 100 100 100

198 赋值操作

    vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}printVector(v1); //0 1 2 3 4 5 6 7 8 9//赋值 operator=vector<int>v2;v2 = v1;printVector(v2);//0 1 2 3 4 5 6 7 8 9//赋值 assignvector<int>v3;v3.assign(v1.begin(), v1.end());printVector(v3);//0 1 2 3 4 5 6 7 8 9//赋值 n个elem方式vector<int>v4;v4.assign(10, 100);printVector(v4);//100 100 100 100 100 100 100 100 100 100

199 容量和大小

    vector<int>v1;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的容量为:13cout << "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 0v1.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 100v1.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 3printVector(v1);v1.insert(v1.begin(), 2, 1000);//1000 1000 100 0 1 2 3printVector(v1);//删除v1.erase(v1.begin());printVector(v1);//1000 100 0 1 2 3v1.erase(v1.begin(),v1.end());printVector(v1);////清空v1.clear();printVector(v1);//

201 数据存取

    vector<int>v1;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可以收缩内存vector<int>v;for (int i = 0; i < 10000; i++){v.push_back(i);}cout << "v容量:" << v.capacity() << endl;//v容量:12138cout << "v大小:" << v.size() << endl;//v大小:10000v.resize(3);cout << "v容量:" << v.capacity() << endl;//v容量:12138cout << "v大小:" << v.size() << endl;//v大小:3//swap可以收缩内存vector<int>(v).swap(v);cout << "v容量:" << v.capacity() << endl;//v容量:3cout << "v大小:" << v.size() << endl;// v大小:3//vector<int>(v)匿名对象//(用拷贝构造函数创造了一个新的对象,会用目前v的元素个数来初始化匿名对象大小)

203 预留空间

    vector<int>v;//利用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<int>& d)
{for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{deque<int>d1;for (int i = 0; i < 10; i++){d1.push_back(i);}printDeque(d1);deque<int>d2(d1.begin(), d1.end());printDeque(d2);deque<int>d3(10,200);printDeque(d3);deque<int>d4(d3);printDeque(d4);
}

205 赋值操作

 deque<int>d1;for (int i = 0; i < 6; i++){d1.push_back(i);}printDeque(d1);deque<int>d2;d2 = d1;printDeque(d2);deque<int>d3;d3.assign(d1.begin(), d1.end());printDeque(d3);deque<int>d4;d4.assign(2,10);printDeque(d4);

206 大小操作

 deque<int>d1;if (d1.empty())cout << "空" << endl;elsecout << "不空" << endl;for (int i = 0; i < 6; i++){d1.push_back(i);}printDeque(d1);if (d1.empty())cout << "空" << endl;else{cout << "不空" << endl;cout << "大小为:"<<d1.size() << endl;}d1.resize(10);printDeque(d1);d1.resize(15, 1);printDeque(d1);d1.resize(5);printDeque(d1);

207 插入和删除

 deque<int>d1;d1.push_back(1);//尾插d1.push_back(2);//尾插d1.push_front(3);//头插d1.push_front(4);//头插printDeque(d1);//4 3 1 2d1.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 1d1.erase(d1.begin());printDeque(d1);//1 1000 3 1 1 1 1000 3 1deque<int>::iterator it = d1.begin();it++;d1.erase(it);printDeque(d1);//1 3 1 1 1 1000 3 1d1.erase(d1.begin(), d1.end());printDeque(d1);//d1.clear();printDeque(d1);//

208 数据存取

 deque<int>d1;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<algorithm>deque<int>d1;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 2sort(d1.begin(), d1.end());printDeque(d1);//0 1 2 2 3 3 4 4 5 5 6

stack容器

211 基本概念

212 常用接口

 stack<int>s;s.push(1);s.push(2);s.push(3);s.push(4);s.push(5);cout << s.size();cout << endl;while (!s.empty()){cout<<"栈顶元素:"<<s.top() << endl;s.pop();}cout << s.size();

queue容器

213 基本概念

214 常用接口

list容器

215 基本概念

216 构造函数

 list<int>L1;for (int i = 0; i < 6; i++){L1.push_back(i);}printList(L1);list<int>L2(L1.begin(),L1.end());printList(L2);list<int>L3(L1);printList(L3);list<int>L4(2,5);printList(L4);

217 赋值和交换

 //赋值list<int>L1;for (int i = 0; i < 6; i++){L1.push_back(i);}printList(L1);list<int>L2;L2 = L1;printList(L2);list<int>L3;L3.assign(L2.begin(),L2.end());printList(L3);list<int>L4;L4.assign(3, 5);printList(L4);//交换list<int>L5;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 <int>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 5set <int>s2(s1);printSet(s2);//0 1 2 3 4 5set <int>s3;s3 = s2;printSet(s3);//0 1 2 3 4 5

224 大小和交换

225 插入和删除

set<int>s1;s1.insert(6);s1.insert(5);s1.insert(2);s1.insert(7);s1.insert(3);printSet(s1);//2 3 5 6 7s1.erase(s1.begin());printSet(s1);//3 5 6 7s1.erase(6);printSet(s1);//3 5 7s1.erase(s1.begin(), s1.end());//s1.clear();//

226 查找和统计

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

227 set和multiset区别

 set<int>s1;pair<set<int>::iterator, bool>ret = s1.insert(10);if (ret.second)cout << "第一次插入成功" << endl;elsecout << "失败" << endl;ret = s1.insert(10);if (ret.second)cout << "第二次插入成功" << endl;elsecout << "第二次失败" << endl;//第一次插入成功//第二次失败multiset<int>s2;s2.insert(10);s2.insert(10);for (multiset<int>::iterator it = s2.begin(); it != s2.end(); it++){cout << *it << " ";}cout << endl;//10 10

228 pair对组的创建

 pair<string, int>p("Tom", 20);cout << p.first << " " << p.second << endl;//Tom 20pair<string, int>p2 = 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()
{set<int,MyCompare>s1;s1.insert(2);s1.insert(6);s1.insert(1);for (set<int, MyCompare>::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()
{set<Person, MyComparePerson>s;Person p1("sa", 10);Person p2("asd", 20);Person p3("sd", 15);s.insert(p1);s.insert(p2);s.insert(p3);for (set<Person, MyComparePerson>::iterator it = s.begin(); it != s.end(); it++){cout << it->m_Name << " " << it->m_Num << endl;}
/*  asd 20sd 15sa 10*/
}

map/multimap容器

231 构造和赋值

void printMap(const map<int, int>& m)
{for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++){cout << "key="<<it->first<<"value="<<it->second << endl;}
}void test01()
{map<int, int>m;//创建m.insert(pair<int, int>(1, 10));m.insert(pair<int, int>(3, 20));m.insert(pair<int, int>(2, 30));printMap(m);//key=1value=10//key = 2value = 30//key = 3value = 20map<int, int>m2(m);printMap(m2);map<int, int>m3;m3 = m2;printMap(m3);
}

232 大小和交换

233 插入和删除

 map<int, int>m;m.insert(pair<int, int>(1, 10));m.insert(make_pair(2, 20));m.insert(map<int, int>::value_type(3, 30));m[4] = 40;//不建议用[]插入,可以利用key访问到valueprintMap(m);m.erase(m.begin());printMap(m);m.erase(3);//按照key删除printMap(m);

234 查找和统计

235 排序

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

  1. 笔记 黑马程序员C++教程从0到1入门编程——基础语法入门

    目录 1.C++初识 02 C++书写helloworld 03 注释 04 变量 05 常量 06 关键字 07 标识符命名规则 2.数据类型 08 整型 09 sizeof关键字 10 实型(浮点 ...

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

    目录 4 STL-函数对象 237 函数对象 谓词 238 一元谓词 239 二元谓词 内建函数对象 240 算术仿函数 241 关系仿函数 242 逻辑仿函数 5 STL-常用算法 遍历算法 243 ...

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

    目录 1 模板 01 模板的概念 02 函数模板基本语法 03 函数模板注意事项 04 函数模板案例-数组排序 05 普通函数与函数模板区别 06 普通函数与函数模板调用规则 07 模板的局限性 08 ...

  4. 【黑马程序员 C++教程从0到1入门编程】【笔记4】C++核心编程(类和对象——封装、权限、对象的初始化和清理、构造函数、析构函数、深拷贝、浅拷贝、初始化列表、友元friend、运算符重载)

    黑马程序员C++教程 文章目录 4 类和对象(类属性[成员属性],类函数[成员函数]) 4.1 封装 4.1.1 封装的意义(三种权限:public公共.protected保护.private私有)( ...

  5. 【黑马程序员 C++教程从0到1入门编程】【笔记3】C++核心编程(内存分区模型、引用、函数提高)

    黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难 文章目录 1 内存分区模型 1.1 程序运行前 1.2 程序运行后(手动开辟内存:c语言malloc,c++new) 1.3 new操作 ...

  6. 【黑马程序员 C++教程从0到1入门编程】【笔记2】通讯录管理系统

    黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难 文章目录 1.系统需求 2.创建项目 2.1 创建项目 3.菜单功能 4.退出功能 5.添加联系人 5.1 设计联系人结构体 5.2 设 ...

  7. 【黑马程序员 C++教程从0到1入门编程】【笔记1】数据类型、运算符、程序流程结构、数组、函数、指针、结构体

    黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难 文章目录 1.C++初识 1.1 第一个c++程序 1.2 注释 1.3 变量 1.4 常量 1.5 关键字 1.6 标识符命名规则 2 ...

  8. 【C++】黑马程序员 | c++教程从0到1入门编程笔记 | c++提高编程

    配套视频:https://www.bilibili.com/video/BV1et411b73Z 文章目录: 一.C++核心编程 二.C++提高编程 1 模板 本阶段主要针对C++泛型编程和STL技术 ...

  9. 【黑马程序员 C++教程从0到1入门编程】【笔记6】C++核心编程(文件操作)

    文章目录 5 文件操作 5.1文本文件 5.1.1写文件 5.1.2读文件 5.2 二进制文件 5.2.1 写文件(用二进制方式写时最好别用c++的string) 5.2.2 读文件 https:// ...

最新文章

  1. VS Code配置PHP XDebug
  2. 机器学习实战--决策树算法
  3. java地图 热力图,腾讯地图数据可视化之热力图
  4. java代码复数包括虚部和实部,Java中编写Applet程序验证复数类(在问题补充中)实现接收用户输入的复数的实部和虚部,计算复数与复数,复数与实数的加减,乘除操作...
  5. WPS2012交叉引用技巧,word比wps这点强更新參考文献
  6. 一个生成验证码的Jsp页面
  7. mount: block device /dev/cdrom is write-protected, mounting read-only 解决方法
  8. 安装mysql查看随机密码命令_deepin安装MySQL没有弹出设置root密码怎么进行修改
  9. 如何在Debian下快速安装中文字体
  10. visio 2020 最新版安装过程及注意事项
  11. 三种css样式应用方式,CSS样式三种形式
  12. 人生有三重境界:看山是山,看水是水;看山不是山,看水不是水;看山还是山,看水还是水=
  13. 如何免费使用正版win10系统
  14. 如何编写一个简单的 Python 程序
  15. 计算机的运算方法(中)测试
  16. MP4文件格式详解——文件类型ftyp(转)
  17. 元界快讯|首个物联网元宇宙平台“物联森友会”即将上线
  18. ctfshow baby杯 六一快乐 部分MISC WriteUp
  19. python oserror路径not found_详谈Python3 操作系统与路径 模块(os / os.path / pathlib)
  20. Codeforces-Edu6-B. Grandfather Dovlet’s calculator

热门文章

  1. html大段文本如何分页,打印大型HTML表时如何处理分页符
  2. Android Studio-开发少年强国App(一)
  3. 30W、45W、60W PD移动电源芯片方案+无线充方案
  4. 【勘误清单】《机器学习》 周志华 北京: 清华大学出版社
  5. 实现在GEF中被选中图形上的右键菜单
  6. 成功解决 KeyError: Unable to open object (object x doesnt exist)和no file found ./SGN/asd.phl
  7. Linux系统重装出现c0409a9f,自学IT吧论坛Linux系统运营系列视频教程#28期2016系统/服务器资源天地 - www.zxit8.com...
  8. ItextPdf给PDF批量插入图片
  9. 【數論】【搜索】【SCOI2009】遊戲
  10. 用微信 远程遥控 服务器