Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน set::find() ใน C++ STL

ฟังก์ชัน set::find() ใน C++ STL คืนค่า Iterator ชี้ไปยังสมาชิกที่ค้นหาใน Set Container Iterator จะชี้ตำแหน่งหลังสมาชิกสุดท้าย หากไม่พบสมาชิก

อัลกอริทึม

Begin
    Define function printS() to print set elements
    Initialize empty set s, insert elements
    Call printS()
    Call set find() to find element in s
    If found print "element is in set" else print "element not in set"
End.

โค้ดตัวอย่าง

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> s; set<int>::iterator it;
    s.insert(7); s.insert(6); s.insert(1); s.insert(4);
    s.insert(2); s.insert(9); s.insert(10);
    auto pos = s.find(6);
    cout << "The set elements after 6 are: ";
    for (it = pos; it != s.end(); it++) cout << *it << " ";
    return 0;
}

ผลลัพธ์

The set elements after 6 are: 6 7 9 10