代码改变世界

随笔分类 -  C++

c++ vector.clear()

2012-04-16 18:51 by youxin, 683 阅读, 收藏, 编辑
摘要: vector::clearvoid clear ( );Clear content 清除全部的内容All the elements of the vector are dropped: their destructors are called, and then they are removed from thevectorcontainer, leaving the container with asizeof0.// clearing vectors#include <iostream>#include <vector>using namespace std;int 阅读全文

c++ 流的clear 与sync

2012-04-16 18:26 by youxin, 1470 阅读, 收藏, 编辑
摘要: 先看函数原型:ios::clearvoid clear ( iostate state = goodbit );Set error state flagsSets a new value for theerror control state.All the bits in thecontrol stateare replaced by the new ones; The value existing before the call has no effect.If the function is called withgoodbitas argument (which is the defau 阅读全文

C++ copy 函数

2012-04-16 17:18 by youxin, 10067 阅读, 收藏, 编辑
摘要: 先看下面的一段代码: vector<int> u(10,100); vector<int> v; copy(u.begin(),u.end(),v.begin()); for(vector<int>::iterator it=v.begin();it!=v.end();it++) { cout<<*it<<ends; }运行错误! 功能很简单,把vector u复制给v,但运行异常。为什么? vector<int> v;定义时定义时没有分配空间,copy不成功。应改为vector<int> v(u.size() 阅读全文

C++ 各种排序算法的区别

2012-04-15 21:55 by youxin, 498 阅读, 收藏, 编辑
摘要: 一、partition 算法可以把满足特定条件的元素放在区间的前部,并且返回一个迭代器,指向第一个不满足条件的元素。例如:我们要找出一个班级中成绩大于60分的学生,那么我们应该这样做。bool IsPass(int a){ return a>=60;}vector<int>::iterator Pass = partition(record.begin(),record.end(),IsPass);在此调用后,所有分数大于60分的学生都被放在record.begin()到Pass(不包括pass)之间,而分数小于60分的学生都被放在Pass到record.end()之间。2. 阅读全文

c++计算程序运行时间

2012-04-15 15:29 by youxin, 2500 阅读, 收藏, 编辑
摘要: C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下: clock_t clock( void ); 这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock)。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对 它的定义: #ifndef _CLOCK_T_DEFINED typedef long clock_t; #define _CLOCK_... 阅读全文

c++ rbegin() 和rend()

2012-04-15 14:15 by youxin, 1877 阅读, 收藏, 编辑
摘要: 看下面的一段代码: for(vector<string>::iterator it=vec.rbegin();it!=vec.rend();it--) { cout<<*it<<ends; }编译有错,为什么?rbegin() rend()的返回类型不是vector<string>::iterator ,而是reverse_iterator; reverse_iterator rbegin();const_reverse_iterator rbegin() const;Return reverse iterator to reverse begi 阅读全文

For在VC6.0和VS2010编译器下的作用域问题

2012-04-06 13:04 by youxin, 393 阅读, 收藏, 编辑
摘要: 根据c++标准,在for的初始语句中声明的变量,其作用范围是从它定义的位置开始,一直到for所带语句的作用域结束。 由于VC6.0对C++标准的支持性不是太好,导致了在for定义的变量在for外部仍可返回。看下面的代码: #include <iostream>using namespace std;int main(){ for(int i=0;i<5;i++) { } cout<<i<<endl; return 0;}在VC6.0上面运行正常,输出5;在vs2010显示错误:error C2065: “i”: 未声明的标识符; 阅读全文

用vector建立2D数组

2012-04-06 08:33 by youxin, 1005 阅读, 收藏, 编辑
摘要: vector< vector<int> > //一定要有空格,否则认为移位运算符。 vector<vector<int> > array(3) ,申请保存3个向量的vector,array[i]返回的是第i个向量,同理array[i][j]返回的是第i个向量的第j个元素。 问题到这里,你可能会得意的说:"我明白了,很简单吗!"。别急,还有一些细节问题:如下 vector< vector<int> > array2(3); array2[1][2] = 9; 我保证你的程序会segement failed 阅读全文

c++ ios_base类

2012-04-05 22:40 by youxin, 1261 阅读, 收藏, 编辑
摘要: ios_base is Base class with type-independent members for the standard stream classes(ios);其中有一个成员函数ios_base::flags 用来控制流的格式。fmtflags flags ( ) const; fmtflags flags ( fmtflags fmtfl );Get/set format flagsThe first syntax returns the current set of format flags set for the stream.The second syntax s. 阅读全文

c++设置左对齐

2012-04-03 22:32 by youxin, 4311 阅读, 收藏, 编辑
摘要: 方法一:cout.flags (ios::left);//设置对齐的标志位为左2. cout<<left 对齐选项:(left,rightorinternal).manip setw ( int n );默认右对齐setw(n) 设置固定宽度Set field widthSets the number of characters to be used as thefield widthfor the next insertion operation.smanip setfill ( char c );// setfill example#include <iostream&g 阅读全文

Error C2361: initialization of (identifier) is skipped by (default) label

2012-04-03 16:16 by youxin, 1011 阅读, 收藏, 编辑
摘要: 看下面一段代码:// C2361.cppvoid func( void ) { int x; switch (x) { case 0 : int i = 1; { int j = 1; } default : // C2361 error int k = 1; }}编译时会报错,Error C2361: initialization of (identifier) is skipped by (default) label,msdn解释是这样的,The initialization ofidentifiercan be skipped in... 阅读全文

C++抽象类的纯虚函数

2012-03-31 22:25 by youxin, 7427 阅读, 收藏, 编辑
摘要: 1,定义: 纯虚函数是在基类中声明的虚函数,它在基类中没有定义,但要求任何派生类都要定义自己的实现方法。在基类中实现纯虚函数的方法是在函数原型后加"=0" ,同 java中抽象方法类似virtualvoidfuntion1()=0二、引入原因:1、为了方便使用多态特性,我们常常需要在基类中定义虚拟函数。2、在很多情况下,基类本身生成对象是不合情理的。例如,动物作为一个基类可以派生出老虎、孔雀等子类,但动物本身生成对象明显不合常理。 为了解决上述问题,引入了纯虚函数的概念,将函数定义为纯虚函数(方法:virtualReturnTypeFunction()=0;),则编译器要求 阅读全文

转:c++ virtual function

2012-03-30 15:12 by youxin, 735 阅读, 收藏, 编辑
摘要: C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. The whole function body can be replaced with a new set of implementation in the derived class. The concept of c++ virtual functions is different fromC++ Function overloading.C++ Virt. 阅读全文

C++virtual 虚函数详解

2012-03-30 15:05 by youxin, 551 阅读, 收藏, 编辑
摘要: 首先看一下程序,猜猜运行结果是什么?#include<iostream>using namespace std;class Animal{public: Animal(int height,int weight) { cout<<"Animal construct"<<endl; } ~Animal() { cout<<"Animal destruct"<<endl; } void eat() { cout<<"Animal's eat"<<e 阅读全文

转:C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法

2012-03-22 22:16 by youxin, 570 阅读, 收藏, 编辑
摘要: C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法学C++的时候,这几个输入函数弄的有点迷糊;这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行)1、cin2、cin.get()3、cin.getline()4、getline()5、gets()6、getchar()附:cin.ignore();cin.get()//跳过一个字符,例如不想要的回车,空格等字符1、cin>>用法1:最基本,也是最常用的用法,输入一个数字:#include using na 阅读全文

C++cin处理空格问题

2012-03-21 22:21 by youxin, 8101 阅读, 收藏, 编辑
摘要: 仔细分析下面程序 #include<iostream>using namespace std;#include<string>int main(){ string str="jack bullshit jack"; { string bullshit="bullshit"; string bush="bush"; size_t pos=str.find(bullshit,0); size_t length=8; if(pos!=string::npos) str.r... 阅读全文
上一页 1 ··· 7 8 9 10 11