您的位置:首页 > 大数据 > 人工智能

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

2013-01-23 11:24 585 查看
std::pair

首先给出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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: