vector的有序化操作
来源:互联网 发布:什么叫网络平台 编辑:程序博客网 时间:2024/06/10 22:54
vector的有序化操作
在有些情况下,需要用到一个有序的vector。它的有序操作有三种:查找,插入,删除。
插入实现:
删除实现:
查找可通过binary_search, lower_bound, upper_bound, 或者equal_range实现。如果要实现类似map的关键字搜索,有一个技巧,就是用比较函数进行重载,比如学生要按学号查找,则用以下定义:
查找学号为5的学生:
删除学号为5的学生:
插入实现:
template <typename Container>
inline void ordered_insert(Container& c, typename Container::value_type const& t)
{
c.insert(std::upper_bound(c.begin(), c.end(), t), t);
}
template <typename Container, typename Cmp>
inline void ordered_insert(Container& c, typename Container::value_type const& t, Cmp cmp)
{
c.insert(std::upper_bound(c.begin(), c.end(), t, cmp), t);
}
inline void ordered_insert(Container& c, typename Container::value_type const& t)
{
c.insert(std::upper_bound(c.begin(), c.end(), t), t);
}
template <typename Container, typename Cmp>
inline void ordered_insert(Container& c, typename Container::value_type const& t, Cmp cmp)
{
c.insert(std::upper_bound(c.begin(), c.end(), t, cmp), t);
}
删除实现:
template <typename Container, typename It>
inline void erase_range(Container& c, std::pair<It, It> const& r)
{
c.erase(r.first, r.second);
}
template <typename Container>
inline void ordered_erase(Container& c, typename Container::value_type const& t)
{
erase_range(c, std::equal_range(c.begin(), c.end(), t));
}
template <typename Container, typename T, typename Cmp>
inline void ordered_erase(Container& c, T const& t, Cmp cmp)
{
erase_range(c, std::equal_range(c.begin(), c.end(), t, cmp));
}
inline void erase_range(Container& c, std::pair<It, It> const& r)
{
c.erase(r.first, r.second);
}
template <typename Container>
inline void ordered_erase(Container& c, typename Container::value_type const& t)
{
erase_range(c, std::equal_range(c.begin(), c.end(), t));
}
template <typename Container, typename T, typename Cmp>
inline void ordered_erase(Container& c, T const& t, Cmp cmp)
{
erase_range(c, std::equal_range(c.begin(), c.end(), t, cmp));
}
查找可通过binary_search, lower_bound, upper_bound, 或者equal_range实现。如果要实现类似map的关键字搜索,有一个技巧,就是用比较函数进行重载,比如学生要按学号查找,则用以下定义:
struct Student
{
int id;
std::string name;
struct LessThan
{
bool operator() (Student const& x, Student const& y)
{
return x.id < y.id;
}
bool operator() (Student const& x, int id)
{
return x.id < id;
}
bool operator() (int id, Student const& y)
{
return id < y.id;
}
};
};
{
int id;
std::string name;
struct LessThan
{
bool operator() (Student const& x, Student const& y)
{
return x.id < y.id;
}
bool operator() (Student const& x, int id)
{
return x.id < id;
}
bool operator() (int id, Student const& y)
{
return id < y.id;
}
};
};
查找学号为5的学生:
std::vector<Student> students;
bool exist = std::binary_search(students.begin(), students.end(), 5, Student::LessThan());
bool exist = std::binary_search(students.begin(), students.end(), 5, Student::LessThan());
删除学号为5的学生:
ordered_erase(students, 5, Student::LessThan());
- vector的有序化操作
- 有序二叉树的操作
- [Redis] 有序集合的操作
- vector对象的操作
- vector操作的习题
- vector的下标操作
- vector的一些操作
- vector的插入操作
- vector的基本操作
- vector的sort操作
- vector的常用操作
- 有序向量Vector
- 判断一个vector是否是另外一个vector的有序子集
- vector的基本操作实现
- 对vector的各种操作
- 对vector容器的操作
- Vector的用法和操作
- vector一些操作的练习
- 最近的二三事
- 微软《SOA in the Real World》笔记03——第一章
- 比尔盖茨:十条“金口玉言”
- VS2005编译问题LINK : E:/aaa/Debug/aaa.exe not found or not built by the last incremental link; performing full link
- [delphi]动态创建MEMO历遍目录TXT文件到listbox中,並將內容加載到memo中.
- vector的有序化操作
- 计划 2008 3 21 随时回来检讨一下, 看看自己完成了多少
- .net继承浅析
- 如果soff侵犯著作权,那么ms或将获得巨额赔偿?
- Windows 的动态链接库(DLL)中获得dll自身的名称
- Hibernate之父
- java字符串之StringBuffer的应用
- SQL常用字符串函数
- .NET新人,立志做一名优秀的.NET开发者