
C++
文章平均质量分 54
YShun顺
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
C++ string_view
string_view 提供字符串的一个视图。使用 string_view 能够减少字符串的拷贝。类basic_string_view : 模板类std::string_view : std::basic_string_view<char>std::wstring_view : std::basic_string_view<wchar_t>std::u8string_view : std::basic_string_view<char8_t>std::u16原创 2021-08-17 22:50:05 · 2590 阅读 · 0 评论 -
C++ 字符串和数字相互转换
字符串转数字std::strtol & std::strtoll & std::strtoul & std::strtoull函数原型#include <cstdlib>long strtol( const char *str, char **str_end, int base );long long strtoll( const char *str, char **str_end, int base );unsigned long s原创 2021-08-16 23:36:04 · 1796 阅读 · 0 评论 -
STL 中 map 需要注意的地方
对于一个已经存在的 map,要想获取其中的某一组值有两种方法,一种是使用 at() 成员函数,另一种则是使用索引 []。接下来将对使用这两种方式时需要注意的事情进行说明。使用 at() 获取使用 at() 获取 map 中的成员时,会对其键值做范围检测,如果 map 中不存在相应的键值,at() 会抛出异常。因此在使用 at() 获取 map 中的数据时,通常会将这部分代码放到 try ... catch ... 模块中。#include <iostream>#include <s原创 2021-08-16 23:34:55 · 1253 阅读 · 0 评论 -
C++ 中的 static 关键字
原文链接:Static Keyword in C++static 是 C++ 中的关键字,用于为元素赋予特殊的特性。静态元素在程序生存期内仅在静态存储区中分配一次存储。它们一直作用到程序生存期。静态关键字可以与以下内容一起使用,函数中的静态变量静态类对象类中的静态成员变量类中的静态方法1. 函数中的静态变量在函数内部使用静态变量时,仅初始化一次,然后即使通过函数调用,它们也保留上次调用的结果值。这些静态变量存储在静态存储区中,而不是栈中。void counter(){ .原创 2021-08-16 23:34:15 · 2377 阅读 · 3 评论 -
C++ 获取函数名和变量名
C++ 中可以通过宏来获取函数的名称和变量的名称,__func__ 就是函数的名称。获取变量名称可以使用以下自定义宏#define NAME(variable) (#variable)例子:#include <iostream>#define NAME(variable) (#variable)auto mysum(int a, int b){ std::cout << "function name: " << __func__ << st原创 2021-08-16 23:32:21 · 9852 阅读 · 0 评论 -
C++20 designated initializers
说明C++20 引入了 designated initializers 来使用它们的名字来初始化聚合的数据成员。聚合类型可以是数组类型的对象,或者满足以下限制的结构体或类对象:只有 public 数据成员没有用户声明或继承的构造函数没有虚函数没有 virtual, private 或 protected 基类例如,对于一个定义如下的员工结构体struct Employee{ char firstInitial; char lastInitial; int emp原创 2021-08-16 23:31:40 · 1334 阅读 · 0 评论 -
C++ 添加和移除 const 属性
C++ 可以通过 const_cast 添加和移除 const 属性,可以通过 std::as_const 返回一个 reference-to-const 变量,具体例子如下:#include <iostream>#include <string>#include <format>#include <typeinfo>int main(){ const char* name_const{ "Tom" }; std::cout &l原创 2021-08-16 23:30:44 · 8330 阅读 · 0 评论 -
C++ 中的 const 关键字
1. 常量变量const 可用于声明常量变量。常量变量是在初始化后无法更改其值得变量。常量变量必须在声明时进行初始化const int i = 10;2. 指向常量的指针指向常量的指针: 指针指向的内容不能被改变。const int * u; // a pointer point to const intint const * u; // a pointer to an int which is of const type3. 常指针常指针:指针的指向不能被改变。在可以更改值但不能移原创 2021-08-16 23:30:09 · 134 阅读 · 0 评论 -
C++ 函数传入不同个数的参数
#include <iostream>#include <cstdarg>void addSum(int count, ...) { va_list args; va_start(args, count); int sum = 0; for(int i = 0; i < count; ++i) { int x = va_arg(args, int); sum += x; } std::cou原创 2021-08-16 23:27:16 · 871 阅读 · 0 评论 -
C/C++ 位操作
关于位操作的几个术语:设置(set):将数二进制的某一位 置 1清除(clear):将数二进制的某一位 1 清除,变为 0将数字的二进制某一位置 1首先将 1 左移 n 位然后将结果与数字进行或(|)操作#include <iostream>void set(int& num, int n) { num |= 1 << n;}int main() { int num = 2; // 0010 int n = 2; //原创 2021-08-16 23:17:54 · 1014 阅读 · 0 评论 -
C++ 大数乘法字符串实现
#include <vector>#include <string>#include <iostream>#include <algorithm>std::string multiply(const std::string& s1, const std::string& s2) { if (s1.empty() ||...原创 2020-02-21 14:02:50 · 847 阅读 · 0 评论 -
C++ 快速排序
#include <iostream>#include <vector>int partition(std::vector<int>& array, int low, int high) { int pivot = array[high]; // take last element as pivot int i = low - 1...原创 2020-02-20 17:30:35 · 245 阅读 · 0 评论 -
C++ 函数传入不同个数的参量
#include <iostream>#include <cstdarg>void addSum(int count, ...) { va_list args; va_start(args, count); int sum = 0; for(int i = 0; i < count; ++i) { int x = ...原创 2019-12-18 16:42:59 · 295 阅读 · 0 评论 -
C++ 打印 vector
打印 1D vector方法一: 使用基本 for 循环#include <iostream>#include <vector>int main() { // initial a vector std::vector<int> arrays{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for(int i =...原创 2019-12-10 11:30:13 · 27755 阅读 · 6 评论 -
使用 winsock 实现简单的 Client 和 Server
本篇文章将介绍如何使用 winsock 来实现 Client 客户端 和 Server 服务器应用程序。由于 Client 和 Server 的具体实现有所不同,所以本文将分成两部分来对 Client 和 Server 的实现进行讲解。运行环境Windows 10Visual Studio Community 2017 (version 15.9.17)Server 的实现Serve...原创 2019-10-28 22:44:37 · 1438 阅读 · 0 评论 -
C++11 智能指针
本文内容源自 C++11 智能指针原作者:Babu_Abdulsalam 本文翻译自 CodeProject,转载请注明出处。引入Ooops. 尽管有另外一篇文章说 C++11 里的智能指针了。近来,我听到许多人谈论 C++ 新标准,就是所谓的 C++0x/C++11。 我研究了一下 C++11 的一些语言特性,发现确实它确实有一些巨大的改变。我将重点关注 C++11 的智能指针部分。...转载 2019-03-07 16:56:21 · 1130 阅读 · 0 评论 -
C++中 new/delete 和 malloc/free 的区别
以下内容源自 What is the difference between new/delete and malloc/free?What is the difference between new/delete and malloc/free?new/deleteAllocate/release memoryMemory allocated from ‘Free Store’Ret...转载 2019-03-07 15:04:38 · 552 阅读 · 0 评论 -
C++字符串分割
#include <iostream>#include <string>#include <vector>//字符串分割函数std::vector<std::string> split(std::string str, std::string pattern){ std::string::size_type pos; std::vector<std::string> resu原创 2017-06-30 18:02:03 · 278 阅读 · 0 评论