std::pair, std::copy, std::lower_bound, std::back_insert使用
2013-01-23 11:24
585 查看
std::pair
首先给出std::pair的class定义:
可见std::pair的两个成员first和second都是公有的,从而可以直接对其进行访问.
这里主要说明std::pairs是可以直接做比较的,现引用官方的一句话:
The header <utility> also overloads the relational operators ==, <, !=, >, >= and <= ,
so as to be able to compare pair objects of the same type directly:
Two pair objects are compared equal if the first elements in both objects compare equal to each other
and both second elements also compare equal to each other - they all have to match.
In inequality comparisons (<, >), the first elements are compared first, and only if the inequality comparison
is not true for them, the second elements are compared.
简单解释一下:对std::pair的比较做了一个重载,首先比较第一个成员函数,如果相等,在比较第二个成员,只有当两个成员
都相等的情况下,std::pair才算相等!这里可以简单实现伪代码如下:
std::lower_bound
反回有序序列中第一个满足条件的元素迭代器,给出它的两种声明,一种是默认以operator<,进行比较,
另一种以传放的仿函数做比较!
同样引用一句话:
Returns an iterator pointing to the first element in the sorted range [first,last) which does not compare less than value.
The comparison is done using either operator< for the first version, or comp for the second.
std::copy
Copies the elements in the range [first,last) into the range beginning at result.
The function returns an iterator to the end of the destination range (which points to the element following the copy of last).
定义如下:
当然还有一点,尽量不要自己复制自己!
那么下面给一个完整的例子!
首先给出std::pair的class定义:
template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair() : first(T1()), second(T2()) {} pair(const T1& x, const T2& y) : first(x), second(y) {} template <class U, class V> pair (const pair<U,V> &p) : first(p.first), second(p.second) { } }
可见std::pair的两个成员first和second都是公有的,从而可以直接对其进行访问.
这里主要说明std::pairs是可以直接做比较的,现引用官方的一句话:
The header <utility> also overloads the relational operators ==, <, !=, >, >= and <= ,
so as to be able to compare pair objects of the same type directly:
Two pair objects are compared equal if the first elements in both objects compare equal to each other
and both second elements also compare equal to each other - they all have to match.
In inequality comparisons (<, >), the first elements are compared first, and only if the inequality comparison
is not true for them, the second elements are compared.
简单解释一下:对std::pair的比较做了一个重载,首先比较第一个成员函数,如果相等,在比较第二个成员,只有当两个成员
都相等的情况下,std::pair才算相等!这里可以简单实现伪代码如下:
//! 1 >; 2 < ; 3= int operators == (std::pair& pair1_, std::pair& pari2_) { if (pair1_.first > pari2_.first) { return 1; } else if (pair1_.first < pari2_.first) { return 2; } else { if (pair1_.second > pari2_.second) { return 1; } else if (pair1_.second < pari2_.second) { return 2; } } return 0; }
std::lower_bound
反回有序序列中第一个满足条件的元素迭代器,给出它的两种声明,一种是默认以operator<,进行比较,
另一种以传放的仿函数做比较!
template <class ForwardIterator, class T> ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value ); template <class ForwardIterator, class T, class Compare> ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value, Compare comp );
同样引用一句话:
Returns an iterator pointing to the first element in the sorted range [first,last) which does not compare less than value.
The comparison is done using either operator< for the first version, or comp for the second.
std::copy
Copies the elements in the range [first,last) into the range beginning at result.
The function returns an iterator to the end of the destination range (which points to the element following the copy of last).
定义如下:
template<class InputIterator, class OutputIterator> OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result) { while (first!=last) { *result = *first; ++result; ++first; } return result; }它通常配合std::back_insert 使用,以push_back的形式复制元素到新的容器中,也就是说,目标容器必须要有push_back成员函数。
当然还有一点,尽量不要自己复制自己!
那么下面给一个完整的例子!
#include <iostream> using namespace std; #include <set> #include <vector> #include <algorithm> struct compare_t { bool operator() (const std::pair<int, double>& pair1_, const std::pair<int, double>& pair2_) { return pair1_.first < pair2_.first; } }; int main() { //! first member compare first, and then second std::pair<int, double> test1(1, 2.0); std::pair<int, double> test2(2, 2.2); std::pair<int, double> test3(1, 2.3); std::pair<int, double> test4(1, 2.4); cout << (test1 < test2) << endl; // 1 cout << (test2 < test3) << endl; // 0 cout << (test3 < test4) << endl; // 1 cout << (test3 < test1) << endl; // 0 cout << endl; std::set<std::pair<int, double>, compare_t> elements; elements.insert(test1); elements.insert(test2); elements.insert(test3); elements.insert(test4); for (std::set<std::pair<int, double>, compare_t>::iterator it = elements.begin(); it != elements.end(); ++it) { cout << it->first << ":" << it->second << endl; } std::pair<int, double> test5(3, 2.4); std::set<std::pair<int, double>, compare_t>::iterator it = std::lower_bound(elements.begin(), elements.end(), test5, compare_t()); //std::set<std::pair<int, double>, compare_t>::iterator it = elements.lower_bound(test5); if (it == elements.end()) { cout << "not foud" << endl; } else { cout << it->first << ":" << it->second << endl; } std::vector<std::pair<int, double> > elements_test; std::copy(elements.begin(), it, std::back_inserter(elements_test)); cout << endl << endl; for (std::vector<std::pair<int, double> >::iterator it = elements_test.begin(); it != elements_test.end(); ++it) { cout << it->first << ":" << it->second << endl; } //!拷贝到自己: std::copy(elements_test.begin(), elements_test.end(), std::back_inserter(elements_test)); cout << endl << endl; for (std::vector<std::pair<int, double> >::iterator it = elements_test.begin(); it != elements_test.end(); ++it) { cout << it->first << ":" << it->second << endl; } return 0; }
相关文章推荐
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
- 使用std::lower_bound和std::upper_bound解决常见的二分查找问题
- C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
- STL插入排序std::upper_bound和std::lower_bound的使用,以及通用排序类
- C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
- lower_bound/upper_bound使用方法
- std::copy() & std::back_inserter()
- std::copy()和std::back_inserter()用法
- std::set::lower_bound与std::lower_bound的效率问题
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
- C++下std::pair的使用?
- lower_bound的使用
- upper_bound()和 lower_bound()使用方法
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
- lower_bound() 的使用
- Lower_bound和upper_bound大致使用事项
- upper_bound()与lower_bound()使用方法
- std::copy使用方法