std::pair, std::copy, std::lower_bound, std::back_insert使用

std::pair

首先给出std::pair的class定义:

template  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 
    pair (const pair &p) : first(p.first), second(p.second) { }
}

可见std::pair的两个成员first和second都是公有的,从而可以直接对其进行访问.
这里主要说明std::pairs是可以直接做比较的,现引用官方的一句话:
The header 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 
  ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
                                const T& value );

template 
  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
  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                                                                                                                                       
using namespace std;

#include 
#include 
#include 

struct compare_t
{
    bool operator() (const std::pair& pair1_, const std::pair& pair2_)
    {
        return pair1_.first < pair2_.first;
    }
};

int main()
{
    //! first member compare first, and then second
    std::pair test1(1, 2.0);
    std::pair test2(2, 2.2);
    std::pair test3(1, 2.3);
    std::pair 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, compare_t> elements;
    elements.insert(test1);
    elements.insert(test2);
    elements.insert(test3);
    elements.insert(test4);
    for (std::set, compare_t>::iterator it = elements.begin(); it != elements.end(); ++it)
    {
        cout << it->first << ":" << it->second << endl;
    }

    std::pair test5(3, 2.4);
    std::set, compare_t>::iterator it = std::lower_bound(elements.begin(), elements.end(), test5, compare_t());
    //std::set, 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 > elements_test;
    std::copy(elements.begin(), it, std::back_inserter(elements_test));
    cout << endl << endl;
    for (std::vector >::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 >::iterator it = elements_test.begin(); it != elements_test.end(); ++it)
    {
        cout << it->first << ":" << it->second << endl;
    }
    return 0;
}  



你可能感兴趣的:(c++)