代码改变世界

随笔分类 -  C++

转:C++中实现对map按照value值进行排序

2012-07-09 00:15 by youxin, 784 阅读, 收藏, 编辑
摘要: map的两个值分别为key值和value值排序,默认是根据key的 < 关系来排序,map是按照key值进行排序的,但有时候需要按照value值进行排序,并且按照value的顺序输出key值,排序代码如下:typedef pair<string, int> PAIR; int cmp(const PAIR& x, const PAIR& y) { return x.second > y.second; } map<string,int> m; vector<PAIR> vec; for (map<wstring,int> 阅读全文

c++ 虚拟析构函数

2012-07-09 00:01 by youxin, 466 阅读, 收藏, 编辑
摘要: 通过基类指针删除派生类对象,基类又没有虚析构函数,结果不可确定。(派生类的析构函数没有被调用,派生类的对象没有被回收)。如下是没有定义虚拟的析构函数。#include <iostream>using namespace std;class Base{public: Base( void ) { cout << "Base::Base( )" << endl; } ~Base( void ) //基类没有虚析构函数时 { cout << "Base::~Base( )"... 阅读全文

c++模板类/模板函数的声明与定义应该放在头文件里

2012-06-29 16:52 by youxin, 3685 阅读, 收藏, 编辑
摘要: 如果函数模板按照普通的函数声明放在头文件的,定义放在。cpp文件,会出现错误:模板函数声明、定义、引用有什么要注意的问题么?? -- mylib.h -- template <class T> T max_v(T v1, T v2); -- mylib.cpp -- #include "mylib.h " template <class T> T max_v(T v1, T v2) { return (v1 > v2 ? v1 : v2); } -- ... 阅读全文

c++ STL unique

2012-06-29 13:46 by youxin, 787 阅读, 收藏, 编辑
摘要: unique用来去掉重复的元素。函数原型如下:template ForwardIterator unique ( ForwardIterator first, ForwardIterator last );template ForwardIterator unique ( ForwardI... 阅读全文

转:正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range

2012-06-29 02:00 by youxin, 1051 阅读, 收藏, 编辑
摘要: 首先可供查找的算法大致有count,find,binary_search,lower_bound,upper_bound,equal_range。带有判别式的如count_if,find_if或者binary_search的派别式版本,其用法大致相同,不影响选择,所以不作考虑。注意这些查找算法需要序列式容器,或者数组。关联容器有相应的同名成员函数except binary_search。首先,选择查找算法时,区间是否排序是一个至关重要的因素。可以按是否需要排序区间分为两组:A. count,findB. binary_search,lower_bound,upper_bound,equal_r 阅读全文

c++ 无法链接的外部变量

2012-06-29 01:34 by youxin, 389 阅读, 收藏, 编辑
摘要: 这是一个很头疼的问题,一般是由于只有声明没有定义,编译时只查找声明,没有报错,链接就报错了。若在头文件了声明:bool compName(const Student &,const Student &);。cpp文件里定义如下:bool compName(const Student &p1,const Student &p2){ return p1.name<p2.name;}链接时会报错,无法链接的外部变量compName。上面的声明中类型为const ,定义却没有,或者反过来,只要声明和定义不一致都会出现错误。在c++中,出现const要谨慎。慎用co 阅读全文

c++ STL equal_range lower_bound upper_bound

2012-06-28 20:13 by youxin, 1051 阅读, 收藏, 编辑
摘要: 用来得到容器中等于一个值的子序列。template <class ForwardIterator, class T> pair<ForwardIterator,ForwardIterator> equal_range ( ForwardIterator first, ForwardIterator last, const T& value );template <class ForwardIterator, class T, class Compare> pair<ForwardIterator,ForwardIterator> equa 阅读全文

c/c++ 调用dos 命令

2012-06-28 17:44 by youxin, 740 阅读, 收藏, 编辑
摘要: 需要用到这个函数:int system ( const char * command ); (头文件为cstdlib)Execute system commandInvokes the command processor to execute a command. Once the command execution has terminated, the processor gives the control back to the program, returning anintvalue, whose interpretation is system-dependent.The fun. 阅读全文

C++基类与派生类的转换

2012-06-28 16:42 by youxin, 438 阅读, 收藏, 编辑
摘要: 只有公用派生类才是基类真正的子类型,它完整地继承了基类的功能。基类与派生类对象之间有赋值兼容关系,由于派生类中包含从基类继承的成员,因此可以将派生类的值赋给基类对象,在用到基类对象的时候可以用其子类对象代替。具体表现在以下几个方面:1派生类对象可以向基类对象赋值。 可以用子类(即公用派生类)对象对其基类对象赋值。如 A a1; //定义基类A对象a1 B b1; //定义类A的公用派生类B的对象b1 a1=b1; //用派生类B对象b1对基类对象a1赋值在赋值时舍弃派生类自己的成员。实际上,所谓赋值只是对数据成员赋值,对成员函数不存在赋值问题。请注意: 赋值后不能企图通过对象a1去... 阅读全文

c++ max_elment和min_element

2012-06-28 00:48 by youxin, 724 阅读, 收藏, 编辑
摘要: max_element和min_element用来求一个范围内的最大值和最小值template <class ForwardIterator> ForwardIterator max_element ( ForwardIterator first, ForwardIterator last );template <class ForwardIterator, class Compare> ForwardIterator max_element ( ForwardIterator first, ForwardIterator last, ... 阅读全文

转:c++ 基类转换为派生类

2012-06-27 21:27 by youxin, 706 阅读, 收藏, 编辑
摘要: 基类类型的引用或指针既可以引用基类对象,也可以引用派生类对象,但编译器只把它当做基类类型对象。#include <iostream>using namespace std;class base{public: base():cat(0){} void print() { cout << "cat" << cat << endl; }private: int cat;};class derive:public base{public: derive():base(),dog(1){} //这里base()可以不用写,默认调用无参 阅读全文

c++ 很多相关概念

2012-06-27 19:59 by youxin, 256 阅读, 收藏, 编辑
摘要: 下面的一个简单的继承:#include<iostream>using namespace std;class Animal{public: Animal(int height,int weight) { cout<<"animal construct"<<endl; } ~Animal() { cout<<"animal destruct"<<endl; }};class Fish:public Animal{public: Fish() { cout<<"fish con 阅读全文

c++ vector resize()和reserve()区别

2012-06-27 18:21 by youxin, 1150 阅读, 收藏, 编辑
摘要: resize()是改变了size和capacity。void resize ( size_type sz, T c = T() );Change sizeResizes the vector to containszelements.Ifszis smaller than the current vectorsize, the content is reduced to its firstszelements, the rest being dropped. 如果sz小于现在的尺寸,内容减小到sz,其余 部分丢弃。Ifszis greater than the current vectors. 阅读全文

c++ 基类与派生类的构造函数

2012-06-23 00:01 by youxin, 365 阅读, 收藏, 编辑
摘要: 基类与派生的构造函数的调用顺序。创建时先基类后派生类。销毁时先派生类后基类。当有参数时,参数必须传送给基类。注意例子中传递的方法。#include <iostream>#include <string>using namespace std;class CBase { string name;public: CBase(string s) : name(s) { cout << "BASE: " << name << endl; } ~CBase() { cout << "~BASE" 阅读全文

c++ 模板函数remove()

2012-06-22 00:51 by youxin, 1452 阅读, 收藏, 编辑
摘要: template ForwardIterator remove ( ForwardIterator first, ForwardIterator last, const T& value );注意返回迭代器。Remove value from rangeCompares the elements in the range[first,last)againstvalue, and removes those that compare equal from the resulting range. The resulting range co... 阅读全文

c++ vector 删除指定元素

2012-06-22 00:29 by youxin, 76044 阅读, 收藏, 编辑
摘要: 只使用vector的erase函数,记住,该函数是迭代器失效,返回下一个迭代器。#include #include using namespace std; int main(){ vector arr; arr.push_back(6); arr.push_back(7); ... 阅读全文

C++ string 比较

2012-06-21 22:19 by youxin, 44790 阅读, 收藏, 编辑
摘要: 传统的c字符串比较必须用strcmp函数:(不能用==,否则比较的只是两个地址)函数名: strcmp功 能: 串比较用 法: int strcmp(char *str1, char *str2);看Asic码,str1>str2,返回值 > 0;两串相等,返回0上面的头文件为<string.h> strncasecmp()是忽略大小写的。c++ string 类型的比较可以用string的函数compare()int compare ( const string& str ) const;int compare ( const char* s ) const; 阅读全文

c++ distance 和advance函数

2012-06-16 22:47 by youxin, 8716 阅读, 收藏, 编辑
摘要: distance主要是用来求两个迭代器之间的元素个数。template<class InputIterator> typename iterator_traits<InputIterator>::difference_type distance (InputIterator first, InputIterator last);Return distance between iteratorsCalculates the number of elements betweenfirstandlast.Ifiis aRandom Access Iterator, the f 阅读全文

c++ fill 和fill_n

2012-06-16 22:30 by youxin, 649 阅读, 收藏, 编辑
摘要: template < class ForwardIterator, class T > void fill ( ForwardIterator first, ForwardIterator last, const T& value );Fill range with valueSetsvalueto all elements in the range[first,last).fill_n函数模板如下:template < class OutputIterator, class Size, class T > void fill_n ( OutputIterato 阅读全文

vs 在watch监视窗口 vector 错误 :overloaded operator not found

2012-06-15 13:42 by youxin, 1213 阅读, 收藏, 编辑
摘要: 一下的代码,debug显示是正常的。如果你调试时,查看watch窗口会发现一个错误:overloaded operator not found找不到重载运算符#include<iostream>#include<vector>#include<string>using namespace std;int main(){ vector<vector<string> > vec; vector<string> vec1,vec2; vec1.push_back("can"); vec1.push_back( 阅读全文
上一页 1 ··· 4 5 6 7 8 9 10 11 下一页