序列容器
vector
1)底层数据结构:数组
2) 支持的操作主要有:
push_back() 尾部插入元素 (只支持尾部操作)
front() 获取头部元素
back() 获取尾部元素
pop_back() 删除尾部元素
clear() 清空元素
size() 获取元素的个数
erase() 删除某个元素
注意事项: 执行erase后,erase内部会对迭代器进行自动加1
3) 操作符:支持[]操作符
4) 内存是否连续: 连续
5). 遍历元素有两种方式:
a 使用迭代器
b 获取元素个数size(),循环语句实现
6) 排数使用sort算法
7) 查 找使用查找发算法
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
using namespace std;
using std::vector;
using std::cout;
typedef vector<int> VInt;
typedef vector<int>::iterator VIter;
void printVector(VInt vInt)
{
for(VIter It = vInt.begin(); It != vInt.end(); It++)
{
cout << *It << " ";
}
cout << endl;
}
void vectorDemoInt()
{
VInt vInt;
VIter It;
srand((unsigned long)time(0));
for(int i = 0; i < 10; i++)
{
vInt.push_back(rand()%100);
}
printVector(vInt);
int nFront = vInt.front();
int nBack = vInt.back();
cout <<"nFront "<<nFront << endl;
cout << "nBack "<<nBack << endl;
vInt.pop_back();
printVector(vInt);
for(It = vInt.begin(); It != vInt.end(); It++)
{
if(*It == 5)
{
It = vInt.erase(It);
It--;
}
}
printVector(vInt);
sort(vInt.begin(),vInt.end(),comp);
printVector(vInt);
vInt.push_back(13);
It = find(vInt.begin(),vInt.end(),13);
printVector(vInt);
if(It != vInt.end())
{
cout << "found it" <<endl;
}
else
{
cout << "not found it" << endl;
}
cout << endl;
It = vInt.begin();
while (It != vInt.end())
{
if(*It == 13)
It = vInt.erase(It);
else
It++;
}
printVector(vInt);
}
int _tmain(int argc, _TCHAR* argv[])
{
vectorDemoInt();
return 0;
}
8)非内建类型类操作
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <functional>
using namespace std;
using std::vector;
using std::cout;
class Student
{
public:
Student(char* name,int age)
{
m_name = (char*)malloc(strlen(name)+1);
memset(m_name,0x00,strlen(name)+1);
strcpy_s(m_name,strlen(name)+1,name);
m_age = age;
}
virtual ~Student()
{
free(m_name);
}
bool operator == (const Student &stu) const
{
if(stu.m_age == m_age && strcmp(stu.m_name,m_name))
{
return true;
}
else
{
return false;
}
}
bool operator < (const Student &stu) const
{
if(m_age < stu.m_age)
{
return true;
}
else
{
return false;
}
}
bool operator > (const Student &stu) const
{
if(m_age > stu.m_age)
{
return true;
}
else
{
return false;
}
}
//赋值运算符
Student& operator= (const Student &stu) //防止在对象拷贝过程中,m_name指针引起的拷贝问题
{
if(this == &stu)
{
return *this;
}
m_name = (char*)malloc(strlen(stu.m_name)+1);
memset(m_name,0x00,strlen(stu.m_name)+1);
strcpy_s(m_name,strlen(stu.m_name)+1,stu.m_name);
m_age = stu.m_age;
return *this;
}
//拷贝构造函数
Student(const Student& stu) //防止在对象拷贝过程中,m_name指针引起的拷贝问题
{
m_name = (char*)malloc(strlen(stu.m_name)+1);
memset(m_name,0x00,strlen(stu.m_name)+1);
strcpy_s(m_name,strlen(stu.m_name)+1,stu.m_name);
m_age = stu.m_age;
}
public:
char* m_name;
int m_age;
};
typedef vector<Student> vStu;
typedef vector<Student>::iterator stuIt;
bool MyComp(const Student &s1, const Student &s2)
{
return s1.m_age > s2.m_age;
}
void printStu(vStu stu)
{
for(stuIt it=stu.begin(); it != stu.end(); it++)
{
cout <<"Student name"<< it->m_name <<"Student age"<< it->m_age<<endl;
}
cout << endl;
}
void vectorDemoStudent()
{
vStu stu;
stuIt it;
Student stu1("Lily",30);
stu.push_back(stu1);
Student stu2("Lucy",29);
stu.push_back(stu2);
Student stu3("Mary",28);
stu.push_back(stu3);
Student stu4("Davy",27);
stu.push_back(stu4);
printStu(stu);
//从小到大 默认
cout <<"从小到大排序结果"<<endl;
sort(stu.begin(),stu.end());
printStu(stu);
//从小到大 默认
cout <<"从小到大排序结果"<<endl;
sort(stu.begin(),stu.end(),less<Student>());
printStu(stu);
cout << "从大到小排序结果"<<endl;
sort(stu.begin(),stu.end(),greater<Student>());
printStu(stu);
cout << "从大到小排序结果"<<endl;
sort(stu.begin(),stu.end(),MyComp);
printStu(stu);
//删除元素操作
for(it = stu.begin(); it != stu.end(); it++)
{
if(0 == strcmp(it->m_name,"Lucy"))
{
it = stu.erase(it);
it--;
}
}
//删除元素后
cout << "删除元素后" << endl;
printStu(stu);
//查找元素
it = find(stu.begin(),stu.end(),Student("Lily",30));
if(it != stu.end())
{
cout << "find student lily" << endl;
}
else
{
cout << "not found lily" << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
vectorDemoStudent();
return 0;
}
deque
1)底层数据结构:队列
2) 支持的操作:
push_front() 头部插入元素
push_back() 尾部插入元素
insert() 中间插入元素
front() 获取头部元素
back() 获取尾部元素
clear() 清空元素
size() 获取元素的个数
erase() 删除某个元素
注意:执行erase后,erase内部会对迭代器进行自动加1
3)操作符:支持[]操作符
4)内存是否连续: 连续
5)排序 (使用排序算法sort)
6)查找 (使用查找算法find)
#include "stdafx.h"
#include <deque>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
using std::deque;
using std::cout;
typedef deque<int> de_q;
typedef deque<int>::iterator It;
void printdeq(de_q deq)
{
for(It it = deq.begin(); it != deq.end(); it++)
{
cout << *it <<" ";
}
cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
de_q deq;
It it;
deq.push_back(5);
deq.push_back(6);
deq.push_back(7);
deq.push_back(8);
printdeq(deq);
deq.push_front(3);
deq.push_front(2);
deq.push_front(1);
printdeq(deq);
deq.insert(deq.begin()+2,100);
printdeq(deq);
for(it = deq.begin(); it != deq.end(); it++)
{
if(*it == 100)
{
it = deq.erase(it);
it--;
}
}
//使用排序算法
sort(deq.begin(),deq.end(),less<int>());
printdeq(deq);
sort(deq.begin(),deq.end(),greater<int>());
printdeq(deq);
//使用查找算法
it = find(deq.begin(),deq.end(),5);
if(it != deq.end())
{
cout << "found 7" << endl;
}
else
{
cout << "not found 7" <<endl;
}
it = deq.begin();
while(it != deq.end())
{
if(*it == 6)
{
it = deq.erase(it);
}
else
{
it++;
}
}
printdeq(deq);
int nFront = deq.front();
cout << "nFront ="<<nFront << endl;
int nBack = deq.back();
cout << "nBack =" <<nBack <<endl;
printdeq(deq);
deq.clear();
if(deq.empty())
{
cout << "deq is empty" <<endl;
}
return 0;
}
list
1)底层数据结构:链表
2)支持的操作:
push_back() 在尾部插入
push_front() 在头部插入
insert() 任意位置插入
front() 获取头部元素
back() 获取尾部元素
pop_front() 删除头部元素
pop_back() 删除尾部元素
sort() 排序成员
erase() 在任意位置删除元素
注意事项: 执行erase后,erase内部会对迭代器进行自动加1
3)操作符:不支持[]操作符
4)内存是否连续: 不连续
#include "stdafx.h"
#include <list>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
using std::list;
using std::cout;
typedef list<char> charlist;
typedef list<char>::iterator charIter;
void printList(charlist List)
{
for(charIter It = List.begin(); It != List.end(); It++)
{
cout << *It << " ";
}
cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
charlist List;
charIter It;
for(char c = 'a';c <= 'z';c++)
{
List.push_back(c);
}
printList(List);
//删除操作
List.pop_back();
List.pop_front();
printList(List);
//插入操作
List.push_front('A');
List.push_back('Z');
printList(List);
//从小到大
List.sort(less<int>());
printList(List);
//从大到小
List.sort(greater<int>());
printList(List);
for(It = List.begin(); It != List.end(); It++)
{
if(*It == 'k')
{
It = List.erase(It);
It--;
}
}
It = find(List.begin(),List.end(),'k');
if(It != List.end())
{
cout << "found it" << endl;
}
else
{
cout << "not found it" << endl;
List.insert(It,'M');
}
printList(List);
char cTempFront = List.front();
char cTempBack = List.back();
cout << "cTempFront" << " " <<cTempFront<<"cTempBack" <<" "<< cTempBack << endl;
return 0;
}