注意看,这是Python
的输入输出:
xxxxxxxxxx
a = input()
print(a)
而这是C++
的输入输出:
xxxxxxxxxx
int a;
cin >> a;
cout << a;
我们可以想到: 为什么不能在定义的时候就输入,简单的输出呢?
(其实C++ 23
也改变了输出,变成了print()
函数,像这么用:print("{}{}", "114514", 1919810);
)
于是,我们可以想到这么一个代码框架:
xxxxxxxxxx
static_assert(__cplusplus, "This header must be included in C++ file");
namespace std {
// TODO
}
那么,根据print()
的第一个参数fmt_string
,我们可以把类名定义成这样:
xxxxxxxxxx
static_assert(__cplusplus, "This header must be included in C++ file");
namespace std {
class FormatString {
public:
private:
};
}
我们可以往成员组里添加一个成员:string s
,用来存储字符串,并加上亿个构造函数
xxxxxxxxxx
static_assert(__cplusplus, "This header must be included in C++ file");
namespace std {
class FormatString {
public:
FormatString() {}
FormatString(int right) : s(to_string(right)) {}
FormatString(string_view right) : s(right) {}
FormatString(const string& right) : s(right) {}
FormatString(double right) : s(to_string(right)) {}
FormatString(long long right) : s(to_string(right)) {}
FormatString(char right) : s(1, right) {}
FormatString(const char* right) : s(right) {}
FormatString(size_t right) : s(to_string(right)) {}
private:
string s;
};
}
既然都有了这么多构造函数了,为什么不加上亿个转换函数呢?
xxxxxxxxxx
static_assert(__cplusplus, "This header must be included in C++ file");
namespace std {
class FormatString {
public:
FormatString() {}
FormatString(int right) : s(to_string(right)) {}
FormatString(string_view right) : s(right) {}
FormatString(const string& right) : s(right) {}
FormatString(double right) : s(to_string(right)) {}
FormatString(long long right) : s(to_string(right)) {}
FormatString(char right) : s(1, right) {}
FormatString(const char* right) : s(right) {}
FormatString(size_t right) : s(to_string(right)) {}
operator int() { return stoi(s); }
operator string() { return s; }
operator double() { return stod(s); }
operator long long() { return stoll(s); }
operator size_t() { return stoull(s); };
private:
string s;
};
}
是个字符串,必须加个比较把:
xxxxxxxxxx
static_assert(__cplusplus, "This header must be included in C++ file");
namespace std {
class FormatString {
public:
FormatString() {}
FormatString(int right) : s(to_string(right)) {}
FormatString(string_view right) : s(right) {}
FormatString(const string& right) : s(right) {}
FormatString(double right) : s(to_string(right)) {}
FormatString(long long right) : s(to_string(right)) {}
FormatString(char right) : s(1, right) {}
FormatString(const char* right) : s(right) {}
FormatString(size_t right) : s(to_string(right)) {}
operator int() { return stoi(s); }
operator string() { return s; }
operator double() { return stod(s); }
operator long long() { return stoll(s); }
operator size_t() { return stoull(s); };
strong_ordering operator<=>(const FormatString& right) {
return s <=> right.s;
}
bool operator==(const FormatString& right) {
return s == right.s;
}
bool operator<(const FormatString& right) {
return s < right.s;
}
bool operator>(const FormatString& right) {
return s > right.s;
}
bool operator<=(const FormatString& right) {
return s <= right.s;
}
bool operator>=(const FormatString& right) {
return s >= right.s;
}
bool operator!=(const FormatString& right) {
return s != right.s;
}
private:
string s;
};
}
最后加个输出输出,以便后面套壳:
xxxxxxxxxx
static_assert(__cplusplus, "This header must be included in C++ file");
namespace std {
class FormatString {
public:
FormatString() {}
FormatString(int right) : s(to_string(right)) {}
FormatString(string_view right) : s(right) {}
FormatString(const string& right) : s(right) {}
FormatString(double right) : s(to_string(right)) {}
FormatString(long long right) : s(to_string(right)) {}
FormatString(char right) : s(1, right) {}
FormatString(const char* right) : s(right) {}
FormatString(size_t right) : s(to_string(right)) {}
operator int() { return stoi(s); }
operator string() { return s; }
operator double() { return stod(s); }
operator long long() { return stoll(s); }
operator size_t() { return stoull(s); };
strong_ordering operator<=>(const FormatString& right) {
return s <=> right.s;
}
bool operator==(const FormatString& right) {
return s == right.s;
}
bool operator<(const FormatString& right) {
return s < right.s;
}
bool operator>(const FormatString& right) {
return s > right.s;
}
bool operator<=(const FormatString& right) {
return s <= right.s;
}
bool operator>=(const FormatString& right) {
return s >= right.s;
}
bool operator!=(const FormatString& right) {
return s != right.s;
}
FormatString operator+(const FormatString& right) {
return FormatString(s + right.s);
}
FormatString operator+(int right) {
return FormatString(s + to_string(right));
}
FormatString operator+(const string& right) {
return FormatString(s + right);
}
FormatString operator+(double right) {
return FormatString(s + to_string(right));
}
FormatString operator+(long long right) {
return FormatString(s + to_string(right));
}
FormatString operator+(char right) {
return FormatString(s + string(1, right));
}
FormatString operator+(const char* right) {
return FormatString(s + string(right));
}
FormatString operator+(size_t right) {
return FormatString(s + to_string(right));
}
friend ostream& operator<<(ostream& os, const FormatString& fs) {
os << fs.s;
return os;
}
friend istream& operator>>(istream& is, FormatString& fs) {
is >> fs.s;
return is;
}
private:
string s;
};
}
顺便加上字符串转换""fs
和输入输出(顺便typedef FormatString fs
一下,方便)
x
static_assert(__cplusplus, "This header must be included in C++ file");
namespace std {
class FormatString {
public:
FormatString() {}
FormatString(int right) : s(to_string(right)) {}
FormatString(string_view right) : s(right) {}
FormatString(const string& right) : s(right) {}
FormatString(double right) : s(to_string(right)) {}
FormatString(long long right) : s(to_string(right)) {}
FormatString(char right) : s(1, right) {}
FormatString(const char* right) : s(right) {}
FormatString(size_t right) : s(to_string(right)) {}
operator int() { return stoi(s); }
operator string() { return s; }
operator double() { return stod(s); }
operator long long() { return stoll(s); }
operator size_t() { return stoull(s); };
strong_ordering operator<=>(const FormatString& right) {
return s <=> right.s;
}
bool operator==(const FormatString& right) {
return s == right.s;
}
bool operator<(const FormatString& right) {
return s < right.s;
}
bool operator>(const FormatString& right) {
return s > right.s;
}
bool operator<=(const FormatString& right) {
return s <= right.s;
}
bool operator>=(const FormatString& right) {
return s >= right.s;
}
bool operator!=(const FormatString& right) {
return s != right.s;
}
FormatString operator+(const FormatString& right) {
return FormatString(s + right.s);
}
FormatString operator+(int right) {
return FormatString(s + to_string(right));
}
FormatString operator+(const string& right) {
return FormatString(s + right);
}
FormatString operator+(double right) {
return FormatString(s + to_string(right));
}
FormatString operator+(long long right) {
return FormatString(s + to_string(right));
}
FormatString operator+(char right) {
return FormatString(s + string(1, right));
}
FormatString operator+(const char* right) {
return FormatString(s + string(right));
}
FormatString operator+(size_t right) {
return FormatString(s + to_string(right));
}
friend ostream& operator<<(ostream& os, const FormatString& fs) {
os << fs.s;
return os;
}
friend istream& operator>>(istream& is, FormatString& fs) {
is >> fs.s;
return is;
}
private:
string s;
};
FormatString operator""fs(const char* s, size_t len) {
return FormatString(string(s, len));
}
void print(const FormatString& _format) {
cout << _format;
}
void println(const FormatString& _format) {
cout << _format << endl;
}
FormatString input() {
FormatString fs;
cin >> fs;
return fs;
}
FormatString input(string_view s) {
cout << s;
return input();
}
FormatString input(const string& s) {
cout << s;
return input();
}
typedef FormatString fs;
}
OK!
// +--------------------------------+
// | Format.h |
// | Copyright (c) zhanghaoxvan. |
// | All rights reserved. |
// +--------------------------------+
// | Note: |
// | If you use the print function |
// | or the println function and |
// | the first item is not a |
// | variable of type FormatString, |
// | add the caster fs() to both |
// | sides of the first term. |
// +--------------------------------+
// | Write down your life motto: |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// +--------------------------------+
static_assert(__cplusplus, "This header must be included in C++ file");
namespace std {
class FormatString {
public:
FormatString() {}
FormatString(int right) : s(to_string(right)) {}
FormatString(string_view right) : s(right) {}
FormatString(const string& right) : s(right) {}
FormatString(double right) : s(to_string(right)) {}
FormatString(long long right) : s(to_string(right)) {}
FormatString(char right) : s(1, right) {}
FormatString(const char* right) : s(right) {}
FormatString(size_t right) : s(to_string(right)) {}
operator int() { return stoi(s); }
operator string() { return s; }
operator double() { return stod(s); }
operator long long() { return stoll(s); }
operator size_t() { return stoull(s); };
strong_ordering operator<=>(const FormatString& right) {
return s <=> right.s;
}
bool operator==(const FormatString& right) {
return s == right.s;
}
bool operator<(const FormatString& right) {
return s < right.s;
}
bool operator>(const FormatString& right) {
return s > right.s;
}
bool operator<=(const FormatString& right) {
return s <= right.s;
}
bool operator>=(const FormatString& right) {
return s >= right.s;
}
bool operator!=(const FormatString& right) {
return s != right.s;
}
FormatString operator+(const FormatString& right) {
return FormatString(s + right.s);
}
FormatString operator+(int right) {
return FormatString(s + to_string(right));
}
FormatString operator+(const string& right) {
return FormatString(s + right);
}
FormatString operator+(double right) {
return FormatString(s + to_string(right));
}
FormatString operator+(long long right) {
return FormatString(s + to_string(right));
}
FormatString operator+(char right) {
return FormatString(s + string(1, right));
}
FormatString operator+(const char* right) {
return FormatString(s + string(right));
}
FormatString operator+(size_t right) {
return FormatString(s + to_string(right));
}
friend ostream& operator<<(ostream& os, const FormatString& fs) {
os << fs.s;
return os;
}
friend istream& operator>>(istream& is, FormatString& fs) {
is >> fs.s;
return is;
}
private:
string s;
};
FormatString operator""fs(const char* s, size_t len) {
return FormatString(string(s, len));
}
void print(const FormatString& _format) {
cout << _format;
}
void println(const FormatString& _format) {
cout << _format << endl;
}
FormatString input() {
FormatString fs;
cin >> fs;
return fs;
}
FormatString input(string_view s) {
cout << s;
return input();
}
FormatString input(const string& s) {
cout << s;
return input();
}
typedef FormatString fs;
}