STL(set)

简介: STL(set)

集合


#include<set>
using namespace std;


insert()


插入元素


#include<set>
#include<string>
using namespace std;
int main(){
  set<string> country;  //{}
  country.insert("China");//{"China"}
  return 0;
}


erase()


删除元素


#include<set>
#include<string>
using namespace std;
int main(){
  set<string> country;         //{}
  country.insert("China");     //{"China"}
  country.erase("China");      //{}
  return 0;
}


count()


判断元素是否存在


#include<set>
#include<string>
#include<stdio.h>
int main(){
  set<string> country;
  country.insert("China");
  if(country.count("China")){
    cout<< "China belong to country" << endl;
  }
  return 0;
}


遍历元素


//set<T>::iterator it
#include<set>
#include<string>
#include<iostream>
using namespace std;
int main(){
  set<string> country;
  country.insert("China");
  country.insert("America");
  country.insert("France");
  for(set<string>::iterator it=country.begin(); it !=country.end();it++){
    cout<<*it<<endl;
  }
  return 0;
}


注:C++中,set遍历是从小到大的。


clear()


清空——会清空set占用的内存


重载运算符


重载<号


#include<iostream>
#include<set>
using namespace std;
struct Point{
  int x,y;
  bool operator <(const Point &rhs)const{
    if(x==rhs.x){
      return y<rhs.y;
    }else{
      return x<rhs.x;
      }
  }
};
int main(){
  int n;
  set<Point> v;
  cin>>n;
  for(int i=0;i<n;i++){
    Point temp;
    cin >>temp.x >> temp.y;
    v.insert(temp);
  }
  for(set<Point>::iterator it=v.begin(); it!=v.end();it++){
    cout<<it->x<<" "it->y<<endl;
  }
}



相关文章
|
2月前
|
C++
stl中set、map的用法
stl中set、map的用法
|
2月前
|
编译器 容器
简易实现 STL--list
简易实现 STL--list
|
4月前
|
存储 自然语言处理 C++
C++ STL中 set和map介绍以及使用方法
C++ STL中 set和map介绍以及使用方法
61 1
|
10月前
|
自然语言处理 容器
C++STL——map与set介绍及使用
C++STL——map与set介绍及使用
C++STL——map与set的模拟实现(中)
C++STL——map与set的模拟实现(中)
C++STL——map与set的模拟实现(下)
C++STL——map与set的模拟实现(下)
|
10月前
|
C++
C++STL——map与set的模拟实现(上)
C++STL——map与set的模拟实现(上)
|
11月前
|
存储 数据处理 C++
STL之set,map
STL之set,map
|
11月前
|
存储 自然语言处理 C++
【STL】set、map的使用介绍
用来表示具有一一对应关系的一种数据结构,该结构中只包含两个成员变量key和value,key代表关键字,value表示关键字对应的值。比如:现在要建立一个英译汉的词典,那该词典中必然有英文单词和与其对应的中文含义,而且,英文单词与中文含义是一一对应的关系,
|
存储 C++ 索引
【C++进阶】八、STL---unordered_set & unordered_set的介绍及使用
目录 一、unordered系列关联式容器 二、unordered_set的介绍及使用 2.1 介绍 2.2 使用 三、unordered_map的介绍及使用 3.1 介绍 3.2 使用
148 0