函数模板作用
语法
template<typename T>
函数声明或定义
#include
using namespace std;
//函数模板
template<typename T> //声明一个模板,告诉编译器后面代码紧跟的T不要报错,T是一个通用类型
void mySwap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
void test1()
{
// 两种方式使用函数模板
// 1.自动类型推导
int a = 20;
int b = 40;
mySwap(a, b);
cout << "a = " << a << endl; // 40
cout << "b = " << b << endl; // 20
// 2.显示指定类型
mySwap<int>(a, b);
cout << "a = " << a << endl; // 20
cout << "b = " << b << endl; // 40
}
int main()
{
test1();
system("pause");
return 0;
}
#include
using namespace std;
template<typename T>
void mySwap(T &a,T &b)
{
T temp = a;
a = b;
b = temp;
}
//模板必须要确定出T的数据类型,才可以使用
template<typename T>
void func()
{
cout << "func的调用" << endl;
}
void test1() {
int a = 10;
int b = 30;
char c = 'a';
mySwap(a, b);
// mySwap(a, c); // error,推导不出不一致的T类型
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
void test2()
{
func<int>();
}
int main()
{
//test1();
test2();
system("pause");
return 0;
}
#include
using namespace std;
template<typename T>
void mySwap(T &a,T &b)
{
T temp = a;
a = b;
b = temp;
}
template<typename T>
void arrSort(T arr[],int len)
{
for (int i = 0;i < len;i++) {
int max = i;
for (int j = i + 1;j < len;j++) {
if (arr[max] < arr[j]) {
max = j;
}
}
if(max != i){
mySwap(arr[max], arr[i]);
}
}
}
template<typename T>
void showArr(T arr[], int len)
{
for (int i = 0;i < len;i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void test1()
{
char arr[] = "abefcej";
arrSort<char>(arr, sizeof(arr) / sizeof(char));
showArr(arr, sizeof(arr) / sizeof(char));
}
int main()
{
test1();
system("pause");
return 0;
}
#include
using namespace std;
//函数模板
template<typename T>
T myAdd2(T a, T b)
{
return a + b;
}
// 普通函数
int myAdd(int a, int b) {
return a + b;
}
void test1() {
int a = 12;
int b = 13;
char c = 's';
cout << myAdd(a, b) << endl;// 25
// 隐藏类型转化
cout << myAdd(a, c) << endl;// 127
cout << myAdd2(a, b) << endl;// 25
// 自动类型推导 -- 不能自动转换(隐式转换)
//cout << myAdd2(a, c) << endl;// error
//显示指定类型
cout << myAdd2<int>(a, c) << endl;// 127
}
int main() {
test1();
system("pause");
return 0;
}
总结: 建议使用显示指定类型的方式,调用函数模板,自己可以确定通用类型
#include
using namespace std;
void myPrint(int a,int b)
{
cout << "调用普通函数" << endl;
}
template <typename T>
void myPrint(T a, T b)
{
cout << "调用函数模板" << endl;
}
template <typename T>
void myPrint(T a, T b,T c)
{
cout << "调用函数模板的重载" << endl;
}
void test1()
{
int a = 10;
int b = 20;
//函数模板和普通函数都可以调用,优先调用普通函数
//普通函数只有函数声明,调用会报错
myPrint(a, b);
//空模板参数列表,强制调用函数模板
myPrint<>(a, b);
//函数模板可以发生重载
myPrint(a, b, 20);
//函数模板产生更好的匹配,优先调用函数模板
char a1 = 'a';
char b1 = 'b';
myPrint(a1, b1);// 调用函数模板
}
int main()
{
test1();
system("pause");
return 0;
}
#include
using namespace std;
#include
class Person
{
public:
Person(string name,int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
template<typename T>
bool myCompare(T& a, T& b)
{
if (a == b) {
return true;
}
else {
return false;
}
}
// 利用具体化Person的版本实现代码,具体优先调用
template<> bool myCompare(Person& p1, Person& p2)
{
if (p1.m_Name == p2.m_Name && p1.m_Age == p2.m_Age) {
return true;
}
else {
return false;
}
}
void test1()
{
int a = 10;
int b = 20;
bool res = myCompare(a, b);
if (res) {
cout << "a==b" << endl;
}
else {
cout << "a != b" << endl;
}
}
void test2()
{
Person person1("Jack", 20);
Person person2("Jack", 20);
bool res = myCompare(person1, person2);
if (res) {
cout << "person1 == person2" << endl;
}
else {
cout << "person1 != person2" << endl;
}
}
int main()
{
//test1();
test2();
system("pause");
return 0;
}
类模板的作用
语法
template<class T>
类
#include
using namespace std;
#include
//类模板
template<class NameType,class AgeType>
class Person
{
public:
Person(NameType name,AgeType age) {
this->m_Name = name;
this->m_Age = age;
}
void showPerson() {
cout << "name:" << this->m_Name << ",age:" << this->m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
void test1()
{
Person<string, int> p1("类模板", 20);
p1.showPerson();
}
int main()
{
test1();
system("pause");
return 0;
}
#include
using namespace std;
//类模板和函数模板的区别
//类模板在参数列表中可以有默认参数 AgeType = int
template<class NameType,class AgeType = int>
class Person
{
public:
Person(NameType name,AgeType age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "name:" << this->m_Name << ",age:" << this->m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
void test1()
{
// 类模板没有自动化类型推导使用方式
//Person p("数字化", 10);
Person<string, int> p("数字化", 10);
p.showPerson();
}
void test2()
{
Person<string> p("智能化", 10);
p.showPerson();
}
int main()
{
//test1();
test2();
system("pause");
return 0;
}
#include
using namespace std;
class Person1
{
public:
void showPerson1()
{
cout << "Person1 show" << endl;
}
};
class Person2
{
public:
void showPerson2()
{
cout << "Person2 show" << endl;
}
};
template<class T>
class MyClass
{
public:
T obj;
//类模板的成员函数
void fun1() {
obj.showPerson1();
}
void fun2() {
obj.showPerson2();
}
};
void test1() {
MyClass<Person1> m;
m.fun1();
// 类模板中成员函数在调用时创建
// m.fun2();
}
int main() {
system("pause");
return 0;
}
#include
using namespace std;
template<class T,class K>
class Person
{
public:
Person(T name, K age) {
this->m_Name = name;
this->m_Age = age;
}
void showPerson() {
cout << "name:" << this->m_Name << ",age:" << this->m_Age << endl;
}
T m_Name;
K m_Age;
};
// 1.指定传入类型(推荐)
void printPerson1(Person<string, int> &p)
{
p.showPerson();
}
void test1() {
Person<string, int> p("智能化", 120);
printPerson1(p);
}
// 2.参数模板化
template<class T,class K>
void printPerson2(Person<T, K>& p) {
p.showPerson();
//查看推导的类型
cout << "T的类型为:" << typeid(T).name() << endl; //class std::basic_string,class std::allocator >
cout << "K的类型为:" << typeid(K).name() << endl; //int
}
void test2() {
Person<string, int> p("数字化", 10);
printPerson2(p);
}
// 3.整个类模板化
template<class T>
void printPerson3(T &p) {
p.showPerson();
cout << "T的数据类型: " << typeid(T).name() << endl;
}
void test3() {
Person<string, int> p("自动化", 30);
printPerson3(p);
}
int main() {
//test1();
//test2();
test3();
system("pause");
return 0;
}
#include
using namespace std;
#include
template<class T,class K>
class Person
{
public:
Person(T name, K age);
/*{
this->m_Name = name;
this->m_Age = age;
}*/
void showPerson();
/*{
cout << "姓名: " << this->m_Name << ",年龄:" << this->m_Age << endl;
}*/
T m_Name;
K m_Age;
};
//构造函数的类外实现
template<class T,class K>
Person<T,K>::Person(T name, K age)
{
this->m_Name = name;
this->m_Age = age;
}
// 成员函数的类外实现
template<class T,class K>
void Person<T, K>::showPerson()
{
cout << "姓名: " << this->m_Name << ",年龄:" << this->m_Age << endl;
}
void test1()
{
Person<string,int> p("智能组装", 8);
p.showPerson();
}
int main() {
test1();
system("pause");
return 0;
}
#include
using namespace std;
#include
template<class T,class K>
class Person
{
public:
Person(T name, K age);
/*{
this->m_Name = name;
this->m_Age = age;
}*/
void showPerson();
/*{
cout << "姓名: " << this->m_Name << ",年龄:" << this->m_Age << endl;
}*/
T m_Name;
K m_Age;
};
//构造函数的类外实现
template<class T,class K>
Person<T,K>::Person(T name, K age)
{
this->m_Name = name;
this->m_Age = age;
}
// 成员函数的类外实现
template<class T,class K>
void Person<T, K>::showPerson()
{
cout << "姓名: " << this->m_Name << ",年龄:" << this->m_Age << endl;
}
void test1()
{
Person<string,int> p("智能组装", 8);
p.showPerson();
}
int main() {
test1();
system("pause");
return 0;
}
类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到
解决方式:
#pragma once
#include
using namespace std;
#include
template<class T,class K>
class Person
{
public:
Person(T name,K age);
void showPerson();
T m_Name;
K m_Age;
};
#include "person.h"
template<class T, class K>
Person<T, K>::Person(T name, K age)
{
this->m_Name = name;
this->m_Age = age;
}
template<class T, class K>
void Person<T, K>::showPerson()
{
cout << "姓名: " << this->m_Name << ",年龄:" << this->m_Age << endl;
}
#include
using namespace std;
#include
// 1.第一种解决方式,直接包含 源文件
//#include "person.cpp"
// 2.第二种解决方式,将.h和.cpp中的内容写到一起,将后缀名改为.hpp文件
#include"person.hpp"
//template
//class Person
//{
//public:
// Person(T name, K age);
// void showPerson();
// T m_Name;
// K m_Age;
//};
//
//template
//Person::Person(T name, K age)
//{
// this->m_Name = name;
// this->m_Age = age;
//}
//
//template
//void Person::showPerson()
//{
// cout << "姓名: " << this->m_Name << ",年龄:" << this->m_Age << endl;
//}
void test1()
{
Person<string,int> person("数据化", 20);
person.showPerson();
}
int main()
{
test1();
system("pause");
return 0;
}
#pragma once
#include
using namespace std;
#include
template<class T, class K>
class Person
{
public:
Person(T name, K age);
void showPerson();
T m_Name;
K m_Age;
};
template<class T, class K>
Person<T, K>::Person(T name, K age)
{
this->m_Name = name;
this->m_Age = age;
}
template<class T, class K>
void Person<T, K>::showPerson()
{
cout << "姓名: " << this->m_Name << ",年龄:" << this->m_Age << endl;
}
#include
#include
using namespace std;
// 声明Person类
template<class T, class K>
class Person;
//类外实现
template<class T, class K>
void printPerson2(Person<T, K> &p)
{
cout << "-----------全局函数的类外实现-----------" << endl;
cout << "姓名: " << p.m_Name << ",年龄: " << p.m_Age << endl;
}
//类模板
template<class T,class K>
class Person
{
// 全局函数 类内实现
friend void printPerson(Person<T, K> p)
{
cout << "姓名: " << p.m_Name << ",年龄: " << p.m_Age << endl;
}
//全局函数 类外实现
// 加空模板参数列表
// 全局函数 是类外实现,需要让编译器提前知道这个函数的存在
friend void printPerson2<>(Person<T, K> &p);
public:
Person(T name, K age)
{
this->m_Name = name;
this->m_Age = age;
}
private:
T m_Name;
K m_Age;
};
// 1.全局函数在类内实现
void test1()
{
Person<string, int> p("Rose", 18);
printPerson(p);
}
// 2.全局函数在类外实现
void test2()
{
Person<string, int> p("Jack", 20);
printPerson2(p);
}
int main()
{
//test1();
test2();
system("pause");
return 0;
}
#pragma once
#include
using namespace std;
template<class T>
class MyArray
{
public:
MyArray(int capacity)
{
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[this->m_Capacity];
}
//析构函数
~MyArray()
{
if (this->pAddress != NULL) {
delete[] this->pAddress;
this->pAddress = NULL;
}
}
//拷贝构造
MyArray(const MyArray &arr)
{
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//this->pAddress = arr.pAddress;
//深拷贝
this->pAddress = new T[arr.m_Capacity];
//将arr中的数据都拷贝过来
for (int i = 0;i < this->m_Size;i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
// operator== 防止浅拷贝问题
MyArray& operator==(const MyArray& arr)
{
//先判断原来堆区是否有数据,如果有先释放
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
//深拷贝
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->pAddress = new T[arr.m_Capacity];
for(int i = 0;i<this->m_Size;i++)
{
this->m_Capacity[i] = arr.m_Capacity[i];
}
return *this;
}
//尾插法
void Push_Back(const T &val)
{
// 判断容量是否有空间
if (this->m_Capacity == this->m_Size)
{
return;
}
this->pAddress[this->m_Size] = val;
//更新数组大小
this->m_Size++;
}
//尾删法
void Pop_Back()
{
//让用户访问不到最后一个元素,即为尾删,逻辑删除
if (this->m_Size == 0)
{
return;
}
this->m_Size--;
}
//通过下标方式访问数组的元素
T& operator[](int index)
{
return this->pAddress[index];
}
//返回数组的容量
int getCapacity()
{
return this->m_Capacity;
}
//返回数组的大小
int getSize()
{
return this->m_Size;
}
//遍历数组
void printArray()
{
for (int i = 0;i < this->m_Size;i++)
{
cout << this->pAddress[i] << " ";
}
cout << endl;
}
private:
// 指针指向堆区开辟的真实数组
T* pAddress;
// 数据容量
int m_Capacity;
//数据大小
int m_Size;
};
#include
using namespace std;
#include "MArray.hpp"
void printArray(MyArray<int> &arr)
{
for (int i = 0;i < arr.getSize();i++)
{
cout << arr[i]<< " ";
}
cout << endl;
}
void test1()
{
MyArray<int> arr(5);
for (int i = 0;i < 5;i++)
{
//利用尾插法插入数据
arr.Push_Back(i);
}
printArray(arr);
cout << "arr的容量为: " << arr.getCapacity() << endl;
cout << "arr的容量为: " << arr.getSize() << endl;
MyArray<int> arr2(arr);
cout << "----arr2的打印输出----" << endl;
printArray(arr2);
arr2.Pop_Back();
cout << "----arr2尾删后打印输出----" << endl;
printArray(arr2);
cout << "arr2的容量为: " << arr.getCapacity() << endl;
cout << "arr2的容量为: " << arr.getSize() << endl;
//遍历数组
arr2.printArray();
}
int main()
{
test1();
system("pause");
return 0;
}
种类 | 功能 | 支持运算 |
---|---|---|
输入迭代器 | 对数据的只读访问 | 只读,支持++、==、!= |
输出迭代器 | 对数据的只写访问 | 只写,支持++ |
向前迭代器 | 读写操作,并能向前推进迭代器 | 读写,支持++、==、!= |
双线迭代器 | 读写操作,并能向前和向后操作 | 读写,支持++、-- |
随机访问迭代器 | 读写操作,可以以跳跃的方式访问任意数据,功能最强的迭代器 | 读写,支持++、--、[n]、-n、<、<=、>、>= |
vector
for_each
vector::iterator
#include
#include
#include
using namespace std;
// vector容器存放内置数据类型
//遍历
void printContainer(int val)
{
cout << val<<" ";
}
void test1()
{
//创建了一个vector容器
vector<int> vec;
//向容器中插入数据
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
vec.push_back(50);
vec.push_back(90);
vec.push_back(20);
// 通过迭代器访问容器中的数据
// 起始迭代器 指向容器中第一个元素
vector<int>::iterator itBegin = vec.begin();
// 结束迭代器 指向容器中最后一个元素的下一个位置
vector<int>::iterator itEnd = vec.end();
//第一种遍历方式
cout << "while循环遍历" << endl;
while (itBegin != itEnd)
{
cout << *itBegin<< " ";
itBegin++;
}
cout << endl;
//第二种遍历方式
cout << "for循环遍历容器" << endl;
for (vector<int>::iterator it = vec.begin();it != vec.end();it++)
{
cout << *it<<" ";
}
cout << endl;
//第三种遍历方式
for_each(vec.begin(), vec.end(), printContainer);
}
int main()
{
test1();
system("pause");
return 0;
}
#include
#include
#include
using namespace std;
// vector容器存放自定义数据类型
// 自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
//遍历
void printInfo(Person person)
{
cout << "标签: " << person.m_Name << ",年限: " << person.m_Age << endl;
}
void test1()
{
vector<Person> vp;
Person p1("数字化", 10);
Person p2("模块化", 23);
Person p3("拟人化", 1);
Person p4("智能化", 9);
Person p5("树脂化", 24);
Person p6("机械化", 11);
//向容器添加数据
vp.push_back(p1);
vp.push_back(p2);
vp.push_back(p3);
vp.push_back(p4);
vp.push_back(p5);
vp.push_back(p6);
//遍历容器中的数据
// while方式遍历
cout << "-----------while遍历---------" << endl;
vector<Person>::iterator itBegin = vp.begin();
vector<Person>::iterator itEnd = vp.end();
while (itBegin != itEnd)
{
cout << "标签: " << (*itBegin).m_Name << ",年限: " << (*itBegin).m_Age << endl;
itBegin++;
}
// for方式遍历
cout << "--------for遍历-------" << endl;
for (vector<Person>::iterator itBegin = vp.begin();itBegin != vp.end();itBegin++)
{
cout << "标签: " << (*itBegin).m_Name << ",年限: " << (*itBegin).m_Age << endl;
}
//for_each方式遍历
cout << "--------for_each遍历-------" << endl;
for_each(vp.begin(), vp.end(), printInfo);
}
int main()
{
test1();
system("pause");
return 0;
}
#include
#include
#include
using namespace std;
// vector容器存放内置数据类型
//遍历
void printContainer(int val)
{
cout << val<<" ";
}
void test1()
{
//创建了一个vector容器
vector<int> vec;
//向容器中插入数据
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
vec.push_back(50);
vec.push_back(90);
vec.push_back(20);
// 通过迭代器访问容器中的数据
// 起始迭代器 指向容器中第一个元素
vector<int>::iterator itBegin = vec.begin();
// 结束迭代器 指向容器中最后一个元素的下一个位置
vector<int>::iterator itEnd = vec.end();
//第一种遍历方式
cout << "while循环遍历" << endl;
while (itBegin != itEnd)
{
cout << *itBegin<< " ";
itBegin++;
}
cout << endl;
//第二种遍历方式
cout << "for循环遍历容器" << endl;
for (vector<int>::iterator it = vec.begin();it != vec.end();it++)
{
cout << *it<<" ";
}
cout << endl;
//第三种遍历方式
for_each(vec.begin(), vec.end(), printContainer);
}
int main()
{
test1();
system("pause");
return 0;
}
string();
string(const char * s);
string(const string & str);
string(int n,char c);
#include
using namespace std;
//string构造函数
void test1()
{
//1.默认构造函数
string s1;
//2.字符串初始化
const char* str = "hello";
string s2(str);
cout << "s2 = " << s2 << endl;
//3.一个字符串给另一个字符串初始化
string s3(s2);
cout << "s3 = " << s3 << endl;
//使用n个字符初始化;
string s4(4, 'x');
cout << "s4 = " << s4 << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
string& operator=(const char *s)
string& operator=(const string &s)
string& operator=(char c)
string & assign(const char *s)
string& assign(const char * s,int n)
string& assgin(const string &s)
string& assign(int n,char c)
#include
using namespace std;
#include
//string赋值操作
void test1()
{
string str1;
str1 = "hello";
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(4, 'g');
cout << "str7 = " << str7 << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
string& operator+=(const char* str)
string& operator+=(const char c)
string& operator+=(const string& str)
string& append(const char *s)
string& append(const char*s,int n)
string& append(const string &s)
string& append(const string &s,int pos,int n)
str7 += " bbb";
cout << "str7 = " << str7 << endl;
str7.append("hello", 2, 2);
cout << "str7 = " << str7 << endl; // gggg bbbll
int find(const string &str,int pos = 0) const
int find(const char* s,int pos = 0) const
int find(const char &s,int pos,int n) const
int find(const char c,int pos = 0) const
int rfind(const string &str,int pos = npos) const
int rfind(const char * s,int pos = npos) const
int rfind(const char &s,int pos,int n) const
int rfind(const char c,int pos = 0) const
string& replace(int pos,int n,const string& str)
string& replace(int pos,int n,const char* s)
int compare(const string &s) const
int compare(const char * s) const
string strs = "hello";
string strs1 = "hollw";
int comp = strs.compare(strs1);
cout << "comp = " << comp << endl;// -1
char& operator[](int n)
char& at(int n)
string strs = "hello";
cout << "字符: " << strs[2] << endl;// l
string& insert(int pos,const char* s)
string& insert(int pos,const string& str)
string& insert(int pos,int n,char c)
string& erase(int pos,int n = npos)
string strs = "hello";
string str12;
str12 = strs.erase(1, 3);
cout << "str12 = " << str12 << endl;// ho
string substr(int pos = 0,int n = npos) const
string strs = "hello";
string stru;
stru = strs.substr(1, 3);
cout << "stru = " << stru << endl;// ell
vector<T> v;
vector(v.begin(),v.end())
vector(n.elem)
vector(const vector &vec)
#include
using namespace std;
#include
void printVector(vector<int> &vec)
{
for (vector<int>::iterator it = vec.begin();it != vec.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
//默认无参构造函数
vector<int> v1;
for (int i = 0;i < 10;i++)
{
v1.push_back(i + 1);
}
printVector(v1);
// 通过区间方式进行构造
vector<int> v2(v1.begin(), v1.end());
printVector(v2);
//n个elem方式构造
vector<int> v3(4, 88);
printVector(v3);
//拷贝构造
vector<int> v4(v3);
printVector(v4);
}
int main()
{
test1();
system("pause");
return 0;
}
vector& operator=(const vector &vec)
assign(beg,end)
assign(n,elem)
//赋值 operator=
vector<int> v11;
v11 = v4;
printVector(v11);
//assign
vector<int> v12;
v12.assign(v4.begin(), v4.end());
printVector(v12);
empty()
capacity()
size()
resize(int num)
resize(int num,elem)
cout << "容量:" << v12.capacity() << endl;// 4
cout << "容量:" << v12.size() << endl;// 4
v12.resize(8);
cout << "容量:" << v12.capacity() << endl;// 8
cout << "容量:" << v12.size() << endl;// 8
push_back(ele)
pop_back()
insert(const_iterator pos,ele)
insert(const_iterator pos,int count,ele)
erase(const_iterator pos)
erase(const_iterator start,const_iterator end)
claer()
v12.push_back(78);
cout << "容量:" << v12.capacity() << endl;// 12
cout << "容量:" << v12.size() << endl;// 9
printVector(v12);
//插入迭代器
v12.insert(v12.begin(), 10);
printVector(v12);
at(int idx)
operator[idx]
front()
back()
swap(vec)
//巧用swap收缩内存
v.resize(3);
vector<int>(v).swap(v)
reserve(int len)
deque<T> depT
deque(beg,end)
deque(n,elem)
deque(const deque &dep)
#include
#include
using namespace std;
//添加const,避免值被修改
void printDeque(const deque<int> &dep)
{
for (deque<int>::const_iterator it = dep.begin();it != dep.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
//默认构造
deque<int> dep;
for(int i = 0;i< 10;i++)
{
dep.push_back(i + 1);
}
printDeque(dep);
deque<int> dep2(dep.begin(), dep.end());
printDeque(dep2);
}
int main()
{
test1();
system("pause");
return 0;
}
deque& operato=(const deque &dep)
assign(beg,end)
assign(n,elem)
empty()
size()
resize(num)
resize(num.elem)
push_back(elem)
push_front(elem)
pop_back()
pop_front()
insert(const_iterator pos,elem)
insert(const_iterator pos,n,elem)
insert(const_iterator pos,beg,end)
clear()
erase(beg,end)
erase(const_iterator pos)
// 在dep1前面插入dep2
dep1.insert(dep1.begin(),dep2.begin(),dep2.end())
at(idx)
operator[]
front()
back()
sort(iterator beg,iterator end)
push
pop
stack<T> stk;
stack(const stack& stack)
stack& operator=(const stack& stack)
push(elem)
pop()
top()
empty()
size()
队列容器允许从一端新增数据,另一端移除数据
队列中只有队头和队尾可以被外界使用,因此队列不允许有遍历行为
队列中进数据称为 — 入队 push
队列中出数据称为 — 出队 pop
queue<T> que;
queue(const queue &que)
queue& operator=(const queue &que)
push(elem)
pop()
back()
front()
empty()
size()
list<T> lst;
list(beg,end)
list(n,elem)
list(const list &lst)
#include
#include
using namespace std;
void printList(const list<int>& lst)
{
for (list<int>::const_iterator it = lst.begin();it != lst.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
list<int> lst;
lst.push_back(20);
lst.push_back(22);
lst.push_back(21);
lst.push_back(29);
lst.push_back(90);
printList(lst);
list<int> lst1(lst.begin(), lst.end());
printList(lst1);
list<int> lst2(5, 30);
printList(lst2);
list<int> lst3;
lst3 = lst;
printList(lst3);
}
int main()
{
test1();
system("pause");
return 0;
}
assign(beg,end)
assign(e,elem)
list& operator=(const list &lst)
swap(lst)
list<int> lst4;
lst4.assign(lst.begin(), lst.end());
printList(lst4);
lst4.swap(lst2);
printList(lst2);
printList(lst4);
size()
empty()
resize(num)
resize(num,elem)
push_back(elem)
pop_back()
push_front(elem)
pop_front()
insert(pos,elem)
insert(pos,n,elem)
insert(pos,beg,end)
clear()
erase(beg,end)
erase(pos)
remove(elem)
lst4.insert(lst4.begin(), 4, 22);
printList(lst4);
lst4.pop_back();
printList(lst4);
front()
back()
//验证迭代器是否支持随机访问
list<int>::iterator it = lst.begin();
it++;
it--;
// it = it + 1;//error,不支持随机访问
reverse()
sort()
lst.sort()
#include
using namespace std;
#include
void printSet(const set<int> &st)
{
for(set<int>::const_iterator it = st.begin();it != st.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test()
{
set<int> st;
st.insert(10);
st.insert(20);
st.insert(50);
st.insert(40);
st.insert(30);
st.insert(30);
cout << "遍历容器" << endl;
printSet(st);// 10 20 30 40 50
}
int main()
{
test();
system("pause");
return 0;
}
set<T> st
set(const set &st)
set& operator=(const set &st)
size()
empty()
swap()
insert(elem)
clear()
erase(pos)
erase(beg,end)
erase(elem)
set<int>::iterator it = st.begin();
it++;
set<int>::iterator its = st.erase(it);
cout << "下一个元素的迭代器:" << *its << endl;// 30
find(key)
count(key)
set<int>::iterator it1= st.find(20);
if (it1 != st.end())
{
cout << "找到元素:" << *it1 << endl;
}
else
{
cout << "未找到该元素" << endl;
}
int num = st.count(30);
cout << "元素的个数:" << num << endl;// 1
pair<type,type> p (value1,value2)
pair<type,type> p = make_pair(value1,value2)
#include
using namespace std;
void test()
{
//第一种方式
pair<string, int> p("Tom", 22);
cout << "姓名:" << p.first << ",年龄:" << p.second << endl;
//第二种方式
pair<string, int> p2 = make_pair("Jerry", 18);
cout << "姓名:" << p2.first << ",年龄:" << p2.second << endl;
}
int main()
{
test();
system("pause");
return 0;
}
#include
#include
#include
using namespace std;
//添加const,避免值被修改
void printDeque(const deque<int> &dep)
{
for (deque<int>::const_iterator it = dep.begin();it != dep.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
void test1()
{
//默认构造
deque<int> dep;
for(int i = 0;i< 10;i++)
{
dep.push_back(i + 1);
}
printDeque(dep);
deque<int> dep2(dep.begin(), dep.end());
printDeque(dep2);
sort(dep2.begin(), dep2.end());
printDeque(dep2);
}
int main()
{
test1();
system("pause");
return 0;
}
map<T1,T2> mp;
map(const map &mp)
map &operator=(const map& mp)
map<string,int> mp;
mp.insert(pair<string,int> ("Tom",18));
size()
empty()
swap()
insert(elem)
clear()
erase(pos)
erase(beg,end)
erase(key)
find(key)
count(key)
#include
using namespace std;
#include
#include
class MyCompare
{
public:
bool operator()(string str1,string str2)const
{
return str1 > str2;
}
};
void test()
{
map<string, int,MyCompare> mp;
//mp.insert(pair("数字化", 13));
mp.insert(make_pair("数字化", 13));//同上
mp.insert(make_pair("模块化", 31));
mp.insert(make_pair("智能化", 20));
mp.insert(make_pair("系统化", 8));
mp.insert(make_pair("助手化", 22));
for (map<string, int>::iterator it = mp.begin();it != mp.end();it++)
{
cout << "项目名称: " << (*it).first << ",项目年限:" << it->second << endl;
}
}
int main()
{
test();
system("pause");
return 0;
}