![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++学习
hellokazhang
不羁的游戏开发者,喝得惯红酒,吃得了大排档,不轻易给自己设限。
展开
-
c++之文件操作,(不以简单而不为,不以难而畏为)
操作相对简单,但不要以为简单而不动手,文件操作流在Java和C++都很重要,以下是雏形:(让您溅笑了)文件写入操作#include#include#includeusing namespace std;int main(){ ofstream file("c:\\hello.txt"); if(!file){ cout<<"can't open it"<<en原创 2012-07-21 21:27:10 · 1033 阅读 · 0 评论 -
map和multimap
直接贴代码:基础的东西,不解释#include#include#includeusing namespace std;int main(){ mapm; m.insert(make_pair("zhang1",21)); m.insert(make_pair("zhang2",22)); m.insert(make_pair("zhang3",23)); (原创 2012-07-25 18:18:42 · 444 阅读 · 0 评论 -
仿函数与普通函数的区别
何为仿函数,简单的说就是一个类中,定义了一个operator()函数,并且该函数用于algorithm的函数中,用该类作为algorithm算法的一个参数:这样做,跟普通函数有何不同呢?仿函数又叫函数对象,形如:class FunctionObjectType{ public: void operator()(){ statem原创 2012-07-26 15:22:54 · 3387 阅读 · 1 评论 -
仿函数和普通函数的区别
何为仿函数,简单的说就是一个类中,定义了一个operator()函数,并且该函数用于algorithm的函数中,用该类作为algorithm算法的一个参数:这样做,跟普通函数有何不同呢?仿函数又叫函数对象,形如:class FunctionObjectType{ public: void operator()(){ statem原创 2012-07-26 23:56:49 · 126 阅读 · 0 评论 -
mem_fun_ref,mem_fun,not1,not2,ptr_fun
今天调试程序的时候,遇到了这样一个问题bool check(int elem);vecotv; ...pos=find_if(v.begin(),v.end(),not1(check))竟然出错,查找资料之后,发现,原来原因如下:ptr_fun做的唯一的事是使一些typedef有效( 仿函数类的operator()所带的参数的类型和它的返回类型。对于binary_functio原创 2012-07-26 23:54:35 · 1893 阅读 · 0 评论 -
STL之组合型函数适配器f(g(x)),f(g(x1,x2)),f(g(x),h(x)),f(g(x1),h(x2))
组合型函数分成以下几种,自己写函数适配器f(g(elem)) ------> compose_f_gxf(g(elem1,elem2)) ------> compose_f_gxyf(g(elem),h(elem)) ------> compose_f_gx_hxf(g(elem),h(elem2))原创 2012-07-27 09:46:48 · 1281 阅读 · 0 评论 -
STL之排序算法
SortingsortSort elements in range (function template)stable_sortSort elements preserving order of equivalents (function template )partial_sortPartially Sort elements in r原创 2012-07-27 10:34:10 · 634 阅读 · 0 评论 -
for_each()浅析
for_each()算法非常灵活,它可以不同的方式存取,处理,修改每一个元素对于区间[beg,end)中的每个元素调用op(elem)返回op的一个副本op可以改变元素,注意它和transform的差别op的任何返回值被忽略复杂度:线性,numberofElement次UnaryProcfor_each(InputIterator beg,InputIterator e原创 2012-07-27 13:35:23 · 554 阅读 · 0 评论 -
find,find_if,count,count_if,search_n,search使用浅析
find,和find_if用于容器查找某个值,返回是迭代器。其中关联容器是基于二叉树的,它有自己更高性能的成员函数find和find_if [count和count_if也是一样的]STL这东西,得多敲代码,多理解,原谅我的惰性,我可直接贴代码了。推荐一个网站,可以根据头文件找函数,里面可以找到所有algorithm函数原型和例子:http://www.cplusplus.com/ref原创 2012-07-27 14:37:50 · 1400 阅读 · 0 评论 -
find_first_of,adjacent_find,equal,mismatch,lexicographical_compare大杂烩
各种算法,近日得闲,敲了一下:看算法名称,也大概能猜出这个算法是做什么的,因此,看一下例子就算了,不解释了;#include#include#include#include#includeusing namespace std;int main(){ int a[]={1,2,3,4,5,6,7,8,9,10}; vectorv(a,a+10); vecto原创 2012-07-27 16:42:05 · 444 阅读 · 0 评论 -
replace,replace_if,replace_copy,repace_copy_if
代码如下:#include#include#include#include#includeusing namespace std;int main(){ vectorv; for(int i=0;i<10;++i){ v.push_back(i); } replace(v.begin(),v.end(),5,555); //把5换成555 co原创 2012-07-29 15:16:25 · 503 阅读 · 0 评论 -
自己写一个数组,同样也可以写一个容器
#include#include#include#include#includeusing namespace std;templateclass carray{private: T v[thesize];public: typedef T value_type; typedef T* iterator; typedef const T* con原创 2012-07-25 19:04:03 · 457 阅读 · 0 评论 -
迭代器之Adapters(STL)
c++标准程序库提供了数个预先定义的特殊迭代器,也叫做迭代器适配器,它们不仅起辅助作用,还能赋予整个迭代器抽象概念更加强大的能力。虽然我的前面一下博客有讲到几种迭代器的适配,但是讲的比较零散,今天翻起STL标准库,想在这里总结一下:迭代器有三种适配器1 Insert iterator(插入迭代器)2 Stream iterator(流迭代器)3 Reverse iterator原创 2012-07-24 20:12:53 · 630 阅读 · 0 评论 -
algorithm算法,举一反三,此处sort为例
algorithm的sort算法给我们带了很多大的方便,用来排序容器的内容,真是方便,不仅可以从小到大的排序,还能从大到小的排序。可以结合function的函数对象,也能自己写谓词。真是太方便了。其他的算法也如出一辙,举一反三。1普通排序:#include#include#includeusing namespace std;int main(){ int a[]={8原创 2012-07-21 14:46:24 · 2335 阅读 · 0 评论 -
dynamic_cast<type>()转型与typeid()的使用
按照c++之父的经典之作 c++程序设计语言的说法dynamic_cast()可以把基类的指针转化为子类的指针,代码如下:#include#includeusing namespace std;class Person{public: Person(string name,int age){ this->name=name; this->age=age;原创 2012-07-19 21:49:17 · 916 阅读 · 0 评论 -
STL之pair源码分析
直接进入主题,请看pair的源码:namespace std{ template struct pair{ typedef T1 first_type; //简化类型名称 typedef T2 second_type; T1 first; //两个数据成员 T2 second; pair():first(T1()),second(T2()){}原创 2012-07-23 16:25:01 · 738 阅读 · 0 评论 -
STL之auto_ptr智能指针
auto_ptr是这样一种指针,它是“它所指向的对象”的拥有者,所以,当身为对象拥有者的auto_ptr被摧毁时,该对象也将遭到摧毁。auto_ptr要求一个对象只能有一个拥有者,严禁一物二主。设计动机:1获取一些资源2执行一些动作3释放所获取的资源,不用自己调用delete,不用担心异常。。。的原因,导致内存泄漏的问题了。#include#include#includ原创 2012-07-18 21:24:07 · 457 阅读 · 0 评论 -
list操作整理
list自己定义了sort(),unique(),可以不用algorithm的sort和unique了。对比请看我的博客《c++容器算法》粘接,排序,归并void splice(iterator pos,list& x);void splice(iterator pos,list& x,iterator p);void splice(iterator pos,list& x,i原创 2012-07-20 14:22:38 · 666 阅读 · 0 评论 -
何谓排序准则
所谓“排序准则”,必须定义strict weak ordering,其意义如下1必须是“反对称的(antisymmetric)”对operator对判断式predicate op()而言,如果op(x,y)为真,则op(y,x)为假2必须是“可传递的(transitive)”对operator对判断式predicate op()而言,如果op(x,y)为真且op(y,z)为原创 2012-07-25 11:35:31 · 623 阅读 · 0 评论 -
set和multiset
比较简单,所以没有注释。#include#include#include#includeusing namespace std;int main(){ set >s; s.insert(4); s.insert(3); s.insert(5); s.insert(1); s.insert(6); s.insert(2); s.insert(5原创 2012-07-25 14:05:23 · 1428 阅读 · 0 评论 -
不再害怕指针(极力推荐!)
很多学习C语言或c++的朋友都很怕指针,但是指针却是那么多的重要。今天,鄙人讲一下对指针的认识,让您溅笑了。何为指针,指针就是一个地址,内存地址。int a=100; int *p=&a; p是一个指针,这个指针指向了一个整数a, p就是一个地址,里面存放着变量a说得更形象一点吧,指针跟变量的关系就像是钥匙和抽屉的关系,抽屉(指针)是用来打开(指向)抽屉(变量)的。而抽屉里面的东西,原创 2012-07-24 14:58:06 · 1684 阅读 · 1 评论 -
计数指针,源码雏形
跟auto_ptr对照一下,auto_ptr复制或转移拥有权,看看计数指针有什么不同,看看计数指针是怎么实现的?#include#include#include#include#include#includeusing namespace std;templateclass CountedPtr{private: T* ptr; long *coun原创 2012-07-25 20:07:49 · 411 阅读 · 0 评论 -
细说容器内的各成员函数的使用范围(绝对全)
自己找找看吧(红色字体表示对前面函数的支持)size_type container::capacity() const vector,stringvoid constainer::reserve(size_type num) vector,stringbool comparison(const container& c1,const container& c2) vecto原创 2012-07-25 22:41:05 · 677 阅读 · 0 评论 -
remov,remove_if,remove_copy,remove_copy_if
代码如下:#include#include#include#include#includeusing namespace std;int main(){ int a[]={1,2,3,4,5,6,7,8,9,10}; vectorv(a,a+10); vector::iterator it=remove(v.begin(),v.end(),5); cou原创 2012-07-29 15:17:35 · 369 阅读 · 0 评论 -
set_union,set_intersection,set_difference,set_symmetric_difference
直接代码://set_union#include#include#include#include#include#includeusing namespace std;bool fanxu(int a,int b){ return a<b;}int main(){ int a[]={1,2,2,4,6,7,7,9}; int b[]={2,2原创 2012-07-29 15:34:08 · 680 阅读 · 0 评论 -
heap算法,binary_search
直接代码如下://heap算法#include#include#include#include#includeusing namespace std;int main(){ int a[]={7,4,1,8,5,2,9,6,3,8}; vectorv(a,a+10); make_heap(v.begin(),v.end()); for(vector:原创 2012-07-29 15:28:34 · 441 阅读 · 0 评论 -
accumulate,inner_product,partial_sum,adjacent_difference
直接代码如下://数值计算//accumulate#include#include#include#include#include#includeusing namespace std;int main(){ int a[]={1,2,3,4,5,6,7,8,9,10}; vectorv(a,a+10); int max=accumulate(v原创 2012-07-29 15:36:17 · 649 阅读 · 0 评论 -
find_end,find_first_of
直接代码:#include#include#include#includeusing namespace std;int main(){ int a[]={1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10}; vectorv(a,a+20); vectorvv(a+3,a+8); vector::iterator end原创 2012-07-29 15:44:48 · 579 阅读 · 0 评论 -
mismatch和lexicographical_compare
代码如下:#include#include#include#include#includeusing namespace std;bool jianyi(int a,int b){ return a==b-1;}int main(){ int a[]={1,2,3,4,5,6,7,8,9,10}; int b[]={1,2,3,5,6,7,8,9,1原创 2012-07-29 15:48:44 · 442 阅读 · 0 评论 -
IO基本操作
要求:理解C++流操作函数理解I/O流格式控制的两种方法输出流(cout)基本操作1、使用流插入运算符( #include void main() { char c[]="Good morning"; cout } 运行结果: Good morning Press any key to continue2、使用成转载 2012-07-30 10:20:55 · 458 阅读 · 0 评论 -
win32
#include#includeLRESULT CALLBACK WinSunProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCm原创 2012-07-13 11:54:22 · 458 阅读 · 0 评论 -
条款23宁以non-member函数代替member函数
1 person.h#ifndef PERSON_H#define PERSON_H#include#includeusing namespace std;namespace stdperson{ class Person { public: Person(); Person(string name, int age); void SetName(strin原创 2013-03-06 15:28:29 · 730 阅读 · 0 评论 -
namespace
1 person.h#ifndef PERSON_H#define PERSON_H#include#includeusing namespace std;namespace stdperson{ class Person { public: Person(); Person(string name, int age); void SetName(strin原创 2013-03-06 13:32:40 · 766 阅读 · 0 评论 -
条款31将文件间的编译依存关系降至最低
将文件间的编译依存关系降至最低1.handle方法:class Date{public: Date(int a):date(a){}private: int date;};class PersonImpl;class Person{public: Person(const string &name,const Date &birthday);转载 2013-03-07 10:13:26 · 669 阅读 · 0 评论 -
双链表操作
//------------------------------------------------------------------------------// 双链表的相关操作//------------------------------------------------------------------------------#include using namesp原创 2013-07-22 08:13:36 · 1163 阅读 · 1 评论 -
partition,stable_partition,sort,stable_sort
直接代码://将元素向前搬移 partition stable_partition#include#include#include#include#includeusing namespace std;bool isoushu(int a){ return a%2==0;}int main(){ int a[]={1,2,3,4,5,6,7,8,9原创 2012-07-29 15:25:21 · 496 阅读 · 0 评论 -
fill,fill_n,generate,generate_n
STL 算法实践,直接贴代码:#include#include#include#includeusing namespace std;int main(){ fill_n(ostream_iterator(cout," "),10,7.7); cout<<endl; vectorv; fill_n(back_inserter(v),10,10086);原创 2012-07-29 15:14:52 · 986 阅读 · 0 评论 -
adjacent_find,equal
代码如下:#include#include#include#include#includeusing namespace std;int main(){ int a[]={1,2,3,4,5,6,6,8,9,10}; vectorv(a,a+10); vectorvv(a+3,a+8); vector::iterator it; it=ad原创 2012-07-29 15:47:08 · 327 阅读 · 0 评论 -
copy,copy_backward,swap,transform
STL 算法#include#include#includeusing namespace std;int main(){ vectorv; vectorvv; for(int i=1;i<=9;++i){ v.push_back(i); } copy(v.begin(),v.end(),back_inserter(vv)); for(vector原创 2012-07-29 15:11:44 · 435 阅读 · 0 评论 -
partial_sort,partial_sort_copy,nth_element
直接代码://局部排序 partial_sort partial_sort_copy#include#include#include#include#includeusing namespace std;int main(){ int a[]={1,4,7,2,5,8,3,6,9,10}; vectorv(a,a+10); vectorvv(a,a+1原创 2012-07-29 15:27:19 · 546 阅读 · 0 评论