How to remove duplicate elements from a forward_list in C++

1 Answer

0 votes
#include <iostream>  
#include <forward_list> 

using std::forward_list;
using std::cout;

void printList(const forward_list<int>& flist)
{
	for (auto elem : flist) {
		cout << elem << ' ';
	}
	cout << std::endl;
}

int main()
{
	forward_list<int> flist = { 1, 1, 2, 3, 4, 3, 4, 4, 5, 5, 5 };

	flist.sort();
	flist.unique();

	printList(flist);

	return 0;
}

/*
run:

1 2 3 4 5

*/

 



answered Jan 8, 2018 by avibootz

Related questions

1 answer 207 views
1 answer 172 views
2 answers 221 views
1 answer 220 views
1 answer 180 views
...