How to define initialize and print set in C++

2 Answers

0 votes
#include <iostream>
#include <set>
#include <string>

using std::cout;
using std::endl;
using std::string;
using std::set;

int main()
{
	set<string> st;

	st.insert("c++");
	st.insert("c");
	st.insert("java");
	st.insert("php");
	st.insert("python");

	set<string>::iterator p = st.begin();

	while (p != st.end()) {
		cout << *p << " ";
		p++;
	}
	cout << endl;

	return 0;
}


/*
run:

c c++ java php python

*/

 



answered Apr 23, 2018 by avibootz
0 votes
#include <iostream>
#include <set>
#include <string>

using std::cout;
using std::endl;
using std::string;
using std::set;

int main()
{
	set<string> st{ "c++", "c", "java", "php" };

	set<string>::iterator p = st.begin();

	while (p != st.end()) {
		cout << *p << " ";
		p++;
	}
	cout << endl;

	return 0;
}


/*
run:

c c++ java php

*/

 



answered Apr 23, 2018 by avibootz

Related questions

5 answers 305 views
2 answers 166 views
3 answers 208 views
1 answer 933 views
3 answers 270 views
1 answer 160 views
1 answer 220 views
...