从0开始c++
从0开始c++
咸鱼10086
这个作者很懒,什么都没留下…
展开
-
哈希表
哈希表的基础使用注意.second与->second#include <iostream>#include <unordered_map>#include "stdlib.h"#include "stdio.h"#include<typeinfo>using namespace std;int main(){ unordered_map<string,int> Hash_1={{"type",0},{"color",1},{"pri原创 2021-03-04 10:30:30 · 189 阅读 · 1 评论 -
向量与迭代器
#include<iostream>#include<vector>using namespace std;int main(){ vector<int> v1,v2; int a[]={100,99,98,97}; vector<int>::iterator it;//定义vector下的迭代器来代替下标 v1.assign(a,a+4);// a数组里面的4个数都添加到v1中 v2.assign(3,10);//assign(n,u)向量原创 2020-08-24 15:37:29 · 421 阅读 · 0 评论 -
简单字符串,istringstream应用
利用字符串读取数字与算数符号从而实现100+12-66等字符串的加减法#include<iostream>#include<sstream>using namespace std;int main(){ istringstream iss; ostringstream oss; int a; char c1='+',c2; double sum=0.0,val=0.0; string input,output; cout<<"请输入算式"<&原创 2020-08-21 10:02:36 · 72 阅读 · 0 评论 -
局部函数与主函数参数改变
局部函数与主函数参数改变在局部函数直接改变参数不能改变主函数的相应参数,可以通过指针和引用改变而针对指针参数的改变,在局部函数只能改变指针指向的值而不能改变指针的指向,改变指向需要借用指针的引用#include<>using namespace std;void change1(int a){ a=5;} void change2(int &a){ a=5;} iostreamstruct node{ int data; node* next;};vo原创 2020-08-20 12:29:44 · 212 阅读 · 0 评论 -
c++ 链栈的实现
链栈的实现#include<iostream>using namespace std;typedef int elementtype;struct node{ elementtype data; node *next;}; typedef node * nodepointer;bool initstack(nodepointer &S){ S=NULL; return true;}void push(nodepointer &S,elementtyp原创 2020-08-15 10:49:51 · 359 阅读 · 0 评论 -
c++实现分数的读取与加减乘除运算
首先定义了Euclid函数,采用欧几里得算法用来求最大公约数,方便后续用在分数的化简与通分上。定义了fraction类私有数据成员:int nume (分母) int deno (分子)共有函数构造函数三个分别用于整形分数,double小数,文本类型来构造分数复制构造函数一个化简函数 simplify输出函数 display运算符重载±*/采用友元函数在类外定义主函数用于测试#include<iostream>#include<string>#include原创 2020-07-11 00:10:56 · 3045 阅读 · 0 评论 -
静态联编、动态联编与虚函数
静态联编:在编译阶段 就将函数的实现与函数的调用绑定起来动态联编:在程序运行时 将函数的实现与函数的调用绑定起来例如:#include<iostream>using namespace std;class base{ public:print(){cout<<"this is base's print"<<endl; }};//派生类公有继承基类 class derived:public base { public:print(){cout<&原创 2020-07-08 23:38:59 · 278 阅读 · 0 评论 -
c++学习静态数据成员与静态成员函数
此次主要想要学习静态数据成员与静态成员函数静态是以static进行声明其是属于类而不属于对象;即一个类只会对静态分配一个存储空间,无论创建n个对象,静态成员的存储只会有一个,而其他非静态成员会有n个**静态成员必须在类外进行初始化例如我这次忘记定义int student::total=0;系统一直报错ld returned 1 exit status为了实现对静态数据成员的使用,一般采用静态成员函数来操作;**静态成员函数不可访问非静态数据成员没有this指针不能被声明为const#i原创 2020-06-17 23:31:30 · 124 阅读 · 0 评论 -
c++使用动态内存构建的数组顺序栈
栈是一种先进后出的结构可利用数组来构造一个顺序栈首先定义栈的结构包含三个部分 :top值用于指示栈顶元素的上一个内存单位用来存放栈值的数组指向栈结构的指针最先开始编写的代码遇到的问题:初始定义函数的参数没有用指针,导致在主函数内的相关值并未改变。即在initstack调用后,cout<<a.top不为0,而为一个任意值。犯了一个经典错误:全局变量的值在局部函数内无法改变。struct stack{ elementtype top; elementtype st[stack原创 2020-06-15 21:33:17 · 158 阅读 · 0 评论 -
c++实现链表的运算
c++实现链表的运算建立一个描述节点的结构体,该结构体由两部分构成 数据域与指针域。指针指向下一个节点,最后一个指针为空。主要包含的功能有创建链表,销毁链表,输出链表,寻找元素,插入元素,删除元素。采用switch case 分支语句对功能进行控制。#include<iostream>using namespace std;struct node{ int data; node *next;}; typedef node* nodepointer;typedef int原创 2020-06-06 22:06:53 · 186 阅读 · 0 评论 -
c++随机数组生成与排序(冒泡、选择)
#include<iostream>#include<stdlib.h>#include<time.h>using namespace std;int bubble(int A[],int n);int select(int A[],int n); int print(int A[],int n); int main(){ int n=100; srand((unsigned int)time(0));//生成随机数 int A[100]; fo原创 2020-05-25 16:40:11 · 2964 阅读 · 1 评论 -
c++利用指针求字符串数组长度
#include<iostream>using namespace std;int lenth(char A[]); int main(){ char A[100]; cout<<"请输入一串字母(不能含有空格以回车结束):"<<endl; cin>>A; int b; b=lenth(A); cout<<"输入字母长度为"<<b<<"个"<<endl; return 0;} int l原创 2020-05-25 16:28:40 · 4142 阅读 · 0 评论