c/c++
极客探索者
热衷于探索各种前沿技术,不断挑战自我。在CSDN上分享我的极客之旅,期待与您一同前行。
展开
-
C++ STL
【代码】C++ STL。原创 2024-01-06 14:49:19 · 365 阅读 · 0 评论 -
C++并发编程(持续更新)
【代码】C++并发编程(持续更新)原创 2024-01-04 11:27:40 · 347 阅读 · 0 评论 -
C++模板编程浅析
return 0;T c = a;a = b;b = c;原创 2023-12-14 18:35:07 · 378 阅读 · 0 评论 -
C++静态成员数据
对象,并访问了静态变量和静态函数。由于静态变量是类级别的,因此我们使用类名来访问它。同样地,我们使用类名来调用静态函数。在上面的例子中,我们声明了一个名为。我们还定义了一个普通的成员函数。的类,其中包含一个静态变量。函数中,我们创建了一个。原创 2023-12-13 14:53:38 · 504 阅读 · 2 评论 -
C++拷贝构造函数的参数 必须带有const吗?
在这种情况下,拷贝构造函数的参数是一个非const引用。这种形式可能会导致在拷贝构造函数中修改传入的对象,但这并不是一个好的实践。因为在拷贝构造函数中修改传入的对象可能会导致未定义的行为或意外的副作用。因此,通常推荐使用带有const修饰符的拷贝构造函数参数。在C++中,拷贝构造函数的参数并不必须带有const修饰符。拷贝构造函数用于创建一个新对象,并将其初始化为另一个已存在的对象的副本。这种形式通常更为常见,因为它可以确保在拷贝构造函数中不会修改传入的对象。在这个例子中,拷贝构造函数的参数是。原创 2023-12-12 18:06:49 · 573 阅读 · 0 评论 -
C++面向对象:列表初始化示例(备忘)
【代码】C++面向对象:列表初始化示例(备忘)原创 2023-12-12 17:23:42 · 337 阅读 · 0 评论 -
C++ string类使用示例(备忘录)
【代码】C++ string类使用示例(备忘录)原创 2023-12-12 13:30:57 · 323 阅读 · 0 评论 -
C++动态内存分配:检查分配成功的方法
1、当new操作成功时,返回申请到的堆空间的一个地址。如果不成功,则返回一个空指针。因此,动态空间是否申请成功可以通过检查new操作的返回值来实现。2、也可以利用C++的assert()宏来确定new操作是否成功。当检测到new操作不成功时,直接退出程序。C++使用new进行动态内存分配,可以使用如下两种方法,检查是否分配成功。原创 2023-11-28 17:49:43 · 605 阅读 · 0 评论 -
C++:通过指针遍历数组的多种方式
C++数组名保存的是数组的首地址,也就是说数组名就是一个指针。而且它是一个指针常量,它的值不能变。原创 2023-11-28 17:16:53 · 1368 阅读 · 0 评论 -
C++面向对象复习笔记暨备忘录
C++11引入新的关键字nullptr, 充当单独的空指针常量。调用f(NULL)将会调用f(int* x)如果有重载函数void f(int *x)和void f(int x)那么,f(NULL)将会调用f(int x),这肯定不是程序员的原意。原创 2023-11-28 16:41:47 · 644 阅读 · 1 评论 -
指针与常量限定符const:指向const对象的指针和const指针
【代码】const指针备忘录。原创 2023-11-22 14:07:21 · 342 阅读 · 0 评论 -
C++贪心算法示例
例子改编自翁惠玉C++教材(第三版)贪心算法一节。原创 2023-11-22 10:13:12 · 473 阅读 · 0 评论 -
C++ static_cast使用示例
注意,代码中的5必须有小括号。原创 2023-11-21 15:15:11 · 48 阅读 · 0 评论 -
C++11 变量初始化,auto, typeid, decltype使用示例
【代码】C++11 变量初始化,auto, typeid, decltype使用示例。原创 2023-11-21 13:36:23 · 49 阅读 · 0 评论 -
C++命名空间
命名空间定义#include <iostream>using namespace std;namespace mynsps{ void radios(){ cout << "----radios----" << endl; }}int main() { int i = 10; int j = 20; cout << "Hello, World!" << endl;原创 2022-05-09 13:00:23 · 310 阅读 · 1 评论 -
局部变量及全局变量
#include<iostream>#include<string>using namespace std;int test_i = 0;void foo(){ cout << test_i << endl;}int main(){ int test_i; foo(); test_i = 1; cout << test_i << endl; foo(); ret.原创 2022-01-31 20:47:53 · 355 阅读 · 0 评论 -
C++ this用法
#include <iostream>using namespace std;class Node{ public: void set_x(int x); void set_y(int y); public: int x; int y;};void Node::set_x(int x){ this->x = x;};void Node::set_y(int y){ thi.原创 2021-12-08 09:52:42 · 387 阅读 · 0 评论 -
C++用Vector实现栈的功能小试牛刀
#include <iostream>#include <vector>using namespace std;int main(){ vector<char> stack(0); char tmp; do{ cin>>tmp; if(tmp != '#'){ stack.push_back(tmp); }else{ stac.原创 2021-12-04 18:06:32 · 1404 阅读 · 0 评论 -
C++面向对象之虚函数
#include <iostream>using namespace std;class Human{ public: virtual void buyTicket(){ cout<<"Human buy ticket"<< endl; }};class Teacher: public Human{ public: virtual void buyTicket(){.原创 2021-12-02 10:40:04 · 488 阅读 · 0 评论 -
C++拷贝构造函数应用示例
#include <iostream>#include <string>#include <fstream>#include <assert.h>using namespace std;struct Keyboard{ string m_strModel;};class Computer{ private: Keyboard *m_pKeyboard; string m_strModel;.原创 2021-12-01 15:15:29 · 349 阅读 · 0 评论 -
类型转换构造函数示例
#include <iostream>#include <string>#include <fstream>using namespace std;class Complex{ public: double real, imag; Complex(int i){ cout << "IntConstructor called" << endl; re.原创 2021-11-29 13:19:51 · 306 阅读 · 0 评论 -
复制构造函数被调用的情况示例
#include <iostream>#include <string>#include <fstream>using namespace std;class A{ public: A(){}; A(A &a){ cout << "Copy constructor called" << endl; }};void Func(A a){}i.原创 2021-11-29 10:59:04 · 188 阅读 · 0 评论 -
C++的string类
#include <iostream>#include <string>using namespace std;int main(){ int n(20); cout << n << endl; string a = "dog"; string b =a + ", cat"; cout << b << endl; string c = a + "dog" + "cat";.原创 2021-11-26 16:50:41 · 301 阅读 · 0 评论 -
C++指针与地址
地址表达式加一个整数,C++将按比例增大地址,整数要乘以表达式基类型大小:新地址 = 旧地址 + (整数 * 基类型大小)#include <iostream>using namespace std;int main(){ int n = 9; int *p = &n; cout << p << endl; cout << p+1 << endl; cout << *原创 2021-11-26 15:17:32 · 1701 阅读 · 1 评论 -
C++实现冒泡排序
#include <iostream>using namespace std;void sort(int a[], int n);void swap(int *a, int *b);int main(){ int a[10] = {34,23,49,74,13,24,65,37,3,1}; for(int i = 0;i < 10;i ++){ cout << a[i] << '\t'; } cout &l.原创 2021-11-26 14:07:49 · 192 阅读 · 0 评论 -
C++ 全局变量和局部变量初始化
#include <iostream>using namespace std;char c1[10] = {};int i1;int main(){ char c2[5] = {}; int i2; cout << "c1[0]: " << c1[0] << '\t' << endl; cout << "c2[0]: " << c2[0] << '\t' &l.原创 2021-11-26 13:32:01 · 240 阅读 · 0 评论 -
[c/c++编程与算法练习]分治法找出一个数组中的最大、最小值
#include<iostream>using namespace std;void minmax(int sz[], int n, int *max_num, int *min_num);int main(){ int max_num, min_num; int sz[9] = {1, 6, 3, 5,9}; minmax(sz, 5, &max_num, &min_num); cout << max_num << '\t' <.原创 2021-11-23 10:43:37 · 740 阅读 · 2 评论 -
[c/c++编程与算法练习]动态内存分配-检查new操作的失误
方法1:#include<iostream>using namespace std;int main(){ int *p; p = new int; if (!p){ cout << "allocation failure" << endl; return 1; } *p = 20; cout << *p << endl; delete p; return 0;}方法2:使用assert#incl原创 2021-11-22 18:27:56 · 392 阅读 · 0 评论 -
[c/c++编程与算法练习]函数模板的定义和使用
#include<iostream>using namespace std;template <class T>T my_max(T a, T b){ return a > b?a : b;}int main(){ cout << my_max(3, 5) << endl; cout << my_max(3.3, 3.5) << endl; cout << my_max('d', 'r') &.原创 2021-11-22 15:09:53 · 472 阅读 · 0 评论 -
[c/c++编程与算法练习]strchr的应用
#include<iostream>#include<cstring>using namespace std;int main(){ char str[] = "This is a sample string"; const char *pch; pch = strchr(str, 's'); cout << pch; while (pch!=0){ cout << pch - str + 1 << endl; pch .原创 2021-11-20 19:57:08 · 235 阅读 · 0 评论 -
[c/c++编程与算法练习]c++指针运算
#include<iostream>#include<cstring>using namespace std;int main(){ int i; cout << i << endl; int *p{&i}, *q{0}; cout << *p << endl; //cout << *q << endl; bool b{p}; cout << boolalpha <.原创 2021-11-20 17:02:02 · 209 阅读 · 0 评论 -
[c/c++编程与算法练习]c++指针
#include <iostream>using namespace std;int main(){ double num = 3; double *pNum; pNum = # cout << "pNum在内存中的地址是:"<< &pNum <<endl; cout << "pNum的值是:"<< pNum<< endl; cout << "num在内存中的地址是:.原创 2021-11-17 11:11:08 · 246 阅读 · 0 评论 -
[c/c++编程与算法练习]字符金字塔
要求输出格式: *********代码://字符金字塔#include<bits/stdc++.h>using namespace std;int main(){ char a = '*'; const int H = 3; for(int i = 0; i < H; i++){ int space_len = H - (i+1); int char_len = 1 + i * 2; fo..原创 2021-11-06 18:45:30 · 381 阅读 · 0 评论 -
[c/c++编程与算法练习]枚举法-计算台阶最少阶数
#include<iostream>using namespace std;int main(){ for (int i = 11;;i++){ if (i % 2 == 1 && i % 3 == 2 && i % 5 ==4 && i % 6 == 5 && i % 7 ==0){ cout << i << endl; break; } } return 0;...原创 2021-11-13 15:50:50 · 785 阅读 · 0 评论