Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

size() in c++ SET

//try to copy the code to your compiler and run it to understand how it works
#include<iostream>
#include<set>
using namespace std; 

int main(){
	set<int>myset;
	set<int>::iterator itbegin;
	set<int>::reverse_iterator itend;
	int n, val, i;
	cout << "Enter n: ";
	cin >> n;

	cout << "Enter " << n << " values: ";
	for (i = 0; i < n; i++){
		cin >> val;
		myset.insert(val);
	}

	cout << "Count of distinct values is " << myset.size() << endl;

	itbegin = myset.begin();
	cout << "Lowest value is " << *itbegin << endl;

	itend = myset.rbegin();
	cout << "Highest value is " << *itend << endl;

	cout << "Distinct values entered in ascending order: ";
	while (itbegin != myset.end()){
		cout << *itbegin << " ";
		itbegin++;
	}
	cout << endl;

	cout << "Distinct values entered in descending order: ";
	while (itend != myset.rend()){
		cout << *itend << " ";
		itend++;
	}
	cout << endl;

	system("pause");
	return 0;
}
 
PREVIOUS NEXT
Tagged: #SET
ADD COMMENT
Topic
Name
3+8 =