#include"string.h"
#include"vector.h"
#include"list.h"
#include"stack.h"
#include"queue.h"
#include"set.h"
#include"map.h"
class people {
public:
people(int age1, string name1) {
age = age1;
name = name1;
}
int age;
string name;
};
class person {
public:
person(int age1, string name1, int lengthl) {
age = age1;
name = name1;
length = lengthl;
}
int age;
string name;
int length;
};
bool mycompare(people& p1, people& p2) {
return p1.age < p2.age;
}
bool mycompare2(person& p1, person& p2) {
if (p1.age == p2.age) {
return p1.length < p2.length;
}
return p1.age < p2.age;
}
namespace map_test{
void test(map<int,int>& t) {
for (map<int, int>::iterator it = t.begin(); it != t.end(); it++)
{
cout << "map的键为:"<<(*it).first<<" map的值为:" <<(*it).second << endl;
}
}
}
//使用仿函数进行set容器的排序规则
class mysetcompare {
public:
bool operator()(int val1, int val2)
{
return val1 > val2;
}
};
void test1() {
vector<int>s9;
int* p = NULL;
int num = 0;
s9.reserve(1000000);
for (int i = 0; i < 1000000; i++) {
s9.push_back(i);
if (p != &s9[0])
{
p = &s9[0]; //由于动态扩展机制,当容量不够的时候会进行动态扩展,这时候p就不等于&s9[0],这时候就相当于进行了一次动态扩展
num++; //这段代码可以查看动态扩展的次数
}
}
cout << num << endl;
}
int main()
{
{ //string 和vector容器使用
string s1;
s1 = "gouwangqi";
string s2(s1);
cout << s1 << endl;
cout << s2 << endl;
vector<int>s3;
s3 = { 1,2,3,4,5,6 };
vector<int>s4(s3);
for (auto iter = s3.begin(); iter != s3.cend(); iter++)
{
cout << *iter << endl;
}
for (auto iter = s4.begin(); iter != s4.cend(); iter++)
{
cout << *iter << endl;
}
s3.push_back(7); //尾插法
s3.push_back(8);
s3.push_back(9);
s3.pop_back(); //尾删法
s3.pop_back();
s4.push_back(7);
s4.push_back(8);
s4.push_back(9);
s4.pop_back();
s4.pop_back();
for (auto iter = s3.begin(); iter != s3.cend(); iter++)
{
cout << *iter << endl;
}
for (auto iter = s4.begin(); iter != s4.cend(); iter++)
{
cout << *iter << endl;
}
cout << s3.front() << endl;//front()返回第一个元素的值
cout << s3.back() << endl;//back()返回最后一个元素的值
cout << s3.size() << endl;//计算容器的大小
auto iter1 = s3.begin(); //begin()返回第一个元素的迭代器
auto iter2 = s3.cend();//begin()返回最后一个元素的迭代器
s3.insert(iter1, 399);//向指定位置插入元素
cout << *iter1 << endl;
auto iter3 = s3.cend() - 3;
s3.erase(iter1, iter3);//删除两个迭代器之间的元素
for (auto iter = s3.begin(); iter != s3.cend(); iter++)
{
cout << *iter << "我被干掉了" << endl;
}
cout << s3.capacity() << endl;
s3.push_back(7);
s3.push_back(8);
s3.push_back(9);
s3.push_back(7);
s3.push_back(8);
s3.push_back(9);
s3.push_back(7);
s3.push_back(8);
s3.push_back(9);
cout << s3.capacity() << endl;
s3.push_back(9);
s3.push_back(9);
s3.push_back(9);
cout << s3.capacity() << endl;
cout << s3.size() << endl;
vector<int>s6{ 1,2,3,4,5,6 };
for (auto iter = s6.begin(); iter != s6.cend(); iter++)
{
cout << *iter << endl;
}
cout << s3.size() << endl;
s3.resize(500);
for (auto iter = s3.begin(); iter != s3.cend(); iter++)
{
cout << *iter << endl;
}
cout << !s3.empty() << endl;
cout << s3.at(9) << endl; //返回下标索引值所指向的数据
s3.insert(s3.begin(), 200);
s3.insert(s3.end(), 300);
cout << s3.front() << endl;
cout << s3.back() << endl;
s3.erase(s3.begin(), s3.end()); //删除两个迭代器之间的元素
if (!s3.empty()) {
for (auto iter = s6.begin(); iter != s6.cend(); iter++)
{
cout << *iter << endl;
}
}
else {
cout << "erase成功!!!" << endl;
}
s3.resize(3);
cout << s3.capacity() << endl;
cout << s3.size() << endl;
vector<int>(s3).swap(s3); //使用这个式子来节约内存
cout << s3.capacity() << endl;
cout << s3.size() << endl;
//正向迭代器叫做iterator vector的迭代器是随机访问迭代器 支持跳跃式的访问
for (vector<int>::iterator iter = s6.begin(); iter != s6.end(); iter++)
{
cout << *iter << "正向迭代器" << endl;
}
//逆向迭代器叫做reverse::iterator vector的迭代器是随机访问迭代器 支持跳跃式的访问
for (vector<int>::reverse_iterator iter = s6.rbegin(); iter != s6.rend(); iter++)
{
cout << *iter << "反向迭代器" << endl;
}
//使用reserve预留空间
//使用reserve预留空间预先留空间的大小,防止多次进行动态扩展,浪费时间
test1();
vector<char>s; //vector容器可以存储char类型的数据
s.push_back('w');
for (vector<char>::iterator iter = s.begin(); iter != s.end(); iter++)
{
cout << *iter << "正向迭代器" << endl;
}
vector<char>sw; //vector容器可以存储char类型的数据
sw.push_back('s');
for (vector<char>::iterator iter = sw.begin(); iter != sw.end(); iter++)
{
cout << *iter << "字符vector" << endl;
}
//vector容器可以存储自定义类型的数据
people people1(20, "q");
people people2(30, "w");
people people3(40, "e");
people people4(50, "r");
people people5(60, "t");
people people6(70, "x");
vector<people>sw1;
sw1.push_back(people1);
sw1.push_back(people2);
sw1.push_back(people3);
sw1.push_back(people4);
sw1.push_back(people5);
sw1.push_back(people6);
for (auto iter = sw1.begin(); iter != sw1.end(); iter++)
{
cout << (*iter).age << "vector存储自定义类型people" << endl;
cout << (*iter).age << "vector存储自定义类型people" << endl;
}
}
{
//list和map容器使用
//基本操作与vector基本相同 list迭代器没有随机访问的能力 只能递增或者递减 取值等操作
//插入和删除操作都不会使原有的迭代器失效,这在vector中是不可能实现的
//list是一个双向循环链表 指针可以往前走也可以往后走
list<people>l;
people l1(10, "xiaoming");
people l2(20, "liming");
people l3(30, "zhangming");
people l4(40, "xiaohua");
people l5(50, "lihua");
people l6(60, "xiaoli");
people l7(60, "xiaoli");
people l8(60, "xiaoli");
people l9(60, "xiaoli");
people l10(70, "xiaoai");
people l11(70, "xiaoai");
l.push_back(l1);
l.push_back(l2);
l.push_back(l3);
l.push_back(l4);
l.push_back(l5);
l.push_back(l6);
l.push_back(l7);
l.sort(mycompare); //对于自定义类型的数据 要给定排序顺序
for (auto iter = l.begin(); iter != l.cend(); iter++)
{
cout << (*iter).age << endl;
cout << (*iter).name<< endl;
}
//list高级排序 多条件进行排序
list<person>m;
person m1(10, "xiaoming", 22);
person m2(20, "liming", 33);
person m3(30, "zhangming", 44);
person m4(40, "xiaohua", 55);
person m5(50, "lihua", 55);
person m6(60, "xiaoli", 55);
person m7(60, "xiaoli", 76);
person m8(60, "xiaoli", 56);
person m9(60, "xiaoli", 66);
person m10(70, "xiaoai", 55);
person m11(70, "xiaoai", 77);
m.push_back(m1);
m.push_back(m2);
m.push_back(m3);
m.push_back(m4);
m.push_back(m5);
m.push_back(m6);
m.push_back(m7);
m.push_back(m8);
m.push_back(m9);
m.push_back(m10);
m.push_back(m11);
m.push_front(m10);//除了能够在后面插入之外也可以在前面插入
m.push_front(m11);
m.sort(mycompare2); //对于自定义类型的数据 对多个条件进行排序
for (auto iter = m.begin(); iter != m.end(); iter++)
{
cout << (*iter).age<<" " << (*iter).name<<" " <<(*iter).length<<endl;
}
//m.erase(m4);
//容器删除自定义类型的时候要在类内重载等号操作符 本质上删除操作是遍历迭代器并且进行判等操作
{
//set容器
//基本操作与vector基本相同
//默认好排序 从小到大
set<int>q{1,6,5,44,13,51,65,288,153,16,84651,545,41,68,46,5};
for (auto iter = q.begin(); iter != q.end(); iter++)
{
cout << *iter << endl;
}
q.insert(100);
q.insert(200);
q.insert(300);
q.insert(400);
q.insert(100);
q.insert(200);
q.insert(300);
q.insert(400);
q.insert(100);
q.insert(200);
q.insert(300);
q.insert(400);
for (auto iter = q.begin(); iter != q.end(); iter++)
{
cout << *iter << endl;
}
cout<<q.count(400)<<endl; //结果为1 插入重复元素之后只能插入一次 cout对容器内元素进行计数 由于set不允许有重复的值 所以count返回的值只有0和1两种
q.lower_bound(300);//返回第一个大于或者等于300的元素的迭代器
q.lower_bound(300);//返回第一个小于或者等于300的元素的迭代器
set<int,mysetcompare>q2{ 1,6,5,44,13,51,65,288,153,16,84651,545,41,68,46,5 };
for (auto iter = q2.begin(); iter != q2.end(); iter++)
{
cout << *iter<<"预先指定set的排序规则" << endl;
}
}
//map容器
//基本操作与vector基本相同
map<int, int>w;
w.insert(map<int, int>::value_type(1, 2));
w.insert(pair<int, int>(2, 3));
w.insert(pair<int, int>(3, 4));
w.insert(make_pair(4, 5));
w[5] = 6;
map_test::test(w);
}
system("pause");
return 0;
};