C++ set insert elements

Introduction

To insert an element into a set, we use the set's.insert(value) member function.

To insert two new elements, we use:

#include <iostream> 
#include <set> 

int main() /* ww  w  . j a v a 2s .  c o m*/
{ 
    std::set<int> myset = { 1, 2, 3, 4, 5 }; 
    myset.insert(10); 
    myset.insert(42); 
        for (auto el : myset) 
    { 
        std::cout << el << '\n'; 
    } 
} 

Since the set holds unique values, the attempt to insert duplicate values will not succeed.




PreviousNext

Related