C++当代教程五

[复制链接]
发表于 2026-2-8 04:38:07 | 显示全部楼层 |阅读模式
  1. #pragma once
  2. _Pragma("once")
  3. # C/C++混合编程
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. // 一些c代码
  8. #ifdef __cplusplus
  9. }
  10. #endif
  11. # 继承构造
  12. struct A
  13. {
  14.   A(int i) {}
  15.   A(double d,int i){}
  16.   A(float f,int i,const char* c){}
  17.   //...等等系列的构造函数版本
  18. };
  19. old:
  20. struct B:A
  21. {
  22.   B(int i):A(i){}
  23.   B(double d,int i):A(d,i){}
  24.   B(folat f,int i,const char* c):A(f,i,e){}
  25.   //......等等好多个和基类构造函数对应的构造函数
  26. };
  27. new:
  28. struct B:A
  29. {
  30.   using A::A;
  31.   //关于基类各构造函数的继承一句话搞定
  32.   //......
  33. };
  34. #regex
  35. std::regex base_regex("([a-z]+)\\.txt");
  36. std::smatch base_match;
  37. for(const auto &fname: fnames) {
  38.     if (std::regex_match(fname, base_match, base_regex)) {
  39.         // sub_match 的第一个元素匹配整个字符串
  40.         // sub_match 的第二个元素匹配了第一个括号表达式
  41.         if (base_match.size() == 2) {
  42.             std::string base = base_match[1].str();
  43.             std::cout << "sub-match[0]: " << base_match[0].str() << std::endl;
  44.             std::cout << fname << " sub-match[1]: " << base << std::endl;
  45.         }
  46.     }
  47. }
  48. #Variadic templates
  49. // C++11
  50. template <typename First, typename... Args>
  51. auto sum(const First first, const Args... args) -> decltype(first) {
  52.   const auto values = {first, args...};
  53.   return std::accumulate(values.begin(), values.end(), First{0});
  54. }
  55. // C++17
  56. template <typename... Args>
  57. auto sum(Args... args) {
  58.     // Unary folding.
  59.     return (... + args);
  60. }
  61. template <typename... Args>
  62. bool logicalAnd(Args... args) {
  63.     // Binary folding.
  64.     return (true && ... && args);
  65. }
  66. bool b = true;
  67. bool& b2 = b;
  68. logicalAnd(b, b2, true); // == true
  69. #Inline namespaces
  70. namespace Program {
  71.   namespace Version1 {
  72.     int getVersion() { return 1; }
  73.     bool isFirstVersion() { return true; }
  74.   }
  75.   inline namespace Version2 {
  76.     int getVersion() { return 2; }
  77.   }
  78. }
  79. int version {Program::getVersion()};              // Uses getVersion() from Version2
  80. int oldVersion {Program::Version1::getVersion()}; // Uses getVersion() from Version1
  81. bool firstVersion {Program::isFirstVersion()};    // Does not compile when Version2 is added
  82. # std::move temp
  83. struct Bar {
  84.     int a = 1;
  85. };
  86. struct Foo {
  87.     Bar getBar() & { return bar; }
  88.     Bar getBar() const& { return Bar{}; }
  89.     Bar getBar() && { return std::move(Bar{}); }
  90. private:
  91.     Bar bar;
  92. };
  93. Foo foo;
  94. auto&& ref = foo.getBar();
  95. ref.a = 10;
  96. # Nested namespaces
  97. namespace A::B::C {
  98.   int i;
  99. }
  100. # __has_include
  101. #ifdef __has_include
  102. #  if __has_include(<optional>)
  103. #    include <optional>
  104. #    define have_optional 1
  105. #  elif __has_include(<experimental/optional>)
  106. #    include <experimental/optional>
  107. #    define have_optional 1
  108. #    define experimental_optional
  109. #  else
  110. #    define have_optional 0
  111. #  endif
  112. #endif
  113. # std::invoke
  114. template <typename Callable>
  115. class Proxy {
  116.   Callable c_;
  117. public:
  118.   Proxy(Callable c) : c_{ std::move(c) } {}
  119.   template <typename... Args>
  120.   decltype(auto) operator()(Args&&... args) {
  121.     // ...
  122.     return std::invoke(c_, std::forward<Args>(args)...);
  123.   }
  124. };
  125. const auto add = [](int x, int y) { return x + y; };
  126. Proxy p{ add };
  127. p(1, 2); // == 3
  128. # std::clamp limitRange
  129. std::clamp(0, -1, 1, std::less<>{}); // == 0
  130. # std::reduce  
  131. const std::array<int, 3> a{ 1, 2, 3 };
  132. std::reduce(std::cbegin(a), std::cend(a)); // == 6  default binary operation is std::plus
  133. // Using a custom binary op:
  134. std::reduce(std::cbegin(a), std::cend(a), 1, std::multiplies<>{}); // == 6
  135. const std::array<int, 3> b{ 1, 2, 3 };
  136. const auto product_times_ten = [](const auto a, const auto b) { return a * b * 10; };
  137. std::transform_reduce(std::cbegin(a), std::cend(a), std::cbegin(b), 0, std::plus<>{}, product_times_ten); // == 140
  138. # std::inclusive_scan
  139. const std::array<int, 3> a{ 1, 2, 3 };
  140. std::inclusive_scan(std::cbegin(a), std::cend(a),
  141.     std::ostream_iterator<int>{ std::cout, " " }, std::plus<>{}); // 1 3 6
  142. std::exclusive_scan(std::cbegin(a), std::cend(a),
  143.     std::ostream_iterator<int>{ std::cout, " " }, 0, std::plus<>{}); // 0 1 3
  144. const auto times_ten = [](const auto n) { return n * 10; };
  145. std::transform_inclusive_scan(std::cbegin(a), std::cend(a),
  146.     std::ostream_iterator<int>{ std::cout, " " }, std::plus<>{}, times_ten); // 10 30 60
  147. std::transform_exclusive_scan(std::cbegin(a), std::cend(a),
  148.     std::ostream_iterator<int>{ std::cout, " " }, 0, std::plus<>{}, times_ten); // 0 10 30
  149.        
  150. # GCD and LCM 公倍数
  151. const int p = 9;
  152. const int q = 3;
  153. std::gcd(p, q); // == 3
  154. std::lcm(p, q); // == 9
  155. # std::not_fn
  156. # C++20 Coroutines
  157. generator<int> range(int start, int end) {
  158.   while (start < end) {
  159.     co_yield start;
  160.     start++;
  161.   }
  162.   // Implicit co_return at the end of this function:
  163.   // co_return;
  164. }
  165. for (int n : range(0, 10)) {
  166.   std::cout << n << std::endl;
  167. }
  168. # concept
  169. template <typename T>
  170. concept unsigned_integral = integral<T> && !signed_integral<T>;
  171. template <typename T>
  172. concept callable = requires (T f) { f(); };
  173. // `T` is a constrained type template parameter.
  174. template <typename T>
  175.   requires my_concept<T>
  176. void f(T v);
  177. # [[likely]] and [[unlikely]] attributes
  178. switch (n) {
  179. case 1:
  180.   // ...
  181.   break;
  182. [[likely]] case 2:  // n == 2 is considered to be arbitrarily more
  183.   // ...            // likely than any other value of n
  184.   break;
  185. }
  186. int random = get_random_number_between_x_and_y(0, 3);
  187. if (random > 0) [[likely]] {
  188.   // body of if statement
  189.   // ...
  190. }
  191. # constexpr virtual functions
  192. struct X1 {
  193.   virtual int f() const = 0;
  194. };
  195. struct X2: public X1 {
  196.   constexpr virtual int f() const { return 2; }
  197. };
  198. # explicit(bool)
  199. struct foo {
  200.   // Specify non-integral types (strings, floats, etc.) require explicit construction.
  201.   template <typename T>
  202.   explicit(!std::is_integral_v<T>) foo(T) {}
  203. };
  204. # enum
  205. enum class rgba_color_channel { red, green, blue, alpha };
  206. std::string_view to_string(rgba_color_channel my_channel) {
  207.   switch (my_channel) {
  208.     using enum rgba_color_channel;
  209.     case red:   return "red";
  210.     case green: return "green";
  211.     case blue:  return "blue";
  212.     case alpha: return "alpha";
  213.   }
  214. }
  215. # constinit
  216. const char* g() { return "dynamic initialization"; }
  217. constexpr const char* f(bool p) { return p ? "constant initializer" : g(); }
  218. constinit const char* c = f(true); // OK
  219. constinit const char* d = f(false); // ERROR: `g` is not constexpr, so `d` cannot be evaluated at compile-time.
  220. # std::span
  221. std::span<const int, 3>{ c.cbegin(), c.cend() }
  222. # Bit operations
  223. std::popcount(0u); // 0
  224. std::popcount(1u); // 1
  225. std::popcount(0b1111'0000u); // 4
  226. # Math constants
  227. std::numbers::pi; // 3.14159...
  228. std::numbers::e; // 2.71828..
  229. # starts_with and ends_with on strings
  230. std::string str = "foobar";
  231. str.starts_with("foo"); // true
  232. str.ends_with("baz"); // false
  233. # std::midpoint
  234. std::midpoint(1, 3); // == 2
  235. # std::to_array
  236. int a[] = {1, 2, 3};
  237. std::to_array(a); // returns `std::array<int, 3>`
复制代码

C++当代教程四_c++ float 转 string-CSDN博客

创作不易,小小的支持一下吧!



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表