"C++ 집합 insert()"의 두 판 사이의 차이

 
23번째 줄: 23번째 줄:
     pair<set<string>::iterator,bool> p;
     pair<set<string>::iterator,bool> p;
     p = fruits.insert("lemon");
     p = fruits.insert("lemon");
     cout << p.second << endl; // 1 (new element)
     cout << p.second << endl; // 1 (new element inserted)
     p = fruits.insert("apple");
     p = fruits.insert("apple");
     cout << p.second << endl; // 0 (not new element)
     cout << p.second << endl; // 0 (no new element inserted)
}
}



2023년 9월 10일 (일) 12:19 기준 최신판

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 inserted)
    p = fruits.insert("apple");
    cout << p.second << endl; // 0 (no new element inserted)
}

2 같이 보기[ | ]

3 참고[ | ]

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