how to do sets in cpp
#include <set>
int main() {
set<int> my_set = {1, 2, 3, 4};
for (int i = 0; i < 4; i++) {
cout << my_set[i] << endl;
}
}
how to do sets in cpp
#include <set>
int main() {
set<int> my_set = {1, 2, 3, 4};
for (int i = 0; i < 4; i++) {
cout << my_set[i] << endl;
}
}
set in c++
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
//set mentains internally the ascending order of these numbers
void setDemo()
{
set<int> S;
S.insert(1);
S.insert(2);
S.insert(-1);
S.insert(-10);
S.erase(1);//to remove an element
//Print all the values of the set in ascending order
for(int x:S){
cout<<x<<" ";
}
//check whether an element is present in a set or not
auto it = S.find(-1);//this will return an iterator to -1
//if not present it will return an iterator to S.end()
if (it == S.end()){
cout<<"not Presentn";
}else{
cout <<" presentn";
cout << *it <<endl;
}
//iterator to the first element in the set which is
//greater than or equal to -1
auto it2 = S.lower_bound(-1);
//for strictly greater than -1
auto it3 = S.upper_bound(-1);
//print the contents of both the iterators
cout<<*it2<<" "<<*it3<<endl;
}
int main() {
setDemo();
return 0;
}
set c++
// constructing sets
#include <iostream>
#include <set>
bool fncomp (int lhs, int rhs) {return lhs<rhs;}
struct classcomp {
bool operator() (const int& lhs, const int& rhs) const
{return lhs<rhs;}
};
int main ()
{
std::set<int> first; // empty set of ints
int myints[]= {10,20,30,40,50};
std::set<int> second (myints,myints+5); // range
std::set<int> third (second); // a copy of second
std::set<int> fourth (second.begin(), second.end()); // iterator ctor.
std::set<int,classcomp> fifth; // class as Compare
bool(*fn_pt)(int,int) = fncomp;
std::set<int,bool(*)(int,int)> sixth (fn_pt); // function pointer as Compare
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us