c/c++笔试题
屁小猪
生于忧患,死于安乐
展开
-
笔试题 --String实现类中默认函数
string:1.构造函数2.析构函数3.拷贝构造函数4.赋值操作符重载函数5.取地址操作符重载函数6.默认重载取地址运算符const函数7.默认移动构造函数(C++11);8.默认重载移动赋值操作符函数(C++11)。#include <iostream>#include <string.h> using namespace std;/*string:1.构造函数2.析构函数3.拷贝构造函数4.赋值操作符重载函数5.取地址操作符重载函数6.默认原创 2021-11-02 10:36:21 · 391 阅读 · 0 评论 -
笔试题 -- 类中默认函数
1.构造函数(可以多个,不能被对象调用)2.析构函数(只有一个,可以被对象调用,会出现内存泄漏)3.拷贝构造函数(一个类对象,赋值给另一个未存在对象)4.赋值函数(一个类对象,赋值给另一个已存在对象)https://blog.csdn.net/wu___hao/article/details/108108546...转载 2021-11-01 10:29:14 · 87 阅读 · 0 评论 -
笔试题 --虚函数表
虚函数表理解:http://c.biancheng.net/view/267.htmlhttps://blog.csdn.net/haoel/article/details/1948051/转载 2021-10-17 20:13:39 · 122 阅读 · 0 评论 -
笔试题 -- << 运算符号重载必须为友元函数或普通函数
/*1. << 运算符号重载<< 重载必须为友元函数(friend)<<有两个参数,一个是输出流对象(我们常用的cout,不为类成员函数),还有就是要输出的东西。例如:cout<<"haha";也就是说<<的第一个参数必须是输出流对象。在成员函数里实现<<重载,我们知道this会作为第一个参数,而这是不符合要求的。*/#include <iostream>using namespace std;原创 2021-10-17 16:21:17 · 665 阅读 · 0 评论 -
笔试题 -- 虚析构函数作用防止内存泄漏(在继承关系上使用)
#include<iostream>using namespace std;class base{public: base() { cout << "base" << endl; } virtual ~base() { cout << "virtual ~base" << endl; } virtual void display(){cout<<"I'm base class!"&l原创 2021-10-15 10:29:04 · 111 阅读 · 0 评论 -
笔试题 -- 函数模板
函数模板: 1 #include<iostream> 2 using namespace std; 3 4 //函数模板声明 5 template<class T,class T2> 6 void output(T x,T2 y); 7 //主函数定义 8 void main() 9 {10 double a = 7.09;11 int b=5;12 output(a,b);13 output(b,a);14 sy原创 2021-10-15 09:32:27 · 82 阅读 · 0 评论 -
笔试题 -- 拷贝构造函数 深拷贝和浅拷贝
C++拷贝构造函数(深拷贝,浅拷贝)对于普通类型的对象来说,它们之间的复制是很简单的,例如:int a=88;int b=a;而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量。下面看一个类对象拷贝的简单例子。#include using namespace std;class CExample {private: int a;public: CExample(int b) { a=b;} void Show () {cout<<a<<转载 2021-10-15 09:07:02 · 215 阅读 · 0 评论 -
笔试题 -- 重载、重写(覆盖)、隐藏
#include <stdio.h>#include <string>#include <iostream>using namespace std;class A{public: //重载 int getval() { return 1; } int getval(int a) { return a; } virtual show() { cout <&l原创 2021-10-14 09:59:54 · 97 阅读 · 0 评论 -
笔试题 -- vector内存空间释放及vector数据清空
#include <stdio.h>#include <vector>#include <string>#include <iostream>using namespace std;int main(){ vector<string> vecStr; cout << "vecStr.size() = " << vecStr.size() << endl; cout << "ve原创 2021-10-14 08:53:21 · 259 阅读 · 0 评论 -
笔试题 -- 虚析构函数作用
虚析构函数作用:防止内存泄漏eg1:内存泄漏#ifndef A_H#define A_H#include <iostream>using namespace std;class A{public: A(); ~A();};#endif A_H``````cpp#ifndef B_H#define B_H#include "A.h"using namespace std;class B:public A{public: B(); ~B();};原创 2021-10-13 13:25:34 · 83 阅读 · 0 评论 -
笔试题 -- mymemmove
#include <stdio.h>#include <string.h>void *mymemmove(void *dest,void *src,int count){ char *tmpdest = dest; char *tmpsrc = src; //高地址拷贝到低地址 if(tmpdest <= tmpsrc) { while(count--) { *tmpdest++ = *tmpsrc++; } return des原创 2021-10-13 00:37:45 · 60 阅读 · 0 评论 -
笔试题 --mymemcpy
#include <stdio.h>#include <string.h>void *mymemcpy(void *dest,void *src,int count){ char *tmpdest = dest; char *tmpsrc = src; while(count--) { *tmpdest++ = *tmpsrc++; } return dest;}int main(){ char dest[10] = {"12"}; char sr原创 2021-10-12 23:37:38 · 113 阅读 · 0 评论 -
笔试题 -- mymemset实现
#include <stdio.h>#include <string.h>void *mymemset(void *s,int a,int count){ char *xs = s; while(count--) *xs++ = a; return s;}int main(){ char buf[10] = {"abcdefg"}; //char *buf="12345678"; mymemset(buf,'a',5); printf(原创 2021-10-12 17:44:19 · 105 阅读 · 0 评论 -
笔试题 -- mystrrchr 字符串逆序查找字符
#include <stdio.h>#include <string.h>/* 逆序查找字符串中字符*/char *mystrrchr(char *s,char c){ char *p = s + strlen(s); do{ if(*p == c) return p; }while(--p >= s); return NULL;}int main(){ char *src = "123456"; char a = '原创 2021-10-12 15:59:25 · 137 阅读 · 0 评论 -
笔试题 -- mystrchr 获取当前字符串中字符所在位置
#include <stdio.h>/* 从字符串中获取字符所在位置*/char *mystrchr(char *src,char c){ // while(*src != '\0') // { // if(*src == c) // { // return src; // } // src++; // } //return NULL; for(;*src !='\0';src++) if(*src == c) return src;原创 2021-10-12 14:35:16 · 94 阅读 · 0 评论 -
笔试题 -- strncmp
#include <stdio.h>int mystrncmp(char *str1, char *str2,int count){ while(count--) { if(*str1 != *str2) return *str1 < *str2 ? -1:1; if(*str1 == '\0') return 0; str1++; str2++; } return 0;}int main(){ char *str1="12原创 2021-10-12 11:23:58 · 68 阅读 · 0 评论 -
笔试题 -- mystrcmp
#include <stdio.h>int mystrcmp(char *str1, char *str2){ while(1) { if(*str1 != *str2) return *str1 < *str2 ? -1:1; if(*str1 == '\0') return 0; str1++; str2++; }}int main(){ char *str1="123"; char *str2="1234"; i原创 2021-10-12 09:17:11 · 89 阅读 · 0 评论 -
笔试题 -- strcat实现
#include <stdio.h>#include <string.h>char *mystrcat(char *dest, char *src){ char *tmp = dest; /* while(*dest++ != '\0')//本实例中dest偏移4 等价于==> *dest!='\0' dest++ */ while(*dest != '\0')//本实例中dest偏移3 { dest++;原创 2021-10-12 08:44:54 · 71 阅读 · 0 评论 -
笔试题 -- strncpy实现
#include <stdio.h>#include <string.h>char *myStrnCpy(char *dest,char *src,int count){ char *tmp = dest; while(count--) { if(*src != '\0') { *dest++ = *src++; } else { *dest++ = '\0'; } } return tmp;}int main(){原创 2021-10-09 17:17:14 · 93 阅读 · 0 评论 -
笔试题 -- strcpy实现
#include <stdio.h>void strcpy1(char *des, char *src){ while((*des++ = *src++) != '\0');}int main(){ char *des; char *src = "123456"; strcpy1(des, src); printf("des = %s\n",des); return 0;}原创 2021-10-09 13:54:05 · 150 阅读 · 0 评论 -
笔试题 -- atoi实现
#include <stdio.h>int strtoi(char *str){ int i = 0; while(*str !='\0') //*str != '\0'; str = str + 1; { i = *str - '0' + i * 10; str++; } return i;}int main(){ char *str = "1234"; int a = strtoi(str); printf("a = %d\n",a);原创 2021-10-09 13:29:53 · 63 阅读 · 0 评论 -
笔试题 -- 将整数转字符串
各位可能在网上看到用以下函数可以将整数转换为字符串:itoa(); //将整型值转换为字符串ultoa(); // 将无符号长整型值转换为字符串请注意,上述函数与ANSI标准是不兼容的,很多编译器根本不提供这几个函数,本文就不介绍了,没什么意义。将整数转换为字符串而且能与ANSI标准兼容的方法是使用sprintf()和snprintf()函数,在实际开发中,我们也是这么做的。1、把整数/长整数格式化输出到字符串标准的C语言提供了atoi和atol函数把字符串转换为整数和长整数,但是没有提供把整数转载 2021-10-09 11:42:24 · 148 阅读 · 0 评论 -
笔试题 -- 字符串处理
编程实现,将一个字符串中的首尾空格全部删除,中间的空格只保留一个,例如:“ I LOVE MY HOME ”删除后的结果为“I LOVE MY HOME”#include <stdio.h>#include <string.h>void fun(char *s1,char *s2){ while(*s1 == ' ') { s1++; } printf("*s1 = %c\n",*s1); while(*s2 = *s1++) {原创 2021-10-09 10:13:02 · 129 阅读 · 0 评论 -
笔试题 -- linux内核字符串处理源码
/* * linux/lib/string.c * * Copyright (C) 1991, 1992 Linus Torvalds *//* * stupid library routines.. The optimized versions should generally be found * as inline code in <asm-xx/string.h> * * These are buggy as well.. * * * Fri Jun 25原创 2021-10-08 16:14:34 · 117 阅读 · 0 评论 -
虚函数与纯虚函数
虚函数和纯虚函数可以定义在同一个类(class)中,含有纯虚函数的类被称为抽象类(abstract class),而只含有虚函数的类(class)不能被称为抽象类(abstract class)。虚函数可以被直接使用,也可以被子类(sub class)重载以后以多态的形式调用,而纯虚函数必须在子类(sub class)中实现该函数才可以使用,因为纯虚函数在基类(base class)只有声明而没有定义。虚函数和纯虚函数都可以在子类(sub class)中被重载,以多态的形式被调用。虚.原创 2021-09-23 17:22:37 · 1575 阅读 · 0 评论 -
笔试题 -- 杨辉三角
#include<stdio.h>/*杨辉三角 1 1 1 1 2 1 1 3 3 11.首项为1,尾项为12.其余的数为正上方和左边数之和;*/int main(){ //画杨辉三角 int i = 0,j = 0, n = 10; int a[10][10] = {0}; for(int i = 0; i < n; i++) { //首项为1,尾项为1 a[i][0] = 1; a[i][i] = 1; for(原创 2021-09-23 14:17:11 · 93 阅读 · 0 评论 -
笔试题 --父类指针保存子类对象
#include <iostream> using namespace std; class A { public: virtual void foo() { cout << "A's foo()" << endl; bar(); } virtual void bar() { cout << "A's bar()" << endl;原创 2021-09-17 14:25:11 · 139 阅读 · 0 评论 -
笔试题(快速排序)
// Quick_Sort.cpp : Defines the entry point for the application.// 快速排序算法//1.基准数//2.左边界和右边界数组下标//3.基准数归位//4.递归思想#include<iostream>using namespace std; //快速排序算法(从小到大)//arr:需要排序的数组,begin:需要排序的区间左边界,end:需要排序的区间的右边界void quickSort(int *arr,int原创 2021-09-09 14:38:03 · 174 阅读 · 0 评论 -
笔试题 斐波那契数列(递归-树状结构)
斐波那契数列(递归-树状结构)/*f(0) = 0f(1) = 1f(2) = 1;f(3) = f(1)+ f(2);n>=2f(n) = f(n-1) + f(n-2)*/int f(int n){ if(n >= 2) { return f(n-1) + f(n-2); } else { if(n == 1) { return 1; }else if(n == 0) { return 0; } }}int main(原创 2021-09-07 16:17:51 · 447 阅读 · 0 评论 -
笔试题-简单选择排序
#include <stdio.h>/*0.简单选择排序1.总共数据N1.趟数 N-12.比较次数N-13.K最小值下标*/void simpleSelectionSort(){ int a[] = {2,1,3,5,4,0}; int N = sizeof(a) / sizeof(int); int k = 0;//最小值下标 //趟数 for(int i = 0; i < N-1;i++) { k = i; //比较次数 for(int j原创 2021-08-23 16:43:19 · 105 阅读 · 0 评论 -
笔试题-冒泡排序
#include <stdio.h>/*1.冒泡排序:数据N个2.轮询次数:N - 13.比较次数:N-1-i4.交换数据*/void bubbleSort(){ int a[5] = {1,3,2,5,4}; int N = sizeof(a) / sizeof(int); for(int i= 0; i < N -1;i++) for(int j = 0; j < N -1 -i; j++) if(a[j] > a[j+1]) {原创 2021-08-20 16:08:55 · 154 阅读 · 0 评论 -
笔试题-二分法
#include <stdio.h>/*1.有序数据2.定义下标 min、max、mid*/int findIndex(int value){ int a[5] = {0,1,2,3,4}; int min = 0; int max = sizeof(a) / sizeof(int) -1; int mid = (min + max) / 2; int k = value; while(min <= max) { if(k < a[mid])原创 2021-08-20 15:35:24 · 160 阅读 · 0 评论