- 博客(165)
- 收藏
- 关注
原创 知识点5-6
std::vector::at 和 std::vector::operator[] operator[] 主要是为了与c语言兼容,他可以向c语言数组一样操作,但是at()其实是首选,因为他会进行边界检查,如果超过vector范围,将会抛出一个例外。操作符++p,–p右边一定是变量int main() { int m = 5; cout << --(m+1) << endl;
2016-07-04 08:12:56
739
原创 Class and Object
一个类可以定义自己类型的对象吗?类中定义静态变量class base { static base base1;};int main() { return 0;} 定义静态类没有问题,因为他们是共享的类中定义指针class base { base *base1;};int main() { return 0;} 这个是没问题的啊,可以想到做的
2016-06-10 10:09:40
763
原创 知识点3-4
class和struct区别?C的struct与C++的class的区别 C是一种过程化的语言,struct只是作为一种复杂数据类型定义,struct中只能定义成员变量,不能定义成员函数。C++中的struct和class的区别 struct中成员默认是public,而class中默认是私有的#include <stdio.h>class Test { int x; // x is
2016-06-06 10:21:27
532
原创 local class and nested class
local class 在一个函数里面声明的类从属于那个函数。比如说void fun() { class base { private: public: };}local class 仅仅能够在函数内部声明对象void fun() { class base { private: int i; public:
2016-06-04 22:35:13
733
原创 八大排序算法
冒泡排序void bubble_sort(vector<int> &nums){ for (int i = 0; i < nums.size() - 1; i++) { // times for (int j = 0; j< nums.size() - i- 1; j++) { if (nums[j] > nums[j + 1]) {
2016-06-03 23:58:55
502
原创 临时对象的生成
强制类型的转换class CRectangle {public: CRectangle(CRectangle &to) { cout << "***" << endl; w = to.w; h = to.h; printTotal(); } CRectangle(int i = 1, int j = 2); ~CRec
2016-05-26 22:57:03
1107
原创 友元
友元函数为类class A{private: int a;public: A() { a = 55; } friend class B;};class B{private: int b;public: void showA(A &x) { cout << x.a << endl; }};int main() { A
2016-05-20 17:06:11
528
原创 知识点1-2
本博客长期更新1、int a = 0 and int a(0) 的区别 没有区别 参考两个网站 SO1,SO22、new/delete和malloc/free的区别 相同点:均可用于申请动态内存和释放内存 不同点: (1)操作对象不同 malloc/free是c++标准库函数,而new/delete是c++的运算符。对于非内部数据类的对象而言,仅仅使用malloc和free不能
2016-04-18 09:04:29
474
原创 qsort函数与sort函数
qsort函数 qsort函数是基于快速排序的方法。 void qsort(void *base,size_t num,size_t width,int (_cdecl *compare)int compare(const void *a,const void *b)) 第一个参数,base是需要排序的目标数组名。 第二个参数,num是参与排序数目个数 第三个参数,width
2016-04-15 19:17:18
660
原创 多态和虚函数
多态与虚函数 在类的定义中,前面有virtual定义的即为虚函数 virtual关键字只用在定义类的函数声明时,写具体函数时不用。class Base{private:public: virtual int get(){}};int base::get() {} 对于派生类的对象可以赋值给基类,或者基类可以引用派生类。 如果基类指针或者引用指向基类对象,那么调用基类
2016-04-14 15:47:44
799
原创 Static Members:
Static Members:c++中一些静态成员函数的有趣的事实静态成员函数没有this指针class Test{private:public: static Test *fun() { return *this;//compiler error }};静态成员函数不能是虚函数成员函数名相同且参数相同,如果其中一个是静态成员函数,那么他们就不能重载c
2016-04-13 09:49:30
733
原创 继承与派生
基本概念 在定义一个新的类B时,如果该类与某个已有的类A的特点相似,及B拥有A的全部特点,那么久可以把A作为一个基类,而把B作为一个派生类(子 类)派生类写法: class A:public B{ …….. ;}class CStudent{private: string sName; int nAge;public: bool i
2016-04-11 19:09:18
577
原创 运算符重载
对已有的运算符赋予多重的含义 扩展c++中提供的运算符的使用范围,以用于不同的类型运算符重载基本概念运算符重载为普通函数 class Complex{ public: double real;double imag; Complex(double r=0.0,double j=0.0){ real=r;imag=j; } protect
2016-04-05 21:51:07
469
原创 封闭类、友元、this指针、常量对象
成员对象和封闭类 成员对象:一个类的成员变量是另一个类的对象 封闭类:包含成员对象的类是封闭类class CTyre{public: CTyre(int a,int b):radius(a),width(b){}protected:private: int radius; int width;};class CEngine{public:protect
2016-04-04 12:50:30
882
原创 静态成员和静态成员函数
静态成员和静态成员函数基本概念 普通成员变量每个对象有各自的一份,而静态成员变量一共就一份,为所有对象共享。 静态成员对象不需要通过对象就能访问class CRectangle{public: CRectangle(int i,int j); ~CRectangle(); static void printTotal();protected:private
2016-03-31 16:36:00
474
原创 构造、解析函数
构造函数 构造函数是成员函数的一种,名字与类名相同,不能有返回值(void也不行) 左用是对对象初始化,比如给成员变量赋初值 类可以有多个构造函数没有初始化对象class Complex{public: void set(double r,double i);protected:private: double real,imag;};Complex c1;
2016-03-18 10:47:22
1297
原创 C语言指针
准备复习C指针,本博客长期更新地址和内容概念int main(){ int i=3; int *pointer; pointer=&i; printf("%d",*pointer); return 0;} 输出: 3int main(){ int i=3; int *pointer; pointer=&i; prin
2016-03-17 10:31:11
633
原创 STL之哈希表
unordered_map、unordered_set用法unordered_map列举出几个主要的unordered_map的函数,并用程序列子说明:1、unordered_map::count#include "iostream"#include "vector"#include "string"#include "queue"#include "algorithm"#include
2016-03-14 21:41:35
714
原创 STL之priority_queue
priority_queue用法priority_queue在STL中模拟实现先写一个用STL模板实现的与真正STL的priority_queue用法相似的priority_queue,以加深理解。#include "iostream"#include "vector"#include "string"#include "queue"#include "algorith
2016-03-11 09:35:43
457
原创 类和对象、内联函数、重载
1.C++类和对象类与结构体一样,有相似之处,从今天开始学习。加油。1.1类的声明与对象的创建声明class Student{ public: char *name; int age; public: void print(){ printf("%s--%d",name,age); } p
2016-03-10 09:00:52
645
原创 Next Permutation
题目:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest
2015-12-17 20:10:31
358
原创 Merge Two Sorted Lists
题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.思路:从最前面开始比较,每次都是最一开始的进行比较,但要注意比较过程中检查是否有链表已经为空。
2015-12-17 19:49:49
385
原创 Valid Parentheses
题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are
2015-12-15 20:51:54
328
原创 Valid Sudoku
题目:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partiall
2015-12-15 20:46:24
365
原创 Reverse Nodes in k-Group
题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain a
2015-12-09 09:25:55
449
原创 Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, suc
2015-12-06 22:22:21
432
原创 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7
2015-12-05 23:50:32
377
原创 First Missing Positive
题目:Given an unsorted integer array, find the first missing positive integer.
2015-12-05 23:50:04
335
原创 Sudoku Solver
题目:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A s
2015-12-05 08:02:53
433
原创 Search in Rotated Sorted Array
题目:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array
2015-12-04 00:17:29
385
原创 Substring with Concatenation of All Words
题目:You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once a
2015-12-03 23:56:02
345
原创 Median of Two Sorted Arrays
题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).思路:本题如此讲解,设两个数组:a[
2015-12-02 23:35:25
389
原创 Palindrome Number
题目:Determine whether an integer is a palindrome. Do this without extra space.思路:一个技巧就是先计算出一共几位数,对应后面几个零;接下来就是循环,首先除以div,在%10,就是最左边最右边的数字,判断两者是否相等。不等,直接返回。代码:class Solution {public:
2015-12-02 23:27:35
359
原创 Word Ladder II
题目:Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:Only one letter can be changed at a time
2015-12-01 23:55:25
373
原创 Word Ladder
题目:Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at
2015-12-01 23:51:17
379
原创 Merge k Sorted Lists
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.思路:代码:/** * Definition for singly-linked list. * struct ListNode { * int val; *
2015-11-30 22:48:15
325
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人