stl find_end_C ++ STL中的多图find(),lower_bound(),upper_bound()

stl find_end

C ++ multimap :: find(),multimap :: lower_bound()和multimap :: upper_bound()函数 (C++ multimap::find(), multimap::lower_bound(), and multimap::upper_bound() functions)

Multimap is used when you need to store the same keys with distinct values where the map fails to do the same.

当需要将相同的键与不同的值存储在一起而映射无法执行相同操作时,可以使用Multimap。

In this article, we are going to see some useful functions in multimap.

在本文中,我们将看到多图中的一些有用功能。

C ++ STL中的multimap :: find() (multimap::find() in C++ STL)

Similarly, as in the map, multimap provides us a way to search a key.

同样,就像在地图中一样,多地图为我们提供了一种搜索键的方法。

The syntax of the find function is like below,

find函数的语法如下所示,

iterator find(key);

Find simply returns the iterator to the first occurrence of the key if the key occurs. If the key doesn't occur at all then it returns iterator multimap::end().

如果键出现,Find仅将迭代器返回到键的第一次出现。 如果该键根本没有出现,则返回迭代器multimap :: end()

In case of multiple occurrences of the same key, it would return an iterator to the first occurrence only.

如果多次出现相同的键,它将仅将迭代器返回到第一次出现。

An example is the following.

下面是一个示例。

Say the multimap mymap already constructed is,

假设已经构造了多图mymap

Key	Value
2	8
2	14
3	12
5	10
5	6

Now mymap.find(2) will return iterator mymap.begin() (as to the first element). Where, mymap.find(7) will return mymap.end() as key is not found().

现在, mymap.find(2)将返回迭代器mymap.begin() (对于第一个元素)。 其中, 由于找到键, mymap.find(7)将返回mymap.end ()

C ++ STL中的multimap :: lower_bound() (multimap::lower_bound() in C++ STL)

Similarly, as in the map, multimap provides us a way to search a key.

同样,就像在地图中一样,多地图为我们提供了一种搜索键的方法。

The syntax of the find function is like below,

find函数的语法如下所示,

iterator lower_bound(key);

Find simply returns the iterator to the first occurrence of the key if the key occurs. If the key doesn’t occur at all then it returns an iterator to the next greater element. If the key is greater than the maximum key in the multimap it will return an iterator to pair <search_key,0>.

如果键出现,Find仅将迭代器返回到键的第一次出现。 如果该键根本没有出现,则它将迭代器返回到下一个更大的元素。 如果键大于多图中的最大键,它将返回一个迭代器,以配对<search_key,0>

In case of multiple occurrences of the same key, it would return an iterator to the first occurrence only .

如果多次出现相同的键,它将仅将迭代器返回到第一次出现。

An example is the following.

下面是一个示例。

Say the multimap mymap already constructed is,

假设已经构造了多图mymap

Key	Value
2	8
2	14
3	12
5	10
5	6

Now mymap.lower_bound(2) will return iterator mymap.begin() (as to the first element). Where mymap.lower_bound(4) will return an iterator to the entry <5,10> as the key is not found and 5 is the next greater key.

现在, mymap.lower_bound(2)将返回迭代器mymap.begin() (对于第一个元素)。 其中mymap.lower_bound(4)将返回迭代器到条目<5,10>,因为未找到密钥,而5是下一个更大的密钥。

C ++ STL中的multimap :: upper_bound() (multimap::upper_bound() in C++ STL)

Similarly, as in the map, multimap provides us a way to search a key.

同样,就像在地图中一样,多地图为我们提供了一种搜索键的方法。

The syntax of the find function is like below:

find函数的语法如下:

iterator upper_bound(key);

Find simply returns the iterator to the next greater key of the searched key. If the searched key is greater than or equals to max key present in the multimap then it returns an iterator to pair <search_key,0>.

Find只是将迭代器返回到搜索关键字的下一个更大关键字。 如果搜索的键大于或等于多重映射中存在的最大键,则它将返回一个迭代器,以配对<search_key,0>

