This is a set of strings. It is possible in C++ to have a set of integers, a set of floats, a set of doubles, etc. This is also an array literal in C++. It is also an initializer_list. It is the set literal, too, though not sorted.
In order to have a set of the above strings, the C++ program should begin as follows:
#include <set>
#include <string>
using namespace std;
The first three lines are directives. The last line is a statement. The first three lines include the necessary libraries. The last line insists on the use of the standard namespace.
The set class has many insert() overloaded member functions. Only four that are most appropriate, will be explained in this article. The four explained are for set and not multiset.
A set is always sorted internally, ascending by default. Whenever a value is inserted, the set is re-sorted.
void insert(initializer_list<value_type>)
This member function takes as argument the initializer_list, which is the array literal (same as set literal). It can insert values into an empty set also. The function returns void. The following program shows the member function in action:
#include <set>
#include <string>
using namespace std;
int main()
{
set<string> st;
st.insert({"gray", "white", "aqua", "black", "fuchsia", "blue"});
for (set<string>::iterator it = st.begin(); it != st.end(); it++) {
cout << *it << ", ";
}
cout << endl;
return 0;
}
The output is:
Note that the output is in ascending order of string literals. If the string library is not included and const-char* is used instead, then it is the pointers that would be sorted and not the string literals.
The set class has a constructor which can take the initializer_list. In this case, there will be no need for initial inserting. The following code illustrates this:
for (set<string>::iterator it = st.begin(); it != st.end(); it++) {
cout << *it << ", ";
}
cout << endl;
The output is still,
for the same input; output sorted ascending.
template<class InputIterator> void insert(InputIterator first, InputIterator last)
This member function will insert a range from another set. The range of the other set begins from where the iterator first is pointing to, but just not including the value that the iterator last is pointing to. The function returns void. The following code illustrates this:
for (set<string>::iterator it = st2.begin(); it != st2.end(); it++) cout << *it << ", "; cout << endl;
set<string>::iterator itB2 = st2.begin(); set<string>::iterator itE2 = st2.end();
itB2++;itB2++; itE2--; itE2--; itE2--;
set<string> st({"gray", "white", "aqua", "black", "fuchsia", "blue"});
for (set<string>::iterator it = st.begin(); it != st.end(); it++) cout << *it << ", "; cout << endl;
st.insert(itB2, itE2);
for (set<string>::iterator it = st.begin(); it != st.end(); it++) cout << *it << ", "; cout << endl;
The output is:
aqua, black, blue, fuchsia, gray, white,
aqua, black, blue, fuchsia, gray, purple, red, white,
The sorted range (purple, red, silver) from the set st2, without “silver” was inserted into the set st. st was re-sorted automatically to have the third line of the output.
iterator insert(const_iterator position, const value_type& x)
The second argument of this member function is the variable of a constant-pointer-to-type(char). This member function should fit the string pointer in the position pointed to by the iterator which is the first argument. This is not likely going to work as appeared because of the sorting that is to take place after insertion. The member function returns an iterator that points to the inserted element. The following program illustrates this:
#include <set>
#include <string>
using namespace std;
int main()
{
const char* str = "purple";
set<string> st({"gray", "white", "aqua", "black", "fuchsia", "blue"});
for (set<string>::iterator it = st.begin(); it != st.end(); it++) cout << *it << ", "; cout << endl;
set<string>::const_iterator itB = st.begin(); itB++; itB++;
set<string>::iterator iter = st.insert(itB, str);
cout << *iter << endl;
for (set<string>::iterator it = st.begin(); it != st.end(); it++) cout << *it << ", "; cout << endl;
return 0;
}
The output is:
purple
aqua, black, blue, fuchsia, gray, purple, white,
iterator insert(const_iterator position, value_type&& x)
This member function is similar to the above but the second argument is actually the value literal and not the variable. The following program illustrates this:
#include <set>
#include <string>
using namespace std;
int main()
{
set<string> st({"gray", "white", "aqua", "black", "fuchsia", "blue"});
for (set<string>::iterator it = st.begin(); it != st.end(); it++) cout << *it << ", "; cout << endl;
set<string>::const_iterator itB = st.begin(); itB++; itB++;
set<string>::iterator iter = st.insert(itB, "purple");
cout << *iter << endl;
for (set<string>::iterator it = st.begin(); it != st.end(); it++) cout << *it << ", "; cout << endl;
return 0;
}
The output is:
purple
aqua, black, blue, fuchsia, gray, purple, white,
Conclusion
A set in C++ can be created empty. If it is created empty, then the insert() member function can be used to inset the initial elements of the set. In this case, the initializer_list has to be used as sole argument to the insert function. The corresponding overloaded member function, returns void.
A set is always sorted internally ascending by default. Whenever a value is inserted, the set is re-sorted automatically. The set library has to be included for the set to be coded.
There are three other commonly used set insert() methods. One returns void and the other two return an iterator pointing to the element inserted. The one that returns void takes a range from another set and inserts into the set of interest. The range is identified in the syntax by the iterators, first and last. Last is just not included in the inserted range.
For the other two member functions, one inserts the variable of a value and the other inserts the literal value itself. Both insert into some intended positions. The intended positions may not be respected as sorting takes place after insertion.