función C ++ conjunto STL :: emplace () : Aquí, vamos a aprender acerca de la función emplace () del conjunto en C ++ STL (Standard Template Library).
C ++ STL set :: emplace () Función
set :: emplace () es una función predefinida, se utiliza para insertar un nuevo elemento en el conjunto, si el elemento es único.
Prototipo:
set<T> st; //declaration
st.emplace(T item);
Parámetro: T artículo; // T es el tipo de datos
class escribir: Si se inserta con éxito, entonces devuelve un par & lt; puntero Iterator al valor insertado, True & gt , si no devuelve un par & lt; iterador al valor existente en el conjunto, Falso & gt;
Uso: La función inserta elementos únicos al conjunto.
Ejemplo:
For a set of integer,
set<int> st;
st.emplace(5);
st.emplace(4);
set content: //sorted always(ordered)
4
5
st.emplace(5) //no insertion this time
set content:
4
5
Archivo de encabezado que se incluirá:
#include <iostream>
#include <set>
OR
#include <bits/stdc++.h>
C ++ aplicación:
#include <bits/stdc++.h>
using namespace std;
void printSet(set<int> st){
set<int>:: iterator it;
cout<<"Set contents are:n";
if(st.empty()){
cout<<"empty setn";
return;
}
for(it=st.begin();it!=st.end();it++)
cout<<*it<<" ";
cout<<endl;
}
int main(){
cout<<"Example of emplace functionn";
set<int> st;
set<int>:: iterator it;
cout<<"inserting 4n";
st.emplace(4);
cout<<"inserting 6n";
st.emplace(6);
cout<<"inserting 10n";
st.emplace(10);
printSet(st); //printing current set
return 0;
}
salida
Example of emplace function
inserting 4
inserting 6
inserting 10
Set contents are:
4 6 10