0
点赞
收藏
分享

微信扫一扫

c++ set(集合容器)

夏侯居坤叶叔尘 2022-01-08 阅读 39

头文件

#include<set>

声明

set<任何类型>集合名         //元素可重复
mutiset<任何类型>集合名     //元素不可重复

集合内部根据红黑树自动排序

相关函数


insert()  //向set中插入元素
size()    //返回set中元素的个数
begin()   //返回set容器的第一个元素的地址
end()     //返回set容器的最后一个元素的地址
clear()   //清空set
empty()   //判断set容器是否为空
max_size() //返回set容器容量

示例

/*
描述
给定 n 个字符串,请对 n 个字符串按照字典序排列。

数据范围:  ,字符串长度满足
输入描述:
输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。
输出描述:
数据输出n行,输出结果为按照字典序排列的字符串。
示例1
输入:
9
cap
to
cat
card
two
too
up
boat
boot
输出:
boat
boot
cap
card
cat
to
too
two
up

*/

#include<iostream>
#include<set>
#include<string>
 
using namespace std;
 
int main() {
    int n;
    while (cin >> n) {
        multiset<string> strset;  //元素可重复集合
        string tmp;
        for (int i = 0; i < n; ++i) {
            cin>>tmp;
            strset.insert(tmp);
        }
        for (auto it = strset.begin(); it != strset.end(); ++it)
            cout << *it << endl; //取出it所指元素
    }
    return 0;
}
举报

相关推荐

C++容器 - set & map

C++容器-list、set、map

0 条评论