xtd 0.2.0
Loading...
Searching...
No Matches
sprintf_class.cpp

Shows how to use xtd::string::sprintf method.

#include <xtd/collections/generic/list>
#include <xtd/console>
#include <xtd/environment>
#include <xtd/string>
using namespace xtd;
using namespace xtd::collections::generic;
class character : public iequatable<character>, public icomparable<character> {
public:
character() = default;
character(const string& name, const string& rank) noexcept : name_(name), rank_(rank) {}
const string& name() const noexcept {return name_;}
const string& rank() const noexcept {return rank_;}
string to_string() const noexcept {return name_ + " (" + rank_ + ")";}
int compare_to(const character& rhs) const noexcept override {
if (name_ == rhs.name_ && rank_ == rhs.rank_) return 0;
if (name_ > rhs.name_) return 1;
if (name_ == rhs.name_ && rank_ > rhs.rank_) return 1;
return -1;
}
bool equals(const character& rhs) const noexcept override {return compare_to(rhs) == 0;}
private:
string name_;
string rank_;
};
using characters = list<character>;
auto main() -> int {
for (auto c : characters {{"Jean-Luc Picard", "Captain"}, {"William Riker", "Commander"}, {"Data", "Commander"}, {"Beverly Crusher", "Commander"}, {"Geordi La Forge", "Lieutenant Commander"}, {"Worf", "Lieutenant Commander"}, {"Tasha Yar", "Lieutenant"}})
console::out << string::sprintf("%s", c.to_string()) << environment::new_line;
}
// This code produces the following output :
//
// Jean-Luc Picard (Captain)
// William Riker (Commander)
// Data (Commander)
// Beverly Crusher (Commander)
// Geordi La Forge (Lieutenant Commander)
// Worf (Lieutenant Commander)
// Tasha Yar (Lieutenant)
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.h:21
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.h:22
@ c
The C key.
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10