STL
1:STL容器就为我们提供了这样的方便,它允许我们重复利用已有的实现构造自己的特定类型下的数据结构
2:迭代器
vecrot<int>::iterator it;
iterator
3:算法部分主要由头文件<algorithm>,<numeric>和<functional>组成。
1:STL容器就为我们提供了这样的方便,它允许我们重复利用已有的实现构造自己的特定类型下的数据结构
2:迭代器
vecrot<int>::iterator it;
iterator
3:算法部分主要由头文件<algorithm>,<numeric>和<functional>组成。
String 容器
1: string是一个类, char*是一个指向字符的指针。
2: string不用考虑内存释放和越界。
3: string提供了一系列的字符串操作函数
查找find(),拷贝copy(),删除erase(),替换replace(),插入insert().
vector 容器 向量 单向
push_back()
pop()
front()
back()
erase(pos); 删除pos位置上的值
其迭代器是一个左闭合区间
vector<int>::iterator 这是一个正向的 begin end
vector<int>::reverse_iterator 这是一个反向的 rbegin rend
1: string是一个类, char*是一个指向字符的指针。
2: string不用考虑内存释放和越界。
3: string提供了一系列的字符串操作函数
查找find(),拷贝copy(),删除erase(),替换replace(),插入insert().
vector 容器 向量 单向
push_back()
pop()
front()
back()
erase(pos); 删除pos位置上的值
其迭代器是一个左闭合区间
vector<int>::iterator 这是一个正向的 begin end
vector<int>::reverse_iterator 这是一个反向的 rbegin rend
deque 容器 双向
push_back push_front
pop_back pop_front
push_back push_front
pop_back pop_front
stack 容器 先进后出
push() 压栈\
pop() 从顶部抛出
empty() 是否为空 size()判断大小
queue 队列先进先出
push()往队尾添加元素
pop() 从队头移除第?一个元素
front()
back()
empty()
size()
push()往队尾添加元素
pop() 从队头移除第?一个元素
front()
back()
empty()
size()
list 双向列表 高效的出入元素 不支持[] at() 操作符
具有迭代器 由于是双向链表
push_back()
push_fron()
pop_back()
remove(val) 删除值是val的元素
erase() 删除 以迭代器作为参数
具有迭代器 由于是双向链表
push_back()
push_fron()
pop_back()
remove(val) 删除值是val的元素
erase() 删除 以迭代器作为参数
set 容器 集合中所包含的元素是唯一的 元素插入过程是按排序规则插入,所以不能指定插入位置 set不可以直接存取元素
如果希望修改一个元素值,必须先删除原有的元素,再插入新的元素
insert(val) 插入值
具有迭代器
begin()
end()
rbegin()
rend()
clear() 清楚所有元素
erase(pos/val) 删除pos迭代器所指的元素,返回下一个元素的迭代器 或者删除值
class Man
{
public:
Man(int age, string name)
{
_age = age;
_name = name;
}
int _age;
string _name;
};
string _name;
};
防函数 set可以自定义排序规则
struct ManFunctor
{
bool operator() (const Man & man1, const Man &man2)
{
return (man1._age > man2._age);
}
};
struct ManFunctor
{
bool operator() (const Man & man1, const Man &man2)
{
return (man1._age > man2._age);
}
};
int main(void)
{
//set<int> si;
//for (int i = 0; i < 10; i++)
//{
// si.insert(i);
//}
{
//set<int> si;
//for (int i = 0; i < 10; i++)
//{
// si.insert(i);
//}
set<Man, ManFunctor> sman;
sman.insert(Man(1, "a1"));
sman.insert(Man(3, "a2"));
sman.insert(Man(5, "a3"));
sman.insert(Man(6, "a4"));
sman.insert(Man(1, "a1"));
sman.insert(Man(3, "a2"));
sman.insert(Man(5, "a3"));
sman.insert(Man(6, "a4"));
set<Man>::iterator it;
for (it = sman.begin(); it != sman.end(); it++)
cout << "it->name" << it->_name << " " << "it->age" << it->_age << endl;
cout << "it->name" << it->_name << " " << "it->age" << it->_age << endl;
system("pause");
return 0;
}
map 键值对的方式存储
map中key值是唯一的
map<int,string> ma;
插入:
ma.insert(pair<int,string>(1,"a1"));
包含迭代器 可以通过迭代器进行遍历
for (map<int, string>::iterator it = ma.begin(); it != ma.end(); it++)
cout << it->first << " " << it->second << endl;
begin()
end()
rbegin()
rend()
clear()
erase()
find(key) 返回的是迭代器
插入:
ma.insert(pair<int,string>(1,"a1"));
包含迭代器 可以通过迭代器进行遍历
for (map<int, string>::iterator it = ma.begin(); it != ma.end(); it++)
cout << it->first << " " << it->second << endl;
begin()
end()
rbegin()
rend()
clear()
erase()
find(key) 返回的是迭代器