- 博客(29)
- 问答 (1)
- 收藏
- 关注
原创 //排序案例//按照年龄进行升序,年龄相同按照身高降序
///排序案例//按照年龄进行升序,年龄相同按照身高降序#include<iostream>#include<string>#include<list>using namespace std;class person {public: int age; string name; int height; person(string name, int age, int height) { this->name.assig...
2022-05-05 09:44:15
502
原创 //reverse反转list中元素//sort排序algorithrm//所有不支持随机访问迭代器不能用标准算法,但是内部会提供相应算法
//reverse反转list中元素//sort排序algorithrm//所有不支持随机访问迭代器不能用标准算法,但是内部会提供相应算法#include<iostream>#include<list>#include<time.h>using namespace std;void print(const list<int>& h) { for (list<int>::const_iterator it = h.beg...
2022-05-05 08:44:53
160
原创 //list容器链表
//list容器链表//节点,数据域,指针域//对任意位置快速插入和删除//遍历元素没有vector快//占用内存空间更大//双向循环链表,prev指向下一个结点#include<iostream>#include<list>#include<algorithm>using namespace std;void printlist(const list<int>&L){for(list<int>::const_i...
2022-05-05 08:23:03
189
原创 全局对象静态对象局部对象
#include<iostream>#include<time.h>using namespace std;class teacheraa {public: int age; teacheraa(int age) { this->age = age; cout << "全局对象构造" << endl; } ~teacheraa() { cout << "全...
2022-04-28 09:06:25
312
原创 获得0~100内的随机数
#include<iostream>#include<time.h>using namespace std;int main() { time_t ti = time(NULL); srand(ti); for(int i=0;i<10;i++){ int a = 0; a = rand() % 101;//获得0~100内的随机数 cout << a << endl;...
2022-04-25 10:30:16
1218
原创 //deque容器//双端数组
//deque容器//双端数组//vector开辟一段连续线性空间,访问元素比deque快//deque中控器记录缓冲区内存地址,迭代器支持随机访问#include<iostream>#include<deque>#include<algorithm>using namespace std;void prinfdeque(const deque<int> d){ for(deque<int>::const_iterator ...
2022-04-24 21:56:42
106
原创 //选择排序//冒泡排序
#include<iostream>#include<vector>#include<algorithm>using namespace std;//选择排序void SelectionSort(vector<int> & arr){ int tem=0; int min_value; int min_pos; for(int i=0;i<arr.size()-1;i++){ min_v...
2022-04-21 10:45:12
183
原创 //类是一个特殊名空间//{}是匿名空间 //静态变量全局区,局部变量栈区,申请空间堆区
//类是一个特殊名空间//{}是匿名空间//静态变量全局区,局部变量栈区,申请空间堆区#include<iostream>#include<vector>#include<algorithm>using namespace std;int ageo = 5;class zs {public: static int age ; class ls { public: int ageo = 4; vo...
2022-04-21 09:50:04
220
原创 通过reserve(int len)预留空间减少vector容器动态开辟的次数,reserve(int len)预留空间不可访问,不初始化
#include<iostream>#include<vector>#include<algorithm>using namespace std;int main(){vector<int> v;int *p=NULL; int num=0;v.reserve(10000);for(int i=0;i<10000;i++){ v.push_back(i); if(p!=&v[0]){num++; ...
2022-04-20 18:05:06
187
原创 //clear()删除所有元素//resize()//重新指定容器长度,其余补0或自行指定,超出则删除 //互换容器swap()
//front()访问容器中第一个元素,back()访问最后一个元素//迭代器begin()指向第一个元素,end()指向最后一个元素的下一个//迭代器rend()指向第一个元素的前一个,rbegin()指向最后一个元素//push_back尾插,pop_back()删除最后一个元素,insert()插入,erase()删除//clear()删除所有元素//三种赋值操作 ,重载等号,assign函数//四种构造方法//resize()//重新指定容器长度,其余补0或自行指定,超出则删...
2022-04-20 17:50:18
242
原创 vector 容器也称为单端数组
//vector 容器也称为单端数组,可以动态扩展//动态扩展:找更大的内存空间,拷贝原数据,释放源空间//迭代器支持随机访问,可以+3,+4//front()访问容器中第一个元素,back()访问最后一个元素//迭代器begin()指向第一个元素,end()指向最后一个元素的下一个//迭代器rend()指向第一个元素的前一个,rbegin()指向最后一个元素//push_back尾插,insert()插入,erase()删除//三种赋值操作 ,重载等号,assign函数//四种构...
2022-04-20 10:00:36
161
原创 queue容器
//queue容器//队列先进先出,类似排队//只有队头和队尾能被访问,不能有遍历//push入队,pop出队//front队头,back队尾//empty判断队列是否为空,size判断queue中元素个数#include<string>#include<queue>#include<iostream>using namespace std;class person{public: int age; string name; ...
2022-04-18 22:41:11
135
原创 stack栈容器
//stack栈容器//先进后出//栈 不允许有遍历行为,只有栈顶元素可被访问//push入栈,pop出栈,top返回栈顶元素//empty判断容器是否为空//size返回stack中元素的个数#include<iostream>#include<stack>using namespace std;int main(){stack<int>s;s.push(10);s.push(20);s.push(30);cout<<s...
2022-04-18 22:08:27
173
原创 字符串比较compare函数 字符串插入insert和删除 erase
#include<iostream>#include<string>using namespace std;int main(){string s1("hello"),s2("hello");s1+='c';s2.append("ccccccc",2,2);//字符串比较compare函数if(s1.compare(s2)==0){ cout<<"a=b"<<endl;}if(s1.compare(s2)==1){ cou...
2022-04-18 09:39:25
234
原创 string字符串的赋值和拼接,append拼接函数,assign赋值函数
//string 本质上是一个类,类内维护一个char*//不用担心复制越界和取值越界#include<iostream>#include<string>using namespace std;int main(){ //字符串构造方式 string str1; const char* s="hello"; string str2(s); string str3(str2); string str4(10,'a'); ...
2022-04-18 09:04:54
429
原创 vector容器嵌套vector容器 ,相当于二维数组
#include<iostream>//vector容器嵌套vector容器 ,相当于二维数组#include<vector>#include<string>#include<algorithm>using namespace std;int main() { vector<int> v1, v2, v3; //相当于二维第一二 三行 vector<vector<int> > v;...
2022-04-17 22:14:01
907
1
原创 vector容器存入自定义类型数据的地址
#include<iostream>#include<vector>#include<string>#include<algorithm>using namespace std;class person {public: int age; string name; person(string name, int age) { this->name = name; this->age...
2022-04-17 21:31:38
312
原创 for_each(pbegin, pend, work);#include<algorithm>
include<iostream>//面向对象三个属性 封装继承多态using namespace std;#include<algorithm>#include<vector>//vector容器的头文件,可以理解成一个数组类void work(int a) { cout << a << endl;}int main() { vector<int>v; v.push_back(12); v...
2022-04-14 11:59:22
219
原创 C++自定义数组
#pragma once#include<iostream>using namespace std;template<class T>class myarray{public: myarray<T>& operator=(const myarray& arr); T& operator[](int a) { return this->address[a]; } T* address;...
2022-04-14 10:25:44
1424
原创 typeid(T1).name()告诉模板T的数据类型
#include<iostream>#include<string>using namespace std;//全局函数的类内实现与类外实现template<typename T1, typename T2>class person;template<typename T1, typename T2>void showperson2(person<T1, T2> p) { cout << "全局函数做友元类外实现"...
2022-04-13 09:24:51
637
原创 C2676 二进制“==”:“T”不定义该运算符或到预定义运算符可接收的类型的转换
原码#include<iostream>using namespace std;class person {public: string name; int age; person(int age, string name) { this->age = age; this->name = name; } };template<typename T>void my_compare(T&a...
2022-04-12 15:14:35
2726
原创 排序算法与函数模板的应用
#include<iostream>using namespace std;template <typename T >//也可以写成classvoid myswa(T& a, T& b);template <typename T >void Arrange_squence(T arr[], int len);template <typename T >void Myprint(T& a, int len);int m
2022-04-11 09:49:12
884
原创 c++二进制读写文件
#include<iostream>#include<fstream>#include<string>class person {public: int m_age; char m_name[1024] = { 0 }; person(char* name, const int age) { strcpy_s(m_name, 50, name); m_age = age; } person...
2022-04-07 11:12:16
967
原创 c++读写文件
#define _CRT_SECURE_NO_WARNINGS 1#include<iostream>//文件流头文件#include<fstream>//ofstream==output stream 输出到文件流,写文件//ifstream==input stream 从文件流获取,读文件//fstream 读写文件#include<string> using namespace std;int main(){...
2022-04-07 09:45:37
467
原创 运算符重载
#include<iostream>#include<string.h>using namespace std;//赋值运算符重载,当数据在堆区,编译器提供的浅拷贝逐字节拷贝而可能会导致堆区数据重复释放,代码崩溃class person{private: friend ostream& operator<<(ostream& cout, const person& p); int* p_age;public:...
2022-03-29 14:34:40
287
空空如也
吴彦祖刘亦菲们,malloc函数为什么返回NULL指针
2022-04-10
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