iterating a set c++
//Given set s
for(auto it: s){
cout << it << endl;
}
iterating a set c++
//Given set s
for(auto it: s){
cout << it << endl;
}
loop through set c++
// set::begin/end
#include <iostream>
#include <set>
int main ()
{
int myints[] = {75,23,65,42,13};
std::set<int> myset (myints,myints+5);
std::cout << "myset contains:";
for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
iterate over a set in C++
//Method 1
// Iterate over all elements of set
// using range based for loop
for (auto& i : mySet)
{
cout << i << " , ";
}
//Method 2
// Iterate over all elements using for_each
// and lambda function
for_each(mySet.begin(), mySet.end(), [](const auto & str)
{
cout<<str<<", ";
});
//Method 3
set<string>::iterator it = mySet.begin();
// Iterate till the end of set
while (it != mySet.end())
{
// Print the element
cout << *it << ", ";
//Increment the iterator
it++;
}
through set c++
//Method 1
for (auto& i : mySet)
{
cout << i << " ";
}
//Method 2
for_each(mySet.begin(), mySet.end(), [](const auto & str)
{
cout<<str<<" ";
});
//Method 3
set<string>::iterator it = mySet.begin();
while (it != mySet.end()) {
cout << *it << " ";
it++;
}
//Method 4
for (set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
cout <<*it << " ";
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