From 975f9124560f6e02f7c88ecb0777a14fc4b2f6bb Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Thu, 10 Jul 2014 15:56:15 +0000 Subject: [PATCH] Let users(1) use an std::set, instead of std::{vector,sort,unique}. Reviewed by: gahr --- usr.bin/users/users.cc | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/usr.bin/users/users.cc b/usr.bin/users/users.cc index ef64a9fcab46..8b2e26f36122 100644 --- a/usr.bin/users/users.cc +++ b/usr.bin/users/users.cc @@ -36,14 +36,14 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include +#include using namespace std; int main(int argc, char **) { struct utmpx *ut; - vector names; + set names; if (argc > 1) { cerr << "usage: users" << endl; @@ -51,19 +51,16 @@ main(int argc, char **) } setutxent(); - while ((ut = getutxent()) != NULL) { - if (ut->ut_type != USER_PROCESS) - continue; - names.push_back(ut->ut_user); - } + while ((ut = getutxent()) != NULL) + if (ut->ut_type == USER_PROCESS) + names.insert(ut->ut_user); endutxent(); - if (names.size() == 0) { - return (0); + if (!names.empty()) { + auto last = names.end(); + --last; + copy(names.begin(), last, ostream_iterator(cout, " ")); + cout << *last << endl; } - - sort(begin(names), end(names)); - vector::iterator last(unique(begin(names), end(names))); - copy(begin(names), last-1, ostream_iterator(cout, " ")); - cout << *(last-1) << endl; + return (0); }