C ++ STL中的set :: upper_bound()函数

C ++ STL set :: upper_bound()函数 (C++ STL set::upper_bound() function)

set::upper_bound() function is a predefined function, it is used to get the upper bound of any element in a set.

set :: upper_bound()函数是预定义的函数,用于获取集合中任何元素的上限。

it finds upper bound of any desired element from the set. Upper bound of any_element means the first number in the set that's immediate next to any_element.

它从集合中找到任何所需元素的上限。 上界any_element手段的第一个号码的组的直接旁边any_element。

Prototype:

原型:

    set<T> st; //declaration
    set<T> st::iterator it; //iterator declaration
    it=st.upper_bound(T key);

Parameter: T key; //T is the data type

参数: T键; // T是数据类型

Return type: If upper_bound of the key exists in the set iterator pointer to the upper bound, Else, st.end()

返回类型:如果键的upper_bound存在于指向顶部上限的设置迭代器指针中,否则为st.end()

Usage:

用法:

The function finds upper bound of any desired element from the set. Upper bound of x is immediate next of x.

该函数从集合中找到任何所需元素的上限。 x的上限紧邻x。

Example:

例:

    For a set of integer,
    set<int> st;
    st.insert(6);
    st.insert(4);
    st.insert(10);
    set content: //sorted always(ordered)
        4
        6
        10

    it=st.upper_bound(4)
    Print *it; //6

Header file to be included:

包含的头文件:

    #include <iostream>
    #include <set>
    OR
    #include <bits/stdc++.h>

C++ implementation:

C ++实现:

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

void printSet(set<int> st){
	set<int>:: iterator it;
	cout<<"Set contents are:\n";
	if(st.empty()){
		cout<<"empty set\n";
		return;
	}
	for(it=st.begin();it!=st.end();it++)
		cout<<*it<<" ";
	cout<<endl;
}

int main(){
	cout<<"Example of upper_bound function\n";
	set<int> st;
	set<int>:: iterator it;
	cout<<"inserting 4\n";
	st.emplace(4);
	cout<<"inserting 6\n";
	st.emplace(6);
	cout<<"inserting 10\n";
	st.emplace(10);

	printSet(st); //printing current set

	cout<<"upper bound of 6 is "<<*(st.upper_bound(6));
	
	return 0;
}

Output

输出量

Example of upper_bound function
inserting 4
inserting 6
inserting 10
Set contents are:
4 6 10
upper bound of 6 is 10 


翻译自: https://www.includehelp.com/stl/set-upper_bound-function-in-cpp-stl.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值