#include
using namespace std;
//函数模板
template<typename T> //声明一个模板
void mySwap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
void test01()
{
int a = 10;
int b = 20;
//两种方式使用函数模板
//1.自动类型推导
mySwap(a, b);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
//2.显示指定类型
mySwap<int>(a, b);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
}
int main()
{
test01();
return 0;
}
#include
#include
#include
using namespace std;
//vector容器存放内置数据类型
void myPrint(int val)
{
cout << val << endl;
}
void test01()
{
//创建了一个vector容器,数组
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++;
}
//第二种遍历方式
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}
//第三种遍历方式
for_each(v.begin(), v.end(), myPrint);
}
int main()
{
test01();
return 0;
}
#include
#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 test01()
{
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
//向容器中添加数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//遍历容器中的数据
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << (*it).m_Name << "年龄:" << (*it).m_Age << endl;
}
}
int main()
{
test01();
return 0;
}
#include
#include
#include
#include
using namespace std;
//容器嵌套
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);
}
//将小容器插入到大容器
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
//通过大容器把所有数据遍历
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();
return 0;
}
#include
#include
using namespace std;
void printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector容器构造
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
//通过区间方式进行构造
vector<int>v2(v1.begin(), v1.end());
printVector(v2);
//n个elem方式构造
vector<int>v3(10, 100);
printVector(v3);
}
int main()
{
test01();
return 0;
}
#include
#include
using namespace std;
void printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector容器容量大小操作
void test01()
{
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;
cout << "v1容量为:" << v1.capacity() << endl;
cout << "v1的大小为:" << v1.size() << endl;
}
//重新指定大小
v1.resize(15);
printVector(v1); //如果重新制定的比原来长,默认用0填充新的位置
}
int main()
{
test01();
return 0;
}
#include
#include
using namespace std;
void printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector容器互换
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
vector<int>v2;
for (int i = 10; i > 0; i--)
{
v2.push_back(i);
}
printVector(v2);
cout << "交换后" << endl;
v1.swap(v2);
printVector(v1);
printVector(v2);
}
//2.实际用途
//巧用swap可以收缩内存空间
void test02()
{
vector<int>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;
/***************/
vector<int>(v).swap(v);
cout << "v的容量为: " << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
}
int main()
{
test01();
test02();
return 0;
}
#include
#include
using namespace std;
void printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector预留空间
void test01()
{
vector<int>v;
//利用reserve预留空间
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++;
}
}
//printVector(v);
cout << "num=" << num << endl;
}
int main()
{
test01();
return 0;
}
#include
#include
using namespace std;
//string的构造函数
void test01()
{
string s1; //默认构造
const char* str = "hello world";
string s2(str); //使用字符串初始化
cout << "s2= " << s2 << endl;
string s3(s2); //使用一个string对象初始化另一个string对象
cout << "s3= " << s3 << endl;
string s4(10, 'a'); //使用n个字符初始化
cout << "s4= " << s4 << endl;
}
int main()
{
test01();
return 0;
}
#include
#include
using namespace std;
//string字符存取
void test01()
{
string str = "hello world";
//1.通过[]访问单个字符
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;
//2.通过at方式访问单个字符
for (int i = 0; i < str.size(); i++)
{
cout << str.at(i) << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
#include
#include
#include
using namespace std;
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//deque容器排序
void test01()
{
deque<int>d;
d.push_back(10);
d.push_back(5);
d.push_back(30);
d.push_back(100);
d.push_back(200);
d.push_back(300);
printDeque(d);
//对于支持随机访问的迭代器的容器,都可以利用sort算法直接排序
sort(d.begin(), d.end());
printDeque(d);
}
int main()
{
test01();
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
class Person
{
public:
Person(string name, int score)
{
this->m_name = name;
this->m_score = score;
}
string m_name;
int m_score;
};
void createPerson(vector<Person>& v)
{
string nameSeed = "ABCDE";
for (int i = 0; i < 5; i++)
{
string name = "选手";
name += nameSeed[i];
int score = 0;
Person p(name, score);
//将创建的Person对象放入到容器中
v.push_back(p);
}
}
void setScore(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
//奖评委的分数放入到deque容器中
deque<int>d;
for (int i = 0; i < 10; i++)
{
int score = rand() % 41 + 60;
d.push_back(score);
}
//排序
sort(d.begin(), d.end());
//去除最高最低分
d.pop_back();
d.pop_front();
//取平均分;
int sum = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
{
sum += *dit;
}
int avg = sum / d.size();
//将平均分赋值给选手
it->m_score = avg;
}
}
void showScore(vector<Person>& v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << it->m_name << "平均分:" << it->m_score << endl;
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
//创建5名选手
vector<Person>v;
createPerson(v);
//打分
setScore(v);
//显示最后得分
showScore(v);
return 0;
}
#include
#include
using namespace std;
//list数据存取
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
cout << "第一个元素" << L1.front() << endl;
cout << "最后一个元素" << L1.back() << endl;
//验证迭代器不支持随机访问
list<int>::iterator it = L1.begin();
it++;
//it=it+1(wrong)
}
int mian()
{
test01();
return 0;
}
#include
#include
#include
using namespace std;
void printList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//list数据存取
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(50);
L1.push_back(70);
L1.push_back(40);
printList(L1);
L1.reverse();
cout << "反转后:" << endl;
printList(L1);
L1.sort();
cout << "排序后:" << endl;
printList(L1);
}
int main()
{
test01();
return 0;
}
#include
#include
#include
#include
using namespace std;
class Student
{
public:
Student(string name, int age, int height)
{
this->m_name = name;
this->m_age = age;
this->m_height = height;
}
string m_name;
int m_age;
int m_height;
};
bool compare(Student a, Student b)
{
if (a.m_age < b.m_age)
return true;
else if (a.m_age == b.m_age && a.m_height > b.m_height)
return true;
else
return false;
}
int main()
{
list<Student>L;
Student s1("林杨",18,180);
Student s2("余周周", 18, 165);
Student s3("楚天阔", 20, 185);
Student s4("陈见夏", 18, 170);
Student s5("余淮", 17, 182);
L.push_back(s1);
L.push_back(s2);
L.push_back(s3);
L.push_back(s4);
L.push_back(s5);
for (list<Student>::iterator it = L.begin(); it != L.end(); it++)
{
cout << "姓名:" << (*it).m_name << "年龄:" << (*it).m_age << "身高:" << (*it).m_height << endl;
}
cout << "排序后:" << endl;
L.sort(compare);
for (list<Student>::iterator it = L.begin(); it != L.end(); it++)
{
cout << "姓名:" << (*it).m_name << "年龄:" << (*it).m_age << "身高:" << (*it).m_height << endl;
}
return 0;
}
#include
#include
using namespace std;
void printSet(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
//插入数据只有insert
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(30);
s1.insert(40);
//set特点:所有元素插入时会自动排序,不允许插入重复值
printSet(s1);
set<int>s2(s1);
printSet(s2);
}
int main()
{
test01();
return 0;
}
#include
#include
using namespace std;
void test01()
{
set<int>s1;
//插入数据只有insert
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(30);
s1.insert(40);
int num = s1.count(30);
cout << "30的个数:" << num << endl;
//查找
set<int>::iterator pos = s1.find(300);
if (pos != s1.end())
{
cout << "找到元素" << *pos << endl;
}
else
{
cout << "未找到元素" << endl;
}
}
int main()
{
test01();
return 0;
}
#include
#include
using namespace std;
void test01()
{
//1.
pair<string, int>p("Tom", 20);
cout << "姓名:" << p.first << "年龄:" << p.second << endl;
//2.
pair<string, int>p2 = make_pair("Jerry", 30);
cout << "姓名:" << p2.first << "年龄:" << p2.second << endl;
}
int main()
{
test01();
return 0;
}
set内置数据类型按序输出
#include
#include
#include
using namespace std;
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(50);
s1.insert(30);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//指定排序规则为从大到小
set<int,MyCompare>s2;
s2.insert(10);
s2.insert(40);
s2.insert(20);
s2.insert(50);
s2.insert(30);
for (set<int,MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
#include
#include
using namespace std;
void printMap(map<int,int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key=" << (*it).first << "value=" << (*it).second << endl;
}
cout << endl;
}
//map容器构造与创建
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
printMap(m);
//拷贝构造
map<int, int>m2(m);
printMap(m2);
}
int main()
{
test01();
return 0;
}
#include
#include
#include
#include
using namespace std;
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
class Worker
{
public:
string m_Name;
int m_Salary;
};
void createWorker(vector<Worker>& v)
{
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
Worker worker;
worker.m_Name = "员工";
worker.m_Name += nameSeed[i];
worker.m_Salary = rand() % 10000 + 10000;
v.push_back(worker);
}
}
//员工分组
void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
{
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
{
int deptId = rand() % 3;
m.insert(make_pair(deptId, *it));
}
}
void showWorkerByGroup(multimap<int, Worker>& m)
{
cout << "策划部门:" << endl;
multimap<int,Worker>::iterator pos=m.find(CEHUA);
int count = m.count(CEHUA);
int index = 0;
for (;pos != m.end()&&index<count; pos++,index++)
{
cout << "姓名:" << pos->second.m_Name << " 工资:" << pos->second.m_Salary << endl;
}
cout << "研发部门:" << endl;
multimap<int, Worker>::iterator pos1 = m.find(YANFA);
int count1 = m.count(YANFA);
int index1 = 0;
for (; pos1 != m.end() && index1 < count1; pos1++, index1++)
{
cout << "姓名:" << pos1->second.m_Name << " 工资:" << pos1->second.m_Salary << endl;
}
cout << "美术部门:" << endl;
multimap<int, Worker>::iterator pos2 = m.find(MEISHU);
int count2 = m.count(MEISHU);
int index2 = 0;
for (; pos2 != m.end() && index2 < count2; pos2++, index2++)
{
cout << "姓名:" << pos2->second.m_Name << " 工资:" << pos2->second.m_Salary << endl;
}
}
int main()
{
//创建员工
vector<Worker>vWorker;
createWorker(vWorker);
//员工分组
multimap<int, Worker>mWorker;
setGroup(vWorker, mWorker);
//分组显示员工
showWorkerByGroup(mWorker);
return 0;
}
#include
#include
using namespace std;
//negate一元仿函数 取反仿函数
void test01()
{
negate<int>n;
cout << n(50) << endl;
}
//plus 二元仿函数 加法
void test02()
{
plus<int>p;
cout << p(10, 20) << endl;
}
int main()
{
test01();
test02();
return 0;
}
#include
#include
#include
#include
using namespace std;
void print01(int val)
{
cout << val << " ";
}
class print02
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
//常用遍历算法 for_each
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), print01);
cout << endl;
for_each(v.begin(), v.end(), print02());
cout << endl;
}
int main()
{
test01();
return 0;
}
#include
#include
#include
#include
using namespace std;
void print01(int val)
{
cout << val << " ";
}
class Transform
{
public:
int operator()(int val)
{
return val;
}
};
class MyPrint
{
public:
void operator()(int val)
{
cout<< val<<" ";
}
};
//常用遍历算法 for_each
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>vTarget; //目标容器
vTarget.resize(v.size()); //提前开辟空间
transform(v.begin(), v.end(), vTarget.begin(), Transform());
for_each(vTarget.begin(), vTarget.end(),MyPrint());
cout << endl;
}
int main()
{
test01();
return 0;
}