杭州旅游景区网站建设,那种非法网站怎么做的,平板网站开发环境,怎样做企业的网站本专栏记录C学习过程包括C基础以及数据结构和算法#xff0c;其中第一部分计划时间一个月#xff0c;主要跟着黑马视频教程#xff0c;学习路线如下#xff0c;不定时更新#xff0c;欢迎关注。 当前章节处于#xff1a; ---------第1阶段-C基础入门 ---------第2阶段实战… 本专栏记录C学习过程包括C基础以及数据结构和算法其中第一部分计划时间一个月主要跟着黑马视频教程学习路线如下不定时更新欢迎关注。 当前章节处于 ---------第1阶段-C基础入门 ---------第2阶段实战-通讯录管理系统 ---------第3阶段-C核心编程 ---------第4阶段实战-基于多态的企业职工系统 第5阶段-C提高编程 ---------第6阶段实战-基于STL泛化编程的演讲比赛 ---------第7阶段-C实战项目机房预约管理系统 文章目录 一、STL初识二、 string 容器2.1 string构造函数2.2 string赋值操作2.3 string字符串拼接2.4 string查找和替换2.5 string字符串比较2.6 string字符存取2.7 string插入和删除2.8 string子串 三、Vector容器3.1 基本概念3.2 vector构造函数3.3 vector赋值操作3.4 vector容量和大小3.5 vector插入和删除3.6 vector数据存取3.7 vector互换容器3.8 vector预留空间 一、STL初识
长久以来软件界一致希望简历一种可重复利用的东西C面向对象和泛型编程的思想目的就是复用性的提升大多情况下数据结构和算法都未能有一套标准导致大量重复性工作的产生为了建立数据结构和算法的一套标准诞生了STL
STL (Standard Template Library,标准模板库) STL从广义上分为 容器container 序列式容器强调值的排序序列式容器中的每个元素均有固定的位置关联式容器二叉树结构各元素之间没有严格的物理上的顺序关系 算法algorithm 质变算法运算过程中会更改区间内的元素内容比如拷贝、替换、删除等非质变算法运算过程中不会更改区间内的元素内容比如查找、计数、遍历、寻找极值等 迭代器iterator 容器和算法之间通过迭代器进行无缝连接STL几乎所有代码都采用了模板类或者模板函数。 stl大体分为六大组件分别是容器、算法、迭代器、仿函数、适配器配接器、空间配置器。
容器各种数据结构如vector、list、deque、set、map等用来存放数据算法各种常用的算法sort、find、copy、for_each等迭代器扮演了容器和算法之间的胶合剂仿函数“行为类似函数可作为算法的某种策略适配器一种用来修饰容器或者仿函数或者迭代器接口的东西空间配置器负责空间的配置和管理。
#include iostream
using namespace std;
#include vector
#include algorithm
void myprint(int num) {cout num endl;
}
int main() {// 创建容器vectorint v;// 向容器中添加元素v.push_back(10);v.push_back(20);v.push_back(30);v.push_back(40);// 遍历容器// 方法一for (vectorint::iterator it v.begin(); it ! v.end(); it) {cout *it endl;}// 方法二for_each(v.begin(), v.end(), myprint);system(pause);return 0;}10
20
30
40
10
20
30
40
请按任意键继续. . .二、 string 容器
string 是C风格的字符串本质是一个类
string和char * 区别
char * 是一个指针string是一个类类内部封装了char*管理这个字符串是一个char*型的容器。
特点
string 类内部封装了很多成员方法
例如查找find拷贝copy删除delete 替换replace插入insert
string管理char*所分配的内存不用担心复制越界和取值越界等由类内部进行负责
2.1 string构造函数
构造函数原型
string(); //创建一个空的字符串 例如: string str; string(const char* s); //使用字符串s初始化string(const string str); //使用一个string对象初始化另一个string对象string(int n, char c); //使用n个字符c初始化
#include iostream
using namespace std;int main() {string s1; //创建空字符串调用无参构造函数cout str1 s1 endl;const char* str hello world;string s2(str); //把c_string转换成了stringcout str2 s2 endl;string s3(s2); //调用拷贝构造函数cout str3 s3 endl;string s4(10, a);cout str3 s3 endl;system(pause);return 0;}str1
str2 hello world
str3 hello world
str3 hello world
请按任意键继续. . .2.2 string赋值操作
给字符串赋值赋值的函数原型
string operator(const char* s); //char*类型字符串 赋值给当前的字符串string operator(const string s); //把字符串s赋给当前的字符串string operator(char c); //字符赋值给当前的字符串string assign(const char *s); //把字符串s赋给当前的字符串string assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串string assign(const string s); //把字符串s赋给当前字符串string assign(int n, char c); //用n个字符c赋给当前字符串
#include iostream
using namespace std;int main() {string str1;str1 hello world;cout str1 str1 endl;string str2;str2 str1;cout str2 str2 endl;string str3;str3 a;cout str3 str3 endl;string str4;str4.assign(hello c);cout str4 str4 endl;string str5;str5.assign(hello c, 5);cout str5 str5 endl;string str6;str6.assign(str5);cout str6 str6 endl;string str7;str7.assign(5, x);cout str7 str7 endl;system(pause);return 0;}str1 hello world
str2 hello world
str3 a
str4 hello c
str5 hello
str6 hello
str7 xxxxx
请按任意键继续. . .2.3 string字符串拼接
函数原型
string operator(const char* str); //重载操作符string operator(const char c); //重载操作符string operator(const string str); //重载操作符string append(const char *s); //把字符串s连接到当前字符串结尾string append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾string append(const string s); //同operator(const string str)string append(const string s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾
#include iostream
using namespace std;int main() {string str1 我;str1 爱玩游戏;cout str1 str1 endl;str1 :;cout str1 str1 endl;string str2 LOL DNF;str1 str2;cout str1 str1 endl;string str3 I;str3.append( love );str3.append(game abcde, 4);//str3.append(str2);str3.append(str2, 4, 3); // 从下标4位置开始 截取3个字符拼接到字符串末尾cout str3 str3 endl;system(pause);return 0;}str1 我爱玩游戏
str1 我爱玩游戏:
str1 我爱玩游戏:LOL DNF
str3 I love gameDNF
请按任意键继续. . .2.4 string查找和替换
查找是找指定字符串是否存在替换是替换掉指定字符串 函数原型
int find(const string str, int pos 0) const; //查找str第一次出现位置,从pos开始查找int find(const char* s, int pos 0) const; //查找s第一次出现位置,从pos开始查找int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置int find(const char c, int pos 0) const; //查找字符c第一次出现位置int rfind(const string str, int pos npos) const; //查找str最后一次位置,从pos开始查找int rfind(const char* s, int pos npos) const; //查找s最后一次出现位置,从pos开始查找int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置int rfind(const char c, int pos 0) const; //查找字符c最后一次出现位置string replace(int pos, int n, const string str); //替换从pos开始n个字符为字符串strstring replace(int pos, int n,const char* s); //替换从pos开始的n个字符为字符串s
#include iostream
using namespace std;//查找和替换
void test01()
{//查找string str1 abcdefgde;int pos str1.find(de);if (pos -1){cout 未找到 endl;}else{cout pos pos endl;}pos str1.rfind(de);cout pos pos endl;}void test02()
{//替换string str1 abcdefgde;str1.replace(1, 3, 1111);cout str1 str1 endl;
}int main() {test01();test02();system(pause);return 0;
}pos 3
pos 7
str1 a1111efgde
请按任意键继续. . .总结
find查找是从左往后rfind从右往左find找到字符串后返回查找的第一个字符位置找不到返回-1replace在替换时要指定从哪个位置起多少个字符替换成什么样的字符串
2.5 string字符串比较
功能描述
字符串之间的比较
比较方式
字符串比较是按字符的ASCII码进行对比 返回 0 返回 1 返回 -1
函数原型
int compare(const string s) const; //与字符串s比较int compare(const char *s) const; //与字符串s比较
#include iostream
using namespace std;
//字符串比较
void test01()
{string s1 hello;string s2 aello;int ret s1.compare(s2);if (ret 0) {cout s1 等于 s2 endl;}else if (ret 0){cout s1 大于 s2 endl;}else{cout s1 小于 s2 endl;}}int main() {test01();system(pause);return 0;
}s1 大于 s2
请按任意键继续. . .总结字符串对比主要是用于比较两个字符串是否相等判断谁大谁小的意义并不是很大
2.6 string字符存取
string中单个字符存取方式有两种
char operator[](int n); //通过[]方式取字符char at(int n); //通过at方法获取字符
#include iostream
using namespace std;
void test01()
{string str hello world;for (int i 0; i str.size(); i){cout str[i] ;}cout endl;for (int i 0; i str.size(); i){cout str.at(i) ;}cout endl;//字符修改str[0] x;str.at(1) x;cout str endl;}int main() {test01();system(pause);return 0;
}h e l l o w o r l d
h e l l o w o r l d
xxllo world
请按任意键继续. . .在这里插入代码片总结string字符串中单个字符存取有两种方式利用 [ ] 或 at
2.7 string插入和删除
功能描述
对string字符串进行插入和删除字符操作
函数原型
string insert(int pos, const char* s); //插入字符串string insert(int pos, const string str); //插入字符串string insert(int pos, int n, char c); //在指定位置插入n个字符cstring erase(int pos, int n npos); //删除从Pos开始的n个字符
#include iostream
using namespace std;//字符串插入和删除
void test01()
{string str hello;str.insert(1, 111);cout str endl;str.erase(1, 3); //从1号位置开始3个字符cout str endl;
}int main() {test01();system(pause);return 0;
}h111ello
hello
请按任意键继续. . .**总结**插入和删除的起始下标都是从0开始
2.8 string子串
功能描述
从字符串中获取想要的子串
函数原型
string substr(int pos 0, int n npos) const; //返回由pos开始的n个字符组成的字符串
#include iostream
using namespace std;//子串
void test01()
{string str abcdefg;string subStr str.substr(1, 3);cout subStr subStr endl;string email hellosina.com;int pos email.find();string username email.substr(0, pos);cout username: username endl;}int main() {test01();system(pause);return 0;
}subStr bcd
username: hello
请按任意键继续. .总结灵活的运用求子串功能可以在实际开发中获取有效的信息
三、Vector容器
3.1 基本概念
vector数据结构和数组非常相似也称为单端数组不同之处在于数组是静态空间而vector可以动态扩展动态扩展机制 并不是在原空间之后续接新空间而是找更大的内存空间然后将原数据拷贝新空间释放原空间。vector容器的迭代器是支持随机访问的迭代器。
3.2 vector构造函数
功能描述
创建vector容器
函数原型
vectorT v; //采用模板实现类实现默认构造函数vector(v.begin(), v.end()); //将v[begin(), end())区间中的元素拷贝给本身。vector(n, elem); //构造函数将n个elem拷贝给本身。vector(const vector vec); //拷贝构造函数。
示例
#include iostream
using namespace std;
#include vectorvoid printVector(vectorint v) {for (vectorint::iterator it v.begin(); it ! v.end(); it) {cout *it ;}cout endl;
}void test01()
{vectorint v1; //无参构造for (int i 0; i 10; i){v1.push_back(i);}printVector(v1);vectorint v2(v1.begin(), v1.end());printVector(v2);vectorint v3(10, 100);// 10个100printVector(v3);vectorint v4(v3);printVector(v4);
}int main() {test01();system(pause);return 0;
}0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100
请按任意键继续. . .总结: vector的多种构造方式没有可比性灵活使用即可
3.3 vector赋值操作
功能描述
给vector容器进行赋值
函数原型 vector operator(const vector vec);//重载等号操作符 assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。 assign(n, elem); //将n个elem拷贝赋值给本身。
示例
#include iostream
using namespace std;#include vectorvoid printVector(vectorint v) {for (vectorint::iterator it v.begin(); it ! v.end(); it) {cout *it ;}cout endl;
}//赋值操作
void test01()
{vectorint v1; //无参构造for (int i 0; i 10; i){v1.push_back(i);}printVector(v1);vectorintv2;v2 v1;printVector(v2);vectorintv3;v3.assign(v1.begin(), v1.end());printVector(v3);vectorintv4;v4.assign(10, 100);printVector(v4);
}int main() {test01();system(pause);return 0;
}
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100 100 100 100 100 100
请按任意键继续. . .总结 vector赋值方式比较简单使用operator或者assign都可以
3.4 vector容量和大小
功能描述
对vector容器的容量和大小操作
函数原型 empty(); //判断容器是否为空 capacity(); //容器的容量 size(); //返回容器中元素的个数 resize(int num); //重新指定容器的长度为num若容器变长则以默认值填充新位置。 //如果容器变短则末尾超出容器长度的元素被删除。 resize(int num, elem); //重新指定容器的长度为num若容器变长则以elem值填充新位置。 //如果容器变短则末尾超出容器长度的元素被删除
示例
#include iostream
using namespace std;#include vectorvoid printVector(vectorint v) {for (vectorint::iterator it v.begin(); it ! v.end(); it) {cout *it ;}cout endl;
}void test01()
{vectorint v1;for (int i 0; i 10; i){v1.push_back(i);}printVector(v1);if (v1.empty()){cout v1为空 endl;}else{cout v1不为空 endl;cout v1的容量 v1.capacity() endl;cout v1的大小 v1.size() endl;}//resize 重新指定大小 若指定的更大默认用0填充新位置可以利用重载版本替换默认填充v1.resize(15, 10);printVector(v1);//resize 重新指定大小 若指定的更小超出部分元素被删除v1.resize(5);printVector(v1);
}int main() {test01();system(pause);return 0;
}
0 1 2 3 4 5 6 7 8 9
v1不为空
v1的容量 13
v1的大小 10
0 1 2 3 4 5 6 7 8 9 10 10 10 10 10
0 1 2 3 4
请按任意键继续. . .总结
判断是否为空 — empty返回元素个数 — size返回容器容量 — capacity重新指定大小 — resize
3.5 vector插入和删除
功能描述
对vector容器进行插入、删除操作
函数原型
push_back(ele); //尾部插入元素elepop_back(); //删除最后一个元素insert(const_iterator pos, ele); //迭代器指向位置pos插入元素eleinsert(const_iterator pos, int count,ele);//迭代器指向位置pos插入count个元素eleerase(const_iterator pos); //删除迭代器指向的元素erase(const_iterator start, const_iterator end);//删除迭代器从start到end之间的元素clear(); //删除容器中所有元素
示例
#include iostream
using namespace std;#include vectorvoid printVector(vectorint v) {for (vectorint::iterator it v.begin(); it ! v.end(); it) {cout *it ;}cout endl;
}//插入和删除
void test01()
{vectorint v1;//尾插v1.push_back(10);v1.push_back(20);v1.push_back(30);v1.push_back(40);v1.push_back(50);printVector(v1);//尾删v1.pop_back();printVector(v1);//插入v1.insert(v1.begin(), 100);printVector(v1);v1.insert(v1.begin(), 2, 1000); // 插入两个1000printVector(v1);//删除v1.erase(v1.begin());printVector(v1);//清空v1.erase(v1.begin(), v1.end());v1.clear();printVector(v1);
}int main() {test01();system(pause);return 0;
}10 20 30 40 50
10 20 30 40
100 10 20 30 40
1000 1000 100 10 20 30 40
1000 100 10 20 30 40请按任意键继续. . .总结
尾插 — push_back尾删 — pop_back插入 — insert (位置迭代器)删除 — erase 位置迭代器清空 — clear
3.6 vector数据存取
功能描述
对vector中的数据的存取操作
函数原型
at(int idx); //返回索引idx所指的数据operator[]; //返回索引idx所指的数据front(); //返回容器中第一个数据元素back(); //返回容器中最后一个数据元素
示例
#include iostream
using namespace std;#include vectorvoid test01()
{vectorintv1;for (int i 0; i 10; 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的第一个元素为 v1.front() endl;cout v1的最后一个元素为 v1.back() endl;
}int main() {test01();system(pause);return 0;
}0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
v1的第一个元素为 0
v1的最后一个元素为 9
请按任意键继续. . .总结
除了用迭代器获取vector容器中元素[ ]和at也可以front返回容器第一个元素back返回容器最后一个元素
3.7 vector互换容器
功能描述
实现两个容器内元素进行互换
函数原型
swap(vec); // 将vec与本身的元素互换
示例
#include iostream
using namespace std;#include vectorvoid printVector(vectorint v) {for (vectorint::iterator it v.begin(); it ! v.end(); it) {cout *it ;}cout endl;
}void test01()
{vectorintv1;for (int i 0; i 10; i){v1.push_back(i);}printVector(v1);vectorintv2;for (int i 10; i 0; i--){v2.push_back(i);}printVector(v2);//互换容器cout 互换后 endl;v1.swap(v2);printVector(v1);printVector(v2);
}void test02()
{vectorint v;for (int i 0; i 100000; i) {v.push_back(i);}cout v的容量为 v.capacity() endl;cout v的大小为 v.size() endl;v.resize(3);cout v的容量为 v.capacity() endl;cout v的大小为 v.size() endl;//收缩内存vectorint(v).swap(v); //匿名对象会根据v.size去创建一个匿名函数cout v的容量为 v.capacity() endl;cout v的大小为 v.size() endl;
}int main() {test01();test02();system(pause);return 0;
}
0 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1
互换后
10 9 8 7 6 5 4 3 2 1
0 1 2 3 4 5 6 7 8 9
v的容量为138255
v的大小为100000
v的容量为138255
v的大小为3
v的容量为3
v的大小为3
请按任意键继续. . .
总结swap可以使两个容器互换可以达到实用的收缩内存效果
3.8 vector预留空间
功能描述
减少vector在动态扩展容量时的扩展次数
函数原型
reserve(int len);//容器预留len个元素长度预留位置不初始化元素不可访问。
示例
#include iostream
using namespace std;
#include vectorvoid test01()
{vectorint v;//预留空间v.reserve(100000);int num 0;int* p NULL;for (int i 0; i 100000; i) {v.push_back(i);if (p ! v[0]) {p v[0];num;}}cout num: num endl;
}int main() {test01();system(pause);return 0;
}num:1
请按任意键继续. . .总结如果数据量较大可以一开始利用reserve预留空间