C/C++
sumup
活到老,学到老。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
实现删除空格
编码实现删除空格。例如“AB C D”变成“ABCD”,要求不能申请相同量级的空间。 int delspace(char * pSrc);//返回删除的空格个数 int delspace(char *pSrc) { int left = 0; int right = 0; char * pStr = pSrc; int count = 0; while (*pStr != '\0') { if (*pStr != ' ') {原创 2021-07-12 22:45:39 · 194 阅读 · 0 评论 -
智能指针使用(1)
智能指针使用 void Check(weak_ptr<int> &wp) { shared_ptr<int> sp = wp.lock(); if (sp != nullptr) { cout<<"still "<<*sp<<endl; } else { cout<<"pointer is invalid."<<endl; } } int main()原创 2020-11-29 16:59:47 · 166 阅读 · 0 评论 -
C++ lambda捕获注意事项
C++ lambda捕获注意事项 示例 int main() { int j {23}; auto byValLambda = [=] { return j + 1;}; auto byRefLambda = [&] { return j + 1;}; cout<<"byValLambda: "<<byValLambda()<<endl; //24 cout<<"byRefLambda: "<<b原创 2020-11-29 13:00:45 · 365 阅读 · 0 评论 -
使用Markdown第一步
记录下MARKDOWN语法学习这是标题二级标题一级标题二级标题三级标题四级标题五标题六级标题下面是分隔符下面是分隔符高级链接注意表格 下载 Typora 参考链接 这是标题 ============================ 二级标题 ------------- # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五标题 ###### 六级标题 这里是文本内容...原创 2020-04-25 19:16:18 · 299 阅读 · 0 评论 -
C++单例模式
#include using namespace std; class Singleton { public: static Singleton& instance(){return s;};//返回一个引用而不是指针,是因为用户可能不小心删除指针 int getValue(){return i;}; void setValue(int x){i=x;}; private:原创 2017-10-02 20:08:14 · 178 阅读 · 0 评论 -
C++11启动线程的多种方式
1、通过函数指针创建线程 #include #include using namespace std; void counter(int id,int numIterations) { for(int i=0;i<numIterations;++i) { cout<<"Counter "<<id<<" has value "<<i<<endl; } } int main() {原创 2017-10-02 15:16:28 · 3554 阅读 · 0 评论 -
C++11多线程原子类型
C++11中10个线程同时竞争资源,会出现竞争问题 处理方式1,mutex #include #include #include #include #include #include #include using namespace std; mutex mut; void func(int &counter) { mut.lock(); for(int i=0;i<1原创 2017-10-01 22:01:59 · 291 阅读 · 0 评论 -
函数指针的使用实例
#include using namespace std; int inc(int a) { return (++a); } int multi(int *a,int *b,int *c) { return (*c=*a**b); } typedef int (*FUNC1)(int in);//typedef简化函数指针的定义 typedef int (*FUNC2原创 2017-10-01 20:34:58 · 976 阅读 · 0 评论 -
C++11四种强制类型转换
1、 static_cast: 功能:完成编译器认可的隐式类型转换。 格式type1 a; type2 b = staic_cast(a);将type1的类型转化为type2的类型; 使用范围: (1)基本数据类型之间的转换,如int->double; int a = 6; double b = static_cast(a); (2)派生体系中向上转型:将派生类指针或引用转化为基转载 2017-10-01 17:50:05 · 1796 阅读 · 0 评论 -
strcpy时如何有效的在堆区申请内存
#include #include #include using namespace std; /* 1.崩溃 void mac(char *p) { char* t; t=(char*)malloc(sizeof(char)*100); p=t; } int main() { char *str=NULL;//str指向地址为空 char array[]="abcdef";原创 2017-10-01 17:05:26 · 585 阅读 · 0 评论 -
嵌套vector的使用
形容vector>怎么初始化: 输入m,n m为vector个数 n每个vector中的元素 #include #include using namespace std; int main() { int m,n; cout<<"m="; cin>>m; cout<<"n="; cin>>n;//m个vector 每个vector有n个元素 vector > ivec;原创 2017-09-10 11:18:20 · 12434 阅读 · 0 评论 -
确保不是在栈区申请内存
//运行Test()会有怎样的结果 #include #include using namespace std; char *GetMemory(void) { char p[] = "hello world";//局部变量,运行后会自动销毁 return p; } void Test(void) { char *str = NULL; str = GetMemory();原创 2017-10-01 16:58:58 · 214 阅读 · 0 评论 -
C++常见面试题(二)strcpy实现
char * strcpy(char * strDest,const char * strSrc) { if ((strDest==NULL)||strSrc==NULL)) return NULL; char * strDestC原创 2017-09-30 11:49:04 · 493 阅读 · 0 评论 -
String的构造,析构,赋值,赋值函数编写
#include #include #include using namespace std; class String { public: String(const char *str);//构造函数 ~String();//析构函数 String(const String &other);//复制函数 String &operator= (const String &原创 2017-09-30 11:21:10 · 390 阅读 · 0 评论 -
构造函数中是否可以调用虚函数
1. 从语法上讲,调用完全没有问题。 2. 但是从效果上看,往往不能达到需要的目的。 Effective 的解释是: 派生类对象构造期间进入基类的构造函数时,对象类型变成了基类类型,而不是派生类类型。 同样,进入基类析构函数时,对象也是基类类型。 所以,虚函数始终仅仅调用基类的虚函数(如果是基类调用虚函数),不能达到多态的效果,所以放在构造函数中是没有意义的,而且往往不能达到本来想要的效原创 2017-10-08 14:46:24 · 7199 阅读 · 1 评论 -
STL容器的底层实现
底层容器实现 http://www.cnblogs.com/hustlijian/p/3611424.html转载 2017-10-07 17:17:28 · 440 阅读 · 0 评论 -
删除vector重复元素
#include #include #include using namespace std; int main() { vector veci; for(int i=0;i<10;++i) { veci.push_back(i); veci.push_back(i); veci.push_back(i+2); } vector::iterator iter原创 2017-10-07 12:06:43 · 435 阅读 · 0 评论 -
vector删除元素
删除指定的元素7 #include #include using namespace std; int main() { vector veci; for(int i=0;i<10;++i) { veci.push_back(i); veci.push_back(i); //veci.push_back(i+2); } vector::iterator i原创 2017-10-06 12:02:21 · 394 阅读 · 0 评论 -
简述malloc/free和new/delete区别
区别: (1)malloc/free是C/C++标准库函数,new/delete是C++运算符。 (2)malloc要手工计算字节数,new自动计算需要分配的空间。 (3)malloc不是类型安全的,new是类型安全的。 如: int *p = new float[2];//编译时出错 int *p = (int*)malloc(2*sizeof(double));//编原创 2017-09-12 20:33:30 · 475 阅读 · 0 评论 -
输入一个大数将其转化为二进制输出
将一个大数转化为2进制输出 #include #include #include #include #include using namespace std; vector turn(string X) { int M=10,N=2; vector vec; int data[1010]; //保存M进制下的各个位数 int ou原创 2017-09-28 20:19:07 · 2474 阅读 · 0 评论 -
32位int和64位int
32位操作系统时 输出 1 2 4 4 4 8 64位操作系统时 输出1 2 4 4 4 8原创 2017-09-28 11:20:46 · 1551 阅读 · 0 评论 -
关于c++空指针0/NULL/nullptr三者的演变
0/NULL/nullptr c语言的NULL,头文件为#include 实际上在C语言中,NULL通常被定义为如下: #define NULL ((void *)0) 因为C++中不能将void *类型的指针隐式转换成其他指针类型,而又为了解决空指针的问题,所以C++中引入0来表示空指针。 我们在C++ 11中使用nullptr来表示空指针。 参原创 2017-09-02 10:35:23 · 1913 阅读 · 0 评论
分享