Answers for "how to iterate over a set in c++"

C++
0

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;
}
Posted by: Guest on March-11-2021
0

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 << " ";
Posted by: Guest on May-10-2021

Code answers related to "how to iterate over a set in c++"

Browse Popular Code Answers by Language