STL初始
为了建立数据结构和算法的一套标准,诞生了STL
STL:standard template library 标准模版库
STL从广义上分为:容器,算法,迭代器
容器和算法之间通过迭代器进行无缝连接
STL几乎所有的代码都采用了模板或者模板函数
STL六大组件
容器,算法,迭代器,仿函数,设配器,空间配置器
容器:各种数据结构,如vector,list,deque,set,map等用来存放数据
算法:各种常用的算法,如sort,find,copy,for_each等
迭代器:扮演了容器和算法之间的胶合剂
仿函数:行为类似函数,可作为算法的某种策略
设配器:一种用来修饰容器或者仿函数或迭代器接口的东西
空间配置器:否则空间的配置与管理
容器算法迭代器初始
容器:vector
算法:for_each
迭代器:vector<int>::iterator
实例:
#include<iostream>
using namespace std;
#include <vector>
#include<algorithm>
void test01() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
vector<int>::iterator itBegin = v.begin();
vector<int>::iterator itEnd = v.end();
while (itBegin != itEnd) {
cout << *itBegin << endl;
itBegin++;
}
}
void test02() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << endl;
}
}
void myPrint(int val) {
cout << val << endl;
}
void test03(){
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
for_each(v.begin(),v.end(),myPrint);
}
int main() {
test03();
system("pause");
return 0;
}
Vector存放自定义时间类型
#include<iostream>
using namespace std;
#include<vector>
#include<string>
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test01() {
vector<Person>v;
Person p1("aaa", 10);
Person p2("aaa", 20);
Person p3("aaa", 30);
Person p4("aaa", 40);
Person p5("aaa", 50);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
cout << "姓名:" << (*it).m_Name << "年龄:" << (*it).m_Age << endl;
cout << "姓名:" << it->m_Name << "年龄:" << it->m_Age << endl;
}
}
void test02() {
vector<Person*>v;
Person p1("aaa", 10);
Person p2("aaa", 20);
Person p3("aaa", 30);
Person p4("aaa", 40);
Person p5("aaa", 50);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
for (vector<Person *>::iterator it = v.begin(); it != v.end();it++) {
cout << "姓名:" << (*it)->m_Name << "年龄:" << (*it)->m_Age << endl;
}
}
int main() {
test02();
system("pause");
return 0;
}
vector容器嵌套容器
容器中嵌套容器,我们将所有数据进行遍历输出
#include<iostream>
using namespace std;
#include<vector>
void test01() {
vector<vector<int>>v;
vector<int>v1;
vector<int>v2;
vector<int>v3;
vector<int>v4;
for (int i = 0; i < 4; i++) {
v1.push_back(i + 1);
v2.push_back(i + 2);
v3.push_back(i + 3);
v4.push_back(i + 4);
}
for (vector<vector<int>>::iterator it = v.begin(); it != v.end();it++) {
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) {
cout << *vit << "";
cout << endl;
}
}
}
int main() {
test01();
system("pause");
return 0;
}
string容器
string是c++风格的字符串,而string吧本质是一个类
string和char *的区别
char *是一个指针
string是一类型,内部封装char * 管理这个字符串,是一个char*的容器
string构造函数
string();
实例:
string类内部封装了很多成员方法:查找find 拷贝copy 删除delete 插入insert
构造函数原型:
string();
string(const char* s);
string(const string& str);
string(int n, char c);
#### 字符串比较
按照ASCII码进行对比
=返回 0
>返回 1
<返回 -1
实例:
#include<iostream>
using namespace std;
#include<string>
void test01() {
string str1;
str1 = "hello world";
cout << "str1=" << str1 << endl;
string str2;
str2.assign("hello lijun",5);
cout << str2 << endl;
}
void test02() {
string str2 = "我";
str2 += "爱玩游戏";
cout << "str2=" << str2 << endl;
str2 += ":";
cout << str2 << endl;
string str3 = "LOF DNF";
str2 = str2 + str3;
cout << str2 << endl;
str3.append("game abcd2", 4);
cout << str3 << endl;
str3.append(str2);
cout << str3 << endl;
str3.append(str3, 0, 3);
cout << str3 << endl;
}
void test03() {
string str1 = "xello";
string str2 = "hello";
if (str1.compare(str2) == 0) {
cout << "str1等于str2" << endl;
}
else if (str1.compare(str2)) {
cout << "str大于str2" << endl;
}
else {
cout << "str1小于str2" << endl;
}
}
void test05() {
string str = "hello";
cout << "str=" << str << endl;
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 << "str=" << str << endl;
}
}
void test06() {
string str = "hello";
str.insert(1, "111");
cout << str << endl;
}
void test07() {
string str = "adbcdef";
string subStr = str.substr(1, 3);
cout << subStr << endl;
}
void test08() {
string email = "zhangsan@sina.com";
int pos = email.find("@");
cout << pos << endl;
string usrName = email.substr(0,pos);
cout << usrName << endl;
}
int main() {
test08();
system("pause");
return 0;
}