
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
- Related Articles
- Haskell Program to Convert Set of String to Array of String
- Swift Program to Convert Set of String to Array of String
- Golang Program To Convert Set Of String To Array Of String
- Convert Set of String to Array of String in Java
- C# Program to convert integer array to string array
- Swift Program to Convert an Set of String to Comma Separated String
- Program to convert Set of Integer to Set of String in Java
- Program to convert set of String to set of Integer in Java
- Java Program to convert Char array to String
- Java Program to convert byte[] array to String
- Java Program to convert String to byte array
- Haskell Program to Convert String to an Array
- Golang Program to Convert String to an Array
- Swift program to convert string to an array
- Swift program to convert array to a string
