No you did not D:
#include <string>
#include <exception>
#include <utility>
#include <algorithm>
#include <vector>
namespace {
int generate_user_id() {
static int counter = 0;
return ++counter;
}
}
class User {
private:
int m_id;
std::string m_name;
public:
User(const std::string& p_name = "") noexcept : m_name(p_name), m_id(generate_user_id()) {}
User(const User& rhs) noexcept : m_name(rhs.m_name), m_id(rhs.m_id) {}
User& operator =(const User& rhs) noexcept
{
this->m_name = rhs.m_name;
this->m_id = rhs.m_id;
return *this;
}
virtual ~User() noexcept {}
inline const bool operator ==(const User& rhs) const noexcept { return this->m_id == rhs.m_id; }
inline const bool operator !=(const User& rhs) const noexcept { return this->m_id != rhs.m_id; }
inline const int get_id() const noexcept { return m_id; }
inline const std::string& get_name() const noexcept { return m_name; }
};
class UserDatabase {
private:
std::vector<User> user_list;
public:
UserDatabase() noexcept {}
UserDatabase(const UserDatabase& rhs) noexcept
{
for (auto node = rhs.user_list.begin(); node != rhs.user_list.end(); ++node)
{
user_list.push_back(*node);
}
}
UserDatabase& operator =(const UserDatabase& rhs) noexcept
{
user_list.clear();
for (auto node = rhs.user_list.begin(); node != rhs.user_list.end(); ++node)
{
user_list.push_back(*node);
}
return *this;
}
virtual ~UserDatabase() noexcept {}
inline void add_user(const User& user) noexcept { user_list.push_back(user); }
inline void add_user(const User* const user) noexcept { user_list.push_back(*user); }
inline const int user_count() const noexcept { return user_list.size(); }
const User& get_user(int u_id) const
{
for (auto node = user_list.begin(); node != user_list.end(); ++node)
{
if (u_id == node->get_id())
{
return *node;
}
}
throw std::runtime_error("User Not Found (" + std::to_string(u_id) + ")");
}
const User& get_user(const std::string& u_name) const
{
for (auto node = user_list.begin(); node != user_list.end(); ++node)
{
if (u_name == node->get_name())
{
return *node;
}
}
throw std::runtime_error("User Not Found (" + u_name + ")");
}
void remove_user(const User& which_user) noexcept
{
auto node = std::find(user_list.begin(), user_list.end(), which_user);
if (node != user_list.end())
{
user_list.erase(node);
}
}
};
This is how you should've done it ;_;
This code is almost flawless in terms of standard C++11 shit
The only things it lacks are move constructors for the class but you don't really need those here anyway.
Usage example:
int main() {
UserDatabase database;
database.add_user(new User("Magtheridon96"));
database.add_user(new User("-Kobas-"));
database.add_user(new User("Geries"));
database.add_user(new User("Kelly"));
database.add_user(new User("Kellypoo"));
std::cout << "User count is " << database.user_count() << std::endl;
try
{
std::cout << "User id of -Kobas- is " << database.get_user("-Kobas-").get_id() << std::endl;
std::cout << "User id of Magtheridon96 is " << database.get_user("Magtheridon96").get_id() << std::endl;
std::cout << "User id of Ralle is " << database.get_user("Ralle").get_id() << std::endl;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
return 0;
}
And it actually works.