An example is the following.

下面是一个示例。

Say the multimap mymap already constructed is

说已经构造的多图mymap

Key	Value
2	8
2	14
3	12
5	10
5	6

Now mymap.upper_bound(2) will return iterator to pair <3,12> (3 is next greater key of 2). Where, mymap.lower_bound(5) will return iterator to the entry <5,0> as next greater key doesn’t exist for 5.  

现在, mymap.upper_bound(2)将返回迭代器以配对<3,12> (3是2的下一个更大的键)。 此处, mymap.lower_bound(5)将迭代器返回到条目<5,0>,因为5中不存在下一个更大的键。

Also some other trivial functions are: size() and count() whose uses are exactly same as map.

其他一些琐碎的函数还有: size()count()的用法与map完全相同。

多地图find(),lower_bound(),upper_bound()的C ++实现 (C++ implementation of multimap find(), lower_bound(), upper_bound())

#include <bits/stdc++.h>
using namespace std;

int main()
{
    multimap<int, int> mymultimap;
    
    // insertion in multimap
    cout << "Inserting like above example\n";
    mymultimap.insert(make_pair(5, 10));
    mymultimap.insert(make_pair(2, 8));
    mymultimap.insert(make_pair(3, 12));
    mymultimap.insert(make_pair(2, 14));
    mymultimap.insert(make_pair(5, 6));

    cout << "Printing the multimap\n";
    multimap<int, int>::iterator ij;
    for (ij = mymultimap.begin(); ij != mymultimap.end(); ij++) {
        cout << "key: " << ij->first << " ,value: " << ij->second << endl;
    }

    cout << "finding key 3\n";
    ij = mymultimap.find(3);
    cout << "key: " << ij->first << " ,value: " << ij->second << endl;

    cout << "finding lower bound for 1\n";
    ij = mymultimap.lower_bound(1);
    cout << "key: " << ij->first << " ,value: " << ij->second << endl;

    cout << "finding lower bound for 2\n";
    ij = mymultimap.lower_bound(2);
    cout << "key: " << ij->first << " ,value: " << ij->second << endl;

    cout << "finding lower bound for 5\n";
    ij = mymultimap.lower_bound(5);
    cout << "key: " << ij->first << " ,value: " << ij->second << endl;
    cout << "finding lower bound for 6\n";
    ij = mymultimap.lower_bound(6);
    cout << "key: " << ij->first << " ,value: " << ij->second << endl;

    cout << "finding upper bound for 1\n";
    ij = mymultimap.upper_bound(1);
    cout << "key: " << ij->first << " ,value: " << ij->second << endl;

    cout << "finding upper bound for 2\n";
    ij = mymultimap.upper_bound(2);
    cout << "key: " << ij->first << " ,value: " << ij->second << endl;

    cout << "finding upper bound for 5\n";
    ij = mymultimap.upper_bound(5);
    cout << "key: " << ij->first << " ,value: " << ij->second << endl;
    
    cout << "finding upper bound for 6\n";
    ij = mymultimap.upper_bound(6);
    cout << "key: " << ij->first << " ,value: " << ij->second << endl;

    return 0;
}

Output:

输出:

Inserting like above example
Printing the multimap
key: 2 ,value: 8
key: 2 ,value: 14
key: 3 ,value: 12
key: 5 ,value: 10
key: 5 ,value: 6
finding key 3
key: 3 ,value: 12
finding lower bound for 1
key: 2 ,value: 8
finding lower bound for 2
key: 2 ,value: 8
finding lower bound for 5
key: 5 ,value: 10
finding lower bound for 6
key: 5 ,value: 0
finding upper bound for 1
key: 2 ,value: 8
finding upper bound for 2
key: 3 ,value: 12
finding upper bound for 5
key: 5 ,value: 0
finding upper bound for 6
key: 5 ,value: 0


翻译自: https://www.includehelp.com/stl/multimap-find-lower_bound-upper_bound-in-cpp-stl.aspx

stl find_end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值