C/C++
。
he626shidizai
这个作者很懒,什么都没留下…
展开
-
c++常量引用
#include <iostream>using namespace std;void A( int& a) { printf("%d", a);}int main() { A(1);}上面这段程序会报错,加上const’就对了#include <iostream>using namespace std;void A(const int& a) { printf("%d", a);}int main() { A(1);}原创 2021-11-18 15:28:18 · 424 阅读 · 0 评论 -
shared_ptr实现
// 智能指针template<class T>class SharedPtr {private: T* _ptr; int* _count;public: SharedPtr(T* ptr = nullptr) : _ptr(ptr), _count(new int(1)) {} SharedPtr(const SharedPtr& s) : _ptr(s._ptr), _count(s._count) { *count++; } ~SharedPtr()原创 2021-06-09 22:48:24 · 106 阅读 · 0 评论 -
关于c++友元类的思考
如果A想要访问B的函数或者成员变量,那么不能在A中声明B为友元类,如果这个可以成立的话,岂不是随便写一个类就可以访问类的私有成员?所以A要访问B的私有成员,只能在B中声明A为友元类。...原创 2021-05-28 19:29:45 · 88 阅读 · 0 评论 -
第一次用简单的智能指针
c++项目写多了,特别是各种类里面的指针各种指向,才意识到智能指针的好处,可以不需要自己焦头烂额写析构的部分。今天终于小试了一下,用shared_ptr写了一个小链表~#include <memory>#include <iostream>#include <string>using namespace std;struct Node{ int num; shared_ptr<Node> next; Node(int _num =原创 2021-02-21 20:51:21 · 91 阅读 · 0 评论 -
c++ vector debug遍历方式效率对比
#include <vector>#include <iostream>#include <time.h>using namespace std;int main(){ vector<int> v(100000000); time_t begin, end; begin = clock(); for (int i = 0; i < v.size(); ++i) { v[i] = 10; } end =原创 2020-10-04 16:32:49 · 224 阅读 · 0 评论 -
error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token| 记录贴
学会C语言也挺长时间了,当时写的时候就记得经常出现这个错误。在网络上查找感觉原因也林林总总。有时候也挺头疼的,就想着在这里记录一下原因:从字面上看是语法上的错误,缺少一些必要的符号。2019-4-26在文件中找了很久,没有找到语法错误,后来才发现是头文件一个函数的声明没有以 ; 结尾。此外struct 的结尾也要记得加 ;...原创 2019-04-26 10:32:51 · 29941 阅读 · 6 评论 -
expected expression before ''结构体" c
#include<stdio.h>#include<stdlib.h>typedef struct{ int i; char c;}Node;int main(){ Node *n= (Node *)malloc(sizeof Node); return 0;}执行这样一段代码时出席拿了错误:E:\11243\Doc...原创 2019-03-15 10:11:42 · 14790 阅读 · 0 评论 -
C 语言程序计时
来源:姥姥的数据结构课程我们测试一个函数的运行时间时,就需要用到下面这套模板。基本程序#include<stdio.h>#include<time.h>clock_t start, stop;double duration;//记录被测函数运行时间,以s为单位int main(){ start = clock(); //开始计时,返回从程序...原创 2019-03-09 14:02:34 · 1608 阅读 · 0 评论 -
C通过结构体给整个数组赋值
参考:C专家编程我们都知道在C语言中,数组A = 数组B的语法是错误的。那么怎么整个给数组赋值呢,我们可以借助结构体给整个数组赋值#include<stdio.h>struct s_tag{ int a[100];};struct s_tag orange, lime, lemon;struct s_tag twofold(struct s_tag s){...原创 2019-03-08 20:31:08 · 6394 阅读 · 3 评论 -
编写一个程序,把C语言的声明翻译成通俗语言--C专家编程
最近在看C专家编程,有一个编程挑战:编写一个程序,把C语言的声明翻译成通俗语言。很像编译原理中的词法分析部分与语法制导的部分。上个学期虽然自己也敲了编译器的各个环节但是自己写的C语言和书上的相比真的略显稚嫩。书上提供的代码真的很完美的感觉,记录在这里以后也方便品读。。。#include<stdio.h>#include<string.h>#include<cty...原创 2019-02-12 17:25:06 · 777 阅读 · 1 评论 -
c++ 整数转化为string
需要把整数转化为string类型,一共找到三种方法int k;char arr[10];string s;1.因为我是用的vs2010,这种方法不好用#include&amp;lt;string&amp;gt;s = std::to_string(k);2.#include&amp;lt;stdlib.h&amp;gt;itoa(k,arr,10);s = arr;3.#include&amp原创 2019-01-25 14:16:09 · 3423 阅读 · 0 评论