
c++中 try-catch 返回数组位导致[不是所有的控件路径都返回值]的问题
使用C++的经常返回一个列表中的东西,而带的参数是一个位置,常常在判断位置时给出异常让它不再向下走,并且有专门的异常处理。 基本的结构大概如下,但是以Catch之外,如果没的返回值,会得到一个[不是所有的控件路Read More →
使用C++的经常返回一个列表中的东西,而带的参数是一个位置,常常在判断位置时给出异常让它不再向下走,并且有专门的异常处理。 基本的结构大概如下,但是以Catch之外,如果没的返回值,会得到一个[不是所有的控件路Read More →
一、普通方式 在C++中,数组永远不会按值传递。它是传递第0个元素的指针同,数组的长度与参数声明无关。 下列三个方式是等价的: void fun(int*); void fun(int[]); void funRead More →
一、大概区别 就像指针的数组和数组的指针一样耐人寻味
1 2 3 4 5 6 |
//array首先向右结合,所以这个相当于 (int&)array[] array是个数组,其中的元素是引用 //应该叫:引用的数组 int &array[] //array首先和&结合,所以array是引用,引用的对象是数组 //应该叫作数组的引用 int (&array)[10] |
二、引用的数组 首先,可以明确的说明,引用的数组是不能当函数的参数的。再者要说明,这种方式Read More →
今天回忆了一下,又发现一个问题出来了,好像掉坑里去了,或者我这个的立意就是错误的吗? 问题: 对于迭代器来说,end应该是无效的,而我这介是有效的,并且产生一个问题那就是,我的数组里只能存n-1个值 因为最后一Read More →
今天用了一个指针数组的迭代器,感觉还不错,在这里重新写一份别的再排遣一下夜晚的寂寞,也当是个学习备份。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
// -------------------------------------------------------------------------- // 名称:ArrayIterator.cpp // 说明:一个C指针数组的迭代器学习 // 环境:vs2008 // 时间:2013.9.12. 00:20 // 附加:写的有些乱,有的地方不太好或者有错误,只是为了学习,如果您看到了,欢迎批评指正 // -------------------------------------------------------------------------- #include "stdafx.h" #include <iostream> using namespace std; // -------------------------------------------------------------------------- // 学生类 // -------------------------------------------------------------------------- class Student { public: Student(int id) :mID(id) { } int GetID() { return mID; } private: int mID; }; // -------------------------------------------------------------------------- // 学生类的指针数组模板类 // 在这里定义指针数组的一个迭代器 // -------------------------------------------------------------------------- typedef Student* StudentPtr; template<int NUM> class StudentPtrList { public: typedef StudentPtr* iterator; typedef const StudentPtr * const_iterator; iterator begin() { return &m_pStudent[0]; } iterator end() { //@MARK 这里碰到个问题 if (m_CurCount == 0) { return &m_pStudent[0]; } else { return &m_pStudent[m_CurCount - 1]; } } StudentPtrList() :m_CurCount(0) { memset(m_pStudent, 0, sizeof(m_pStudent)); } int GetCurCount() const {return m_CurCount;} int GetMaxCount() const {return NUM;} bool AddStudent(StudentPtr pStudent) { if (NULL == pStudent) { return false; } if (m_CurCount < NUM) { m_pStudent[m_CurCount++] = pStudent; return true; } return false; } private: StudentPtr m_pStudent[NUM]; int m_CurCount; }; // -------------------------------------------------------------------------- // 打印好学生ID的函数 // 这里好像有些问题,只能打印好学生的,应该可以再重构一下,有谁我指正 // -------------------------------------------------------------------------- const int GOOD_NUM = 10; typedef StudentPtrList<GOOD_NUM> GoodStudentList; void PrintStudentID(GoodStudentList& rList) { GoodStudentList::iterator iter = rList.begin(); StudentPtr pStudent = NULL; for (; iter != rList.end(); ++iter) { pStudent = *iter; if (NULL == pStudent) { continue; } cout << "ID: " << pStudent->GetID() << "\n"; } }; // -------------------------------------------------------------------------- // 一个存学生数据的池,从里面捞并初始化学生的数据 // -------------------------------------------------------------------------- const int MAX_STORE_NUM = 100; struct StudenStore { StudenStore() { memset(m_pStudent, 0, sizeof(m_pStudent)); } ~StudenStore() { for(int i = 0; i < MAX_STORE_NUM; ++i) { if (m_pStudent[i]) { delete m_pStudent[i]; } } } void Init() { for(int i = 0; i < MAX_STORE_NUM; ++i) { m_pStudent[i] = new Student(i); } } void GetGoodStudent(GoodStudentList& rList) const { int nCount = rList.GetMaxCount(); for (int i = 0; i < nCount; ++i) { rList.AddStudent(m_pStudent[i]); } } private: Student* m_pStudent[MAX_STORE_NUM]; }; // -------------------------------------------------------------------------- // 主函数测试 // 创建学生池,挑出好学生,打印好学生的ID // -------------------------------------------------------------------------- int main(int argc, char* argv[]) { StudenStore store; store.Init(); GoodStudentList goodList; store.GetGoodStudent(goodList); PrintStudentID(goodList); system("pause"); return 0; } |
关于Mark的解释 1.如果用 m_pSRead More →
〇、开场 1.今天编译新项目的工程,遇到一个Warning C4150的警告,观察之,查之,原来如此。 2.夜深的时候,一觉醒来,就不想再睡了,人的心事,总是越想越复杂,越想越烦躁,不如简简单单的看几行代码。 Read More →
关键字 UTC(世界标准时间),Calendar Time(日历时间),epoch(时间点),clock tick(时钟计时单元) 一、概念 1.Coordinated Universal TimRead More →
stlport据说是一位俄罗斯高人之作,目的就是为了让我们在多平台下可以使用高效的SGI STL,废话少说,先介绍下它的主页: http://sourceforge.net/projects/stlport/ Read More →
一、基本认识 2010年学习过的一点总结,今天没防看到,感觉还是不太完善,做事情,就一定要把它做好。不是追求完美,但一定要保持一颗保持进步的心。 二、重载和句法区别 C++规定后缀形式有一个int类型参数,当函Read More →
一、废话 之前由于工作需要,要封装一个Linux加密解密转换的动态库,这个之前只做过Windows下面的,Linux下面还真没有做过,之后做了整一个晚上才算做好,不过其中也学到了不少东西,包括Linux下的动态Read More →
Designed using Responsive Brix WordPress Theme. Powered by WordPress.