English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ set insert() usage and example

C++ STL Set(集合)

C++ set insert()is used to insert elements into the setInserting a new element.

because the element key in the set isUniquefirst checks whether the given key already exists in the set, and if the key exists in the set, it will not insert it into the set and iterate to the existing iterator to return the key, otherwise, it will insert the new element into the set.

Syntax

// Parameter passing by normal reference
pair<iterator,bool> insert(const value_type& val);
// Parameter passing by right value reference
pair<iterator,bool> insert(value_type&& val);
// Pass the val value by normal reference
iterator insert(const_iterator position, const value_type& val);
// Pass the val value by right value reference
iterator insert(const_iterator position, value_type&& val);
template<class InputIterator>
  void insert(InputIterator first, InputIterator last);
  void insert(initializer_list<value_type> il); // Since C++ 11

Parameters

val: The value to be inserted into the set.

position: The position in the element where the element is to be inserted.

first: The start of the range to be inserted.

last: The end of the range to be inserted.

il: Initialize the list.

Return value

Returns a boolean pair to indicate whether the insertion occurred and returns an iterator to the newly inserted element.

Complex

  • If a single element is inserted, the complexity will be logarithmic.

  • If a hint is given and the given position is the best position, the complexity will be amortized to constant.

Iterator validity

No changes.

Data races

The container has been modified.

Exception safety

This function does not throw an exception.

Example 1

Let's look at a simple example of inserting elements into a set:

#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<int> s;
 
    // Function to insert elements
    // In the set container
    s.insert(1);
    s.insert(4);
    s.insert(2);
    s.insert(5);
    s.insert(3);
 
    cout << "Gli elementi della raccolta sono: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
 
    return 0;
}

输出:

Gli elementi della raccolta sono: 1 2 3 4 5

Negli esempi precedenti, gli elementi sono stati inseriti utilizzando la chiave fornita.

Esempio 2

Lasciate che vi mostriamo un esempio semplice, l'inserimento di un elemento in una posizione specifica:

#include <iostream>
#include <set>
using namespace std;
int main()
{ 
    set<int> s;
 
     //La funzione di inserimento degli elementi
     //Nel contenitore set
    auto itr = s.insert(s.begin(), 1);
 
    itr = s.insert(itr, 4);
    itr = s.insert(itr, 2);
    itr = s.insert(itr, 5);
    itr = s.insert(itr, 3);
 
    cout << "Gli elementi della raccolta sono: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
 
    return 0;
}

输出:

Gli elementi della raccolta sono: 1 2 3 4 5

Negli esempi precedenti, gli elementi sono stati inseriti nella posizione definita.

Esempio 3

Lasciate che vi mostriamo un esempio semplice, l'inserimento di un insieme di elementi in un altro insieme nel range specificato:

#include <iostream>
# include<iostream>
# include<set>
using namespace std;
int main()
{ 
    set<int> s1;
 
    //La funzione di inserimento degli elementi
     //Nel contenitore set
    s1.insert(1);
    s1.insert(4);
    s1.insert(2);
    s1.insert(5);
    s1.insert(3);
 
    cout << "Gli elementi di set1 sono: ";
    for (auto it = s1.begin(); it != s1.end(); it++)
        cout << *it << " ";
 
    set<int> s2;
 
    //La funzione inserisce un insieme in un altro insieme
    //Tutti gli elementi da 3 alla fine
    //Inserisci in set2
    s2.insert(s1.find(3), s1.end());
 
    cout << "\nGli elementi di set2 sono: ";
    for (auto it = s2.begin(); it != s2.end(); it++)
        cout << *it << " ";
 
    return 0;
}

输出:

Gli elementi di set1 sono: 1 2 3 4 5 
Gli elementi di set2 sono: 3 4 5

Esempio 4

Lasciate che vi mostriamo un esempio semplice, l'inserimento di elementi da una lista di inizializzazione:

#include <iostream>
#include <set>
using namespace std;
int main(void) {
   set<string> m = {"Java", "C++", "SQL"};
   
  // 插入初始化器列表中的元素
   m.insert({"VB", "Oracle"});
   cout << "集合包含以下元素:" << endl;
   for (auto it = m.begin(); it != m.end(); ++it)
      cout << *it<< endl;
   return 0;
}

输出:

集合包含以下元素:
C++
Java
Oracle
SQL
VB

在上面的示例中,元素是从初始化列表中插入的。

C++ STL Set(集合)