C++ 집합 insert()

Jmnote (토론 | 기여)님의 2023년 9월 10일 (일) 12:14 판 (→‎개요)

1 개요

C++집합 insert()
#include <iostream>
#include <set>
using namespace std;

int main() {
    set<int> myset;
    for (int i=1; i<=5; i++) {
        myset.insert(i*10);
    }
    for(auto& el: myset) cout << el << ' '; // 10 20 30 40 50
}
#include <iostream>
#include <set>
using namespace std;
int main() {
    set<string> fruits = {"apple", "banana", "cherry"};
    pair<set<string>::iterator,bool> p;
    p = fruits.insert("lemon");
    cout << p.second << endl; // 1 (new element)
    p = fruits.insert("apple");
    cout << p.second << endl; // 0 (not new element)
}

2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}