forked from parallel101/course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include <cstdio> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
string s1 = "hello"; | ||
string s2 = "world"; | ||
string s3 = s1 + s2; | ||
printf("%s\n", s3.c_str()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
#include <string> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <iomanip> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
int n = 42; | ||
auto s = to_string(n) + " yuan"s; | ||
cout << s << endl; | ||
//string s = (stringstream() << setprecision(100) << 3.1415f).str(); | ||
//stringstream("3.1415926535") >> f; | ||
int n = 10011; | ||
cout << setprecision(4) << (n * 0.01) << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include <string> | ||
#include <string_view> | ||
#include <stdexcept> | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <locale> | ||
|
||
using namespace std; | ||
|
||
|
||
int main() { | ||
string s = "你好"; | ||
wstring ws = L"你好"; | ||
cout << s.size() << endl; | ||
cout << ws.size() << endl; | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include "scienum.h" | ||
|
||
enum Color { | ||
RED = 1, GREEN = 2, BLUE = 3, YELLOW = 4, | ||
}; | ||
|
||
int main() { | ||
std::cout << scienum::get_enum_name(YELLOW) << std::endl; | ||
std::cout << scienum::enum_from_name<Color>("YELLOW") << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace scienum { | ||
|
||
namespace details { | ||
|
||
template <class T, T N> | ||
const char *get_enum_name_static() { | ||
#if defined(_MSC_VER) | ||
return __FUNCSIG__; | ||
#else | ||
return __PRETTY_FUNCTION__; | ||
#endif | ||
} | ||
|
||
template <bool Cond> | ||
struct my_enable_if { | ||
}; | ||
|
||
template <> | ||
struct my_enable_if<true> { | ||
typedef int type; | ||
}; | ||
|
||
template <int Beg, int End, class F, typename my_enable_if<Beg == End>::type = 0> | ||
void static_for(F const &func) { | ||
} | ||
|
||
template <int Beg, int End, class F, typename my_enable_if<Beg != End>::type = 0> | ||
void static_for(F const &func) { | ||
struct int_constant { | ||
enum { value = Beg }; | ||
}; | ||
func(int_constant()); | ||
static_for<Beg + 1, End>(func); | ||
} | ||
|
||
} | ||
|
||
template <int Beg = 0, int End = 256, class T> | ||
std::string get_enum_name(T n) { | ||
std::string s; | ||
details::static_for<Beg, End + 1>([&] (auto i) { | ||
if (n == (T)i.value) s = details::get_enum_name_static<T, (T)i.value>(); | ||
}); | ||
if (s.empty()) | ||
return std::to_string((int)n); | ||
#if defined(_MSC_VER) | ||
auto pos = s.find(','); | ||
pos += 1; | ||
auto pos2 = s.find('>', pos); | ||
#else | ||
auto pos = s.find("N = "); | ||
pos += 4; | ||
auto pos2 = s.find_first_of(";]", pos); | ||
#endif | ||
s = s.substr(pos, pos2 - pos); | ||
auto pos3 = s.find("::"); | ||
if (pos3 != s.npos) | ||
s = s.substr(pos3 + 2); | ||
return s; | ||
} | ||
|
||
template <class T, int Beg = 0, int End = 256> | ||
T enum_from_name(std::string const &s) { | ||
for (int i = Beg; i < End; i++) { | ||
if (s == get_enum_name((T)i)) { | ||
return (T)i; | ||
} | ||
} | ||
throw; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters