C++ Program to Convert Set of String to Array of String


Sets in C++ re containers that contain unique values of a specific type. Arrays or the array container in std C++ is a fixed-size container that contains elements of a specific size. Arrays are like vectors, but the main difference is that arrays are of a fixed size whereas vectors can be dynamic. The array container in C++ is a wrapper for the standard arrays that are available in both C and C++. There is a problem in this conversion though, std arrays do not have support for the common insertion methods that are available with the other containers. So, arrays are a bit limited for conversion to other containers. Let’s take a look at the conversion methods from a set containing strings to an array containing strings.

Naïve Method – Using std::array

To simplest and easiest way to insert the elements of a set into an array is by iterating through all the elements in the set and inserting them one by one into the array. The following example explains the process.

Syntax

set<string> mySet;
array<string, <size> > myArray;
int i = 0;
for ( string const &val: mySet ) {
   myArray[i] = val;
   i++;
}

Algorithm

  • Take input in a set.

  • Iterate through each element in the set and insert them one by one into the array.

  • Display the contents of the array.

Example

#include <iostream>
#include <set>
#include <array>
using namespace std;
int main(){
   
   //initializing the list
   set<string> mySet = {"lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit"};
   array<string, 8> myArray;
   cout<< "The set contents are:" << endl;
   
   //displaying the set contents
   for ( string const &val: mySet ) {
      cout << val << ' ';
   }
   
   //copying the set contents into the array
   int i = 0;
   for ( string const &val: mySet ) {
      myArray[i] = val;
      i++;
   }
   cout << "\nThe array contents are:" << endl;
   for ( string const &val: myArray ) {
      cout << val << ' ';
   }
   return 0;
}

Output

The set contents are:
adipiscing amet consectetur dolor elit ipsum lorem sit 
The array contents are:
adipiscing amet consectetur dolor elit ipsum lorem sit 

Naïve Method – Using normal arrays

The next method is easier, it is using normal C++ arrays. We follow the same approach here.

Syntax

set<string> mySet;
string myArray[<size of the set>];
int i = 0;
for ( string const &val: mySet ) {
   myArray[i] = val;
   i++;
}

Algorithm

  • Take input in a set.

  • Iterate through each element in the set and insert them one by one into the array.

  • Display the contents of the array.

Example

#include <iostream>
#include <set>
#include <array>
using namespace std;
int main(){
   
   //initializing the list
   set<string> mySet = {"lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit"};
   string myArray[8];
   cout<< "The set contents are:" << endl;
   
   //displaying the set contents
   for ( string const &val: mySet ) {
      cout << val << ' ';
   }
   
   //copying the set contents into the array
   int i = 0;
   for ( string const &val: mySet ) {
      myArray[i] = val;
      i++;
   }
   cout << "\nThe array contents are:" << endl;
   for (int i = 0; i < 8; i++ ) {
      cout << myArray[i] << ' ';
   }
   return 0;
}

Output

The set contents are:
adipiscing amet consectetur dolor elit ipsum lorem sit 
The array contents are:
adipiscing amet consectetur dolor elit ipsum lorem sit 

Conclusion

Arrays in the std C++ library add some features to the traditional arrays that are present in C and C++. But, the features of the normal array are also present in the std array, the biggest one being of a fixed size. Vectors are more versatile in this case and also are dynamic, so it allows more data to be inserted into them. However, there are applications of the std::array, and the conversion method from other containers to array is similar to converting from set to array.

Updated on: 14-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements