String set in cpp

Creating a Set in C++ STL

Learn Algorithms and become a National Programmer

Set is a container that is used to store the values which are unique i.e no value in a set can be repeated and no value can be repeated. If you want to edit the value added by you the only way is to remove the wrong element and add the correct one.

The value stored in the set can be in ascending or desending order (with the help of greater function).

By default it is stored in ascending order.

For using set you need to mention either of the header files mentioned :

#include #include #include #include

Defining a set

A set is defined as follows:

  • datatype can be a primitive datatype like int or a user defined type
  • comparison function defines the sorting order of the datatype and is optional

Intializing set with int data type

The following example helps you to undestand how the set declared by you can be a type of int and give you the value stored in your set.

//int set with int values #include #include int main() < std::seta; a.insert(1); a.insert(2); a.insert(3); for(auto& str: a) std::cout

Inserting char instead of int

The following example store the value of char inserted with the help of ' ' and the set defined is int so the value printed is the ascii values.

//int set with values in ' ' #include #include int main() < std::seta; a.insert('1'); a.insert('2'); a.insert('3'); for(auto& str: a) std::cout

Intializing set with char data type

In the just above example we saw that when an int set gets a char value it prints the respective value so in the below example we have changed the type of set into char and hence get the desired output.

//char set with char values #include #include int main() < std::seta; a.insert('1'); a.insert('2'); a.insert('3'); for(auto& str: a) std::cout

When do the vise versa of code 2 in the intializing set with int data type we get no result as char to int conversion is not possible in set.

//char set with char values #include #include int main() < std::seta; a.insert(1); a.insert(2); a.insert(3); for(auto& str: a) std::cout

Intializing set with array

In the following example we assign the value of static array into a set. Since the array has a duplicate value hence the set only stores only one value.
Hence the output only have one value of each value in array.

#include #include #include using namespace std; int main() < static int const a[] = ; set const numbers( begin( a ), end( a ) ); for( auto const v : numbers ) < cout cout

Intializing set with string

In the following example we first declare a set with sting data type and store the value in set using " " and then print the value.

#include #include #include int main() < std::seta; a.insert("1"); a.insert("2"); a.insert("3"); for(auto& str: a) std::cout

Intializing set with user defined function

In the following example with the help of a user defined class we compare the value in set and print them in descending order.

#include #include #include class comp < public : template bool operator () (const T &l,const T&r) < return l>r; > >; int main() < std::setnews = ; // note provided a custom comparison function for(auto i : news) < std::coutreturn 0; > 

Harshita Sahai

Harshita Sahai

Maintainer at OpenGenus | Previously Software Developer, Intern at OpenGenus (June to August 2019) | B.Tech in Information Technology from Guru Gobind Singh Indraprastha University (2017 to 2021)

Читайте также:  text-transform

OpenGenus Tech Review Team

Software Engineering

Work with files using File System Module in Node.js

In this article we'll be dealing with the files & perform basic operations such as Creating, Reading, Updating, Deleting & Renaming in Node.JS

Add elements to your vector using Vector::push_back() in C++

In this article we will take a look into one of the most conventional ways of taking input in the vector as vector::push_back() and will try to find the complexity and other aspects in which it differs from the usual array implementation in taking the input.

Источник

Create a STD Set in C++

The following is a set of strings (items on a reading table):

In C++, each value in each of the above sets, is called a key.

In C++, a set does not allow duplicate values. However, still in C++, a multiset allows duplicate values. This article addresses set, and does not address multiset.

STD means Standard. This article is on how to create a standard set in C++. Adding elements (values) into the set, is also mentioned.

Library

C++ has one main library, called the C++ Standard Library. This library has sub libraries that are also divided into further sub libraries which are divided further into more sub libraries. The bottom sub-libraries can be seen as modules. The first level sub-library of interest here is called the Containers Library. The Containers Library has a sub-library, called the Associative Containers Library. The Associative Containers Library has a sub-library called the set library. This set library can be considered as a module. In order to code sets, it has to be included at the beginning of the program as follows:

Читайте также:  Check map for key java

iostream should always be included if the terminal (console) is to be used for output (and input). The second line in this code segment includes the set module. The third line is a statement ending with a semicolon, the insists on the use of the standard namespace.

In order to compile the program, with the g++20 compiler for C++ 20, use the following command:

assuming that the compiled file is in the user (home) directory.

Constructing a Set

Constructing or creating a set is the main issue of this article. There are many constructors for the set. Only the most commonly used will be explained here.

Constructing an Empty Set

