![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C/C++
文章平均质量分 62
duyiwuer2009
这个作者很懒,什么都没留下…
展开
-
scanf的输入字符时的陷阱
//这个例子证明了:scanf("%c", &ch)等价于ch = getchar()//在用%c的时候要格外小心//int型:正确#include int main(){ int i; int var1, var2; for(i=0 ; i<10; i++) { scanf("%d%d",&var1, &var2); pr原创 2011-12-13 17:25:16 · 1098 阅读 · 0 评论 -
Linux 和 Windows 中 fopen 打开方式的区别
The mode string can also include the letter 'b' either as a last characteror as a character between the characters in any of the two-characterstrings described above. This is strictly for compati原创 2012-05-10 20:49:24 · 3553 阅读 · 0 评论 -
判断CPU或JVM是大端还是小端的方法——字节数组与整形互转
----------字节数组转为整形C版:/** * Windows 7 32位,CodeBlocks */#include #include /** * 将字节数组(char[])转为整形(int) */int main(){ char chArray[] = {0x78,0x56,0x34,0x12}; int *p = (int *)chArray; p原创 2012-04-12 20:57:10 · 4150 阅读 · 0 评论 -
fopen() 的打开模式
对于Windows平台,若要以二进制形式打开文件,则需添加'b'(UNIX/Linux平台不区分文本文件和二进制文件,http://blog.csdn.net/duyiwuer2009/article/details/7555137)。'b'的添加规则如下:This additional "b" character can either be appended at the end of th原创 2012-05-13 15:32:19 · 1081 阅读 · 0 评论 -
指向数组的指针与动态二维数组
Example-01:用指向数组的指针实现与静态二维数组兼容的动态二维数组#include #include #define COLUMNS 5#define ROWS 10void output(int a[][COLUMNS], int rows){ int i, j; for(i = 0; i < rows; i++) { for(j = 0; j < COLUMN原创 2012-06-03 16:10:28 · 1107 阅读 · 0 评论 -
二维数组作实参的传递方法
//二维数组作实参的传递方法#include //用二维数组作形参void output1(int m[][3]){ int i, j; for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { printf("%d ", m[i][j]); }原创 2011-12-13 10:41:12 · 4206 阅读 · 1 评论 -
C/C++ 中的main函数的argc和argv参数
argc - argument countargv - argument vectorThe main() functionhttp://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fmainf.htm原创 2012-06-19 15:53:33 · 803 阅读 · 0 评论 -
C99数据类型
【注意】C标准没有具体规定各种类型数据占用存储单元的长度,这是由各编译系统自行决定的。≥C标准只要求long型数据长度不短于int型,short型不长于int型。即sizeof(short)≤sizeof(int)≤sizeof(long)≤sizeof(long long)VC和gcc所占字节数:int shortlonglong longVC 4 248gc原创 2012-08-15 13:29:29 · 3066 阅读 · 0 评论 -
防止内存泄露的几个良好的编码习惯
以将子函数设计成共别人使用的 API 的思想来考虑如何设计子函数----------情景:主调函数需要调用子函数 f,函数f返回一段数据区1、在主调函数中分配好内存,将地址传递给子函数f。这样做的好处是,内存的释放工作交给分配者,子函数不用管,保证子函数的模块独立性。与之对应的不良编码习惯是,子函数分配内存,将这段内存的首地址返回给主调函数,这样做的坏处很明显,调用者很容易忘记释放这段原创 2012-08-15 20:12:51 · 1640 阅读 · 0 评论 -
Introduction to Endianness(大端小端介绍)
Introduction to EndiannessMichael Barr12/28/2001 10:35 AM ESTFrom: http://www.eetimes.com/discussion/beginner-s-corner/4023889/Introduction-to-Endianness译文:http://blog.csdn.net/xbt746/arti转载 2012-04-12 20:04:29 · 1068 阅读 · 0 评论 -
What does the first letter "c" mean in "calloc"?
The 'c' indicates 'cleared.' The allocated memory is initialised, or cleared. The initialisation value used by calloc is 0.http://answers.yahoo.com/question/index?qid=20080217131743AAlmmpy原创 2013-04-06 16:20:23 · 825 阅读 · 0 评论 -
STL 之 list
list 由双向链表实现,下面主要介绍 STL list 的c原创 2014-04-15 13:50:40 · 701 阅读 · 0 评论 -
【栈】【双端队列】【stack】【deque】hdu 4286
http://acm.hdu.edu.cn/showproblem.php?pid=4286sh原创 2014-04-07 22:05:31 · 999 阅读 · 0 评论 -
STL中map,multimap,set,multiset,unordered_map,unordered_multimap,unordered_set,unordered_multiset的实现方法
1. set, multiset, map, multimap, unordered_map原创 2014-04-15 19:30:12 · 2585 阅读 · 0 评论 -
【优先队列】【priority_queue】【getline】【istringstream】hdu 1873
此题需要注意2点:1. 重载 "2. 由于每组数据处理完之后,队列并没有 pop 空,但 priority_queue 又没有提供清空队列的方法,所以只能每组数据都重新创建对象/** * hdu 1873 * 【优先队列】【priority_queue】 * 此题需要注意2点: * 1. 重载 "<" 的语义:当优先级相等时,先到的优先级高,不能指望 * priorit原创 2014-04-12 23:34:07 · 808 阅读 · 0 评论 -
【map】【multiset】hdu 4022
1. 可能有重复点(这一点题目并没有说明),因此 set 行不通2. 用 C++ 的 cin, cout 超时原创 2014-04-18 12:34:07 · 794 阅读 · 0 评论 -
【map】【set】poj 3297
1. 每个项目的有效报名人数 --> map --> map mapNum4Prj2. 局部(当前项目)报名学生 --> set --> set setStu4Curr用于检测当前项目中该学生是否是第一次出现3. 全局学生报名的第一个项目 --> map --> map mapFirstPrj两个作用: (1) 用于检测该学生是否已经报名过其它项目 (2) 记录原创 2014-04-18 20:07:48 · 797 阅读 · 0 评论 -
C/C++ 字符串与数字相互转化方法小结
1. 字符串 --> 数字ato: atoi(), atol(), atoll(), atof()strto: strtol(), strtoul(), strtod()strto 是 ato 的升级版:(1) strto 支持转化成多种进制(2) ato 对错误情况的处理很不完善, strto 对完善了错误处理2. 数字 --> 字符串(1) sprint原创 2014-04-08 10:16:51 · 3048 阅读 · 0 评论 -
函数调用符重载
#include // std::cout#include // std::minus#include // std::accumulatestruct myclass{ int operator()(int x, int y) { return x+3*y; }};int main (){ int in原创 2014-04-09 21:14:08 · 759 阅读 · 0 评论 -
【STL】ostringstream, istringstream, stringstream
#include #include #include using namespace std;int main(){ /* * ostringstream * 可以理解为将输出重定向至 ostringstream 对象(内存区域) */ ostringstream oss; oss << 3.1415926 << " " << 3.原创 2014-04-08 10:09:39 · 1536 阅读 · 0 评论 -
拷贝构造函数与 "=" 操作符
#include using namespace std;class CTest{public: CTest(int aa): a(aa) { cout << "constructor: a = " << a << endl; } CTest(const CTest &in) { a = in.a; cout << "copy co原创 2014-09-07 14:29:57 · 632 阅读 · 0 评论 -
const 二级指针:invalid conversion from 'char**' to 'const char**'
将 cha原创 2014-09-19 19:34:28 · 10736 阅读 · 0 评论 -
显式调用构造函数及临时对象
在读 Google Protocol Buffer 的源码时发现一个段很晦涩的代码原创 2014-09-07 12:35:24 · 1005 阅读 · 0 评论 -
C++ const 小结
声明常量const int i = 10; // 常用int const i = 10; // 不常用const int i; // 错误1 和 2 意思一样,都是 i 的值不可修改。修饰指针const int *p; // 常用int * const p; // 不常用第一种:p 指向的变量的的值不可修改。You can think of this as read原创 2012-11-24 14:24:47 · 855 阅读 · 0 评论 -
运算符重载
1. 成员函数2.原创 2014-04-09 19:54:37 · 614 阅读 · 0 评论 -
防止全局变量、头文件重复包含与 extern 的使用
C语言中经常会出现文件重复包含而导致全局变量重复定义,下面以一个例子说明-----MakefileCC = gccOBJS = main.o errhandle.o strcopy.oCFLAGS = -Wall -std=c99main: ${OBJS} # or $(OBJS), ${OBJS} is shell style ${CC} ${CFLAGS} -o $@ ${原创 2012-08-17 21:25:57 · 2909 阅读 · 0 评论 -
一个例子理解 C++ 面向对象——构造函数、虚函数、抽象基类
#include using namespace std;void TestException(int argc){ try { if (argc != 2) { cout << "number of params not 2" << endl; throw -1; }原创 2013-11-12 21:25:43 · 1936 阅读 · 0 评论 -
C++ 子类能不能改变父类中成员的访问权限?
先看一个例子:#include using namespace std;class Base{public: Base(int i): common(i) {} virtual ~Base() { cout << "destructing Base" << endl; } virtual void Both() const { cout << "common="原创 2014-09-17 19:15:42 · 3496 阅读 · 0 评论 -
C++ 构造函数和析构函数是否可以继承?
先看一个例子:#include class A{public: A() { ::std::cout << "constructor: A" << ::std::endl; } // 构造函数不能为 virtual A(int aa): a(aa) { ::std::cout << "constructor: A, a = " << a << ::std::endl; }原创 2014-11-12 18:13:24 · 9597 阅读 · 0 评论 -
C++ 在线编译器(支持 C++11)
C++11 的 Inheriting constructors 特性在 GCC原创 2014-11-12 18:02:42 · 66707 阅读 · 3 评论 -
#pragma 预处理指令
#pragma预处理指示供编译器实现一些非标准的特性,C标准没有规定#pragma后面应该写什么以及起什么作用,由编译器自己规定。有的编译器用#pragma定义一些特殊功能寄存器名,有的编译器用#pragma定位链接地址,本书不做深入讨论。如果编译器在代码中碰到不认识的#pragma指示则忽略它,例如gcc的#pragma指示都是#pragma GCC ...这种形式,用别的编译器编译则忽略这些指原创 2014-11-13 19:59:24 · 708 阅读 · 0 评论 -
C++ 静态变量或成员初始化陷阱与缺陷
C/C++ 中静态变量初始化的问题归根节点在于一点:静态变量的创建顺序【禁止或不推荐】用函数返回值初始化静态变量int func(){ return 50;}void test(){ static int i = func();}int main(){ test(); return 0;}这段代码用 C 写是编译出错的,因为 C 中静态变量如果在声明原创 2014-11-17 21:08:27 · 2339 阅读 · 0 评论 -
C++ 字符串大小写转换
使用 algorithm 中的 transform:#include #include #include int main(){ std::string str = "FrancisHe"; std::transform(str.begin(), str.end(), str.begin(), ::toupper); std::cout << str << s原创 2015-01-07 15:48:12 · 1632 阅读 · 0 评论 -
gtest(Google Test)使用
gtest 代码目录结构说明:以 gtest-1.7.0 为例cmake, codegear, make, msvc, xcode构建测试项目的构建文件,如 make 就是 Makefile.To write a test program using Google Test, you need to compile Google Test into a library and li原创 2015-04-02 20:06:21 · 3158 阅读 · 0 评论 -
C++ memset 类对象 crash(core) 的问题
#include class Base1{private: char aChar; short aShort; int aInt; float aFloat;};class Derived1: public Base1{private: char charArray[3]; short shortArray[3]; int原创 2015-04-07 17:07:39 · 2046 阅读 · 1 评论 -
mutex 测试程序
这段程序使用了 C++ 11 中的线程库:// mutex::lock/unlock#include // std::cout#include // std::thread#include // std::mutexstd::mutex mtx; // mutex for critical sectionvo原创 2015-04-15 11:26:33 · 1039 阅读 · 0 评论 -
【优先队列】【链表】【priority_queue】【list】hdu 1434
方法一:优先队列方法二:liaobiao原创 2014-04-14 22:26:19 · 925 阅读 · 0 评论 -
STL map, multimap, set, multiset 函数介绍
1. 类模板和构造函数和前面介绍的ro原创 2014-04-18 00:50:11 · 973 阅读 · 0 评论 -
元素为自定义复合结构时 map,set 如何处理重复 key 及排序?
map, set 为 类模板,默认使用的 comparator 为类模板 less, 因此我们需要重载关系运算符 ".下面的例子是一个排行榜,排序规则如下:1. 按分数由高到低排;2. 分数一样的情况下,按时间由新到旧排;3. 时间也一样的情况下,按用户 ID 由小到大排。#include #include namespace test{ // sorted原创 2015-07-26 13:50:33 · 2166 阅读 · 0 评论 -
【双端队列】【栈】【队列】STL之deque、stack、queue、容器适配器
hexplicit deque (const allocator_type& alloc = allocator_type());explicit deque (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());template原创 2014-04-11 19:58:39 · 1262 阅读 · 0 评论