Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to convert array to set in C++

3 Answers

0 votes
#include <iostream>
#include <unordered_set>

int main()
{
	int arr[] = { 5, 7, 3, 2, 8 };
	std::unordered_set<int> st;
	
	for (int n: arr) {
		st.insert(n);
	}

	for (int n: st) {
		std::cout << n << " ";
	}
}



/*
run:

8 3 5 2 7 

*/

 



answered Sep 22, 2020 by avibootz
edited May 9, 2024 by avibootz
0 votes
#include <iostream>
#include <set>
 
int main()
{
    int arr[] = { 5, 7, 3, 2, 8 };
    std::set<int> st;
    
    for (int n: arr) {
        st.insert(n);
    }
 
    for (int n: st) {
        std::cout << n << " ";
    }
}
 
 
 
/*
run:
 
2 3 5 7 8  
 
*/

 



answered Sep 22, 2020 by avibootz
edited May 9, 2024 by avibootz
0 votes
#include <iostream>
#include <unordered_set>
 
template <size_t N> std::unordered_set<int> ConvertArray(int (&arr)[N]) {
    std::unordered_set<int> set;
    
    for (int val : arr) {
        set.insert(val);
    }
    
   return set;
}
 
int main() {
    int arr[] = { 3, 9, 0, 4, 7, 6, 2, 2, 2, 3 };
    std::unordered_set<int> set = ConvertArray(arr);
 
    for (int val : set) {
        std::cout << val << " ";
    }
}
 
 
 
 
/*
run:
 
2 6 7 4 0 9 3 
 
*/

 



answered May 9, 2024 by avibootz

Related questions

1 answer 138 views
138 views asked Sep 22, 2020 by avibootz
1 answer 70 views
2 answers 122 views
1 answer 94 views
1 answer 117 views
117 views asked Jun 21, 2020 by avibootz
...