c++ stl erase_C ++ STL中的set :: erase()函数

news/2024/7/19 12:00:44 标签: python, matlab, js, 列表, stl

c++ stl erase

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

set::erase() function is a predefined function, it is used to erase an element from a set.

set :: erase()函数是预定义的函数,用于删除集合中的元素。

Prototype:

原型:

    set<T> st; //declaration
    set<T>::iterator it; //iterator declaration
    st.erase( const T item); //prototype 1

    or

    st.erase(iterator position) //prototype 2

Parameter:

参数:

    const T item;  //prototype 1
    Or
    Iterator position //prototype 2

Return type:

返回类型:

    size_type //prototype 1
    Or
    void //prototype 2

Usage: The function is used to erase an element based on its value or iterator position from the set

用法:该函数用于根据元素的值或元素在集合中的迭代器位置来擦除它

Example:

例:

    For a set of integer,
    set<int> st;
    set<int>::iterator it;
    st.insert(4);
    st.insert(5);
    set content:
        4
        5

    st.erase(4);  
    set content:
        5

    st.erase(st.begin()); //erases 5
    set content:
    empty set

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";
	for(it=st.begin();it!=st.end();it++)
		cout<<*it<<" ";
	cout<<endl;
}

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

	printSet(st); //printing current set

	cout<<"erasing 6..\n";
	st.erase(6); //prototype 1
	cout<<"After erasing 6...\n";
	printSet(st);
	cout<<"erasing first element of the set now\n";
	st.erase(st.begin());//prototype 2
	cout<<"after erasing first element of set now\n";

	printSet(st);
	
	return 0;
}

Output

输出量

Example of erase function
inserting 4
inserting 6
inserting 10
Set contents are:
4 6 10     
erasing 6..
After erasing 6...        
Set contents are:
4 10       
erasing first element of the set now    
after erasing first element of set now  
Set contents are:
10


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

c++ stl erase


http://www.niftyadmin.cn/n/1254087.html

相关文章

stl中empty_C ++ STL中的set :: empty()函数

stl中emptyC STL set :: empty()函数 (C STL set::empty() function) set::empty() function is a predefined function, it is used to check whether a set is empty or not. If set is empty it returns true (1), if set is not empty it returns false (0). set :: empty…

20181127-2 每周例行报告

此作业要求参见&#xff1a;[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2447] 一、本周PSP表格 总计&#xff1a;238min 二、本周进度表 三、代码累积折线图 四、博客字数累积图 五、本周PSP饼状图 六、PSP时间累积图 转载于:https://www.cnblogs.com/handsome-b…

ruby hash方法_Ruby中带有示例的Hash.eql?()方法

ruby hash方法Hash.eql&#xff1f;()方法 (Hash.eql?() Method) In this article, we will study about Hash.eql?() Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this…

Java Socket通信以及可能出现的问题解决

https://www.cnblogs.com/shakinghead/p/7647761.html 待总结转载于:https://www.cnblogs.com/genggeng/p/10066225.html

codd's是什么_EF-Codd的12条黄金法则| 数据库管理系统

codds是什么In 1970’s DR. EF-Codd published a paper, titled a Relational Model of Data for large shared databases. This paper becomes the all development RDBMS. In support of their relational model, Dr. EF-Codd also proposed 12 rules which are known as 12 …

线性代数中两个向量相乘_加两个向量| Python的线性代数

线性代数中两个向量相乘Prerequisite: Linear Algebra | Defining a Vector 先决条件&#xff1a; 线性代数| 定义向量 In the python code, we will add two vectors. We can add two vectors only and only if the both the vectors are in the same dimensional space. For…

【Cocos Creator实战教程(10)】——UI组件(4)Slider 组件

1. 相关知识点 Slider是一个滑动器组件 1.1 Slider组件 点击 属性检查器下面的添加组件按钮&#xff0c;然后从添加UI组件中选择Slider&#xff0c;即可添加Slider组件到节点上。 1.2 Slider属性 属性功能说明Handle滑动按钮部件&#xff0c;可以通过该按钮进行滑动调节Slider数…

linux tcp服务器软件,linux下tcp服务器源码示例

#include #include #include #include #include #include #include #include #include #include #include struct _NSS_HEADER{unsigned short ProtocolVersion; /* 协议版本信息 */unsigned short MsgType; /* 消息类型 */unsigned short TransactionNo; /* 传输编号 */unsign…