The following statement will construct an empty set:

It begins with the class type. This is followed by angle brackets, which has the type for the elements (values). There is a space and then the name of the set (st).

Inserting Values

Elements can be inserted with the insert() method of the set class, as follows:

set < int >st ;
st. insert ( - 5 ) ; st. insert ( 6 ) ; st. insert ( 9 ) ;
st. insert ( 8 ) ; st. insert ( - 2 ) ;

The set has been inserted.

Returning an Iterator

The set class does not have the square brackets operator, like the array. So, to scan the elements of the set, an iterator is needed. If the name of the set is st, then the following statement will return an iterator that points to the first element of the set:

Appreciate the syntax of this statement.

Size of the Set

The following statement returns the size of a set:

The variable, sz, holds the size of the set.

Reading Values of the Set

The following program uses the iterator to read all the values in the set:

set < int >st ;
st. insert ( - 5 ) ; st. insert ( 6 ) ; st. insert ( 9 ) ;
st. insert ( 8 ) ; st. insert ( - 2 ) ;

for ( set < int >:: iterator iter = st. begin ( ) ; iter ! = st. end ( ) ; iter ++ )
cout cout

Note how the for-loop and the iterator were used. “st.end()” returns the end iterator which points just after the last element.

With strings as elements, the string module has to be included with;

Consider the following code with string elements:

set < string >st ;
st. insert ( "reading lamp" ) ; st. insert ( "computer" ) ; st. insert ( "pen" ) ;
st. insert ( "pencil" ) ; st. insert ( "exercise books" ) ; st. insert ( "text books" ) ;

for ( set < string >:: iterator iter = st. begin ( ) ; iter ! = st. end ( ) ; iter ++ )
cout cout

Note that when values are added with the insert() command, the set is sorted internally.

Note also that, to use strings, the string class has to be included; otherwise, it is the pointers to the strings that will be sorted and not the string alphabetic literals themselves.

Читайте также:  Enabling javascript and updating flash

set(const set& x)
This is a set constructor, that would take the identifier of another set as argument, to construct a new set. The following code illustrates this:

set st ;
st. insert ( - 5 ) ; st. insert ( 6 ) ; st. insert ( 9 ) ; st. insert ( 8 ) ; st. insert ( - 2 ) ;

for ( set < int >:: iterator iter = st2. begin ( ) ; iter ! = st2. end ( ) ; iter ++ )
cout cout

set(initializer_list, const Compare& = Compare(), const Allocator& = Allocator())

This is a constructor, where the second and third arguments are optional. When not given, the default values are chosen by C++. The first argument is an initializer_list (array literal). The following code illustrates the use of the constructor:

set < char >st ( { 'B' , 'M' , 'A' , 'C' , 'T' , 'O' , 'Q' } ) ;

for ( set < char >:: iterator iter = st. begin ( ) ; iter ! = st. end ( ) ; iter ++ )
cout cout

Notice that the output is sorted despite the fact that the input is an unsorted initializer_list.

Note: With the initializer_list, the parentheses of the constructor call, may be omitted, as in the following code:

set < char >st { 'B' , 'M' , 'A' , 'C' , 'T' , 'O' , 'Q' } ;

for ( set < char >:: iterator iter = st. begin ( ) ; iter ! = st. end ( ) ; iter ++ )
cout cout

Copy Constructors

A set can be created by assigning the identifier of another set to the identifier of the new set, or by assigning the literal set (array literal) to the identifier of the new set.

set& operator=(const set& x)
This assigns the identifier of another set to the identifier of a new set as shown, thus:

set < char >st ;
st. insert ( 'B' ) ; st. insert ( 'M' ) ; st. insert ( 'A' ) ; st. insert ( 'C' ) ;
st. insert ( 'T' ) ; st. insert ( 'O' ) ; st. insert ( 'Q' ) ;

for ( set < char >:: iterator iter = st2. begin ( ) ; iter ! = st2. end ( ) ; iter ++ )
cout cout

set& operator=(initializer_list)
This assigns the literal set (array literal) to the identifier of a new set as shown, thus:

set < char >st = { 'B' , 'M' , 'A' , 'C' , 'T' , 'O' , 'Q' } ;

for ( set < char >:: iterator iter = st. begin ( ) ; iter ! = st. end ( ) ; iter ++ )
cout cout

Conclusion

The set literal in C++ is similar to that of mathematics. A set, which is not sorted becomes sorted, ascending, after construction (creation) with the default settings. STD means Standard. The common ways of creating a set has been illustrated above.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.

Источник

Оцените статью