C++课程设计__学生(成绩)信息管理系统__包括doc报告文.doc_第1页
C++课程设计__学生(成绩)信息管理系统__包括doc报告文.doc_第2页
C++课程设计__学生(成绩)信息管理系统__包括doc报告文.doc_第3页
C++课程设计__学生(成绩)信息管理系统__包括doc报告文.doc_第4页
C++课程设计__学生(成绩)信息管理系统__包括doc报告文.doc_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

打印出系统时间优点:利用系统函数,还能修改系统时间,此文件必须是c+文件1. #include 2. #include 3. usingnamespacestd; 4. voidmain() 5. 6. system(time); 7. 运行效果如下:但是如果system里面的参数变为:time/t,就不允许修改系统时间了(缺点:仅仅显示小时和分钟)#include using namespace std; void main() system(time/t); system(pause); 改变DOS字体颜色#include using namespace std; void main() system(color ob); system(pause); 注:当system(“color ob”)中的ob变为相应的数字时,DOS屏幕字体变为相应的颜色改变DOS标题#include using namespace std; void main() system(title 王万腾); system(pause); 打印出系统日期#include using namespace std; void main() system(date); system(pause); #include using namespace std; void main() system(date/t); system(pause); for_each的简单应用#include #include #include #include /特别注意:for_each()还是在algorithm头文件中定义的来using namespace std;void print_i(int t)couttends;int main()int a=1,2,3,4,5,6,7,8,9,10,11,12,13;vector elements(a,a+13);for_each(elements.begin(),elements.end(),print_i);coutendl;system(pause); return 0; #include #include using namespace std;int main()string s(WangWanteng);string:size_type len2=s.size();couts.capacity():s.capacity()endss.size():len2endl;system(pause); return 0; 总结:若s为一字符串,则s.size()=s.length(),s.capacity()总比s.size()或s.length()大4个字符string的substr()#include #include using namespace std;int main()string s(WangWanteng);string s1(MaYu);s1=s.substr(4,3);couts1endl;system(pause); return 0; 上例:s.substr(4,3)是指从s串第(4+1)个字符开始往后数3个字符 返回关于swap()的传说#include #include using namespace std;int main()string s(Wangwanteng);string s1(王万腾);swap(s,s1);int a=1;int b=2;swap(a,b);coutaendl;couts 3; ;If we create an instance of class biggerThanThree, every time we reference this object using the function call syntax, the function call operator member function is invoked. To generalize this class, we add a constructor and a constant data member, which is set by the constructor:class biggerThan public:const int testValue;biggerThan(int x) : testValue(x) bool operator()(int val) const return val testValue; ;The result is a general biggerthanX function, where the value of X is determined when we create an instance of the class. We can do so, for example, as an argument to one of the generic functions that require a predicate. In this manner the following code finds the first value in a list that is larger than 12:std:list:iterator firstBig = std:find_if(aList.begin(), aList.end(), biggerThan(12);3.2.2 UseThere are a number of situations where it is convenient to substitute function objects in place of functions: to use an existing function object provided by the C+ Standard Library instead of a new function; to improve execution by using inline function calls; and to allow a function object to access or set state information that is held by an object. Lets deal with each of these in the next three sections. To Employ Existing C+ Standard Library Function ObjectsTable5 illustrates the function objects provided by the C+ Standard Library.Table5: Function objects provided by the C+ Standard LibraryFunction object Implemented operations Arithmetic functionsplusaddition x + y minussubtraction x - ymultipliesmultiplication x * ydividesdivision x / ymodulusremainder x % ynegatenegation - xComparison functionsequal_toequality test x = y not_equal_toinequality test x != ygreatergreater-than comparison x ylessless-than comparison x = yless_equalless than or equal comparison x = yLogical functionslogical_andlogical conjunction x & ylogical_orlogical disjunction x | ylogical_notlogical negation ! xLets look at a couple of examples that show how these might be used. The first example uses std:plus() to compute the by-element addition of two lists of integer values, placing the result back into the first list. This can be performed by the following code:std:transform(listOne.begin(), listOne.end(), listTwo.begin(), listOne.begin(), std:plus() );The second example negates every element in a vector of boolean values:std:transform(aVec.begin(), aVec.end(), aVec.begin(), std:logical_not() );The base class templates used by the C+ Standard Library to define the functions in Table5 are also available for creating new unary and binary function objects. The class templates unary_function and binary_function are defined in the header .The base classes are defined as follows:namespace std template struct unary_functiontypedef Arg argument_type;typedef Result result_type;template struct binary_functiontypedef Arg1 first_argument_type;typedef Arg2 second_argument_type;typedef Result result_type;An example of the use of these templates is found in Section6.3. There we want to take a binary function of type Widget and an argument of type int, and compare the widget identification number against the integer value. A function to do this is written in the following manner:struct WidgetTester : std:binary_function bool operator() (const Widget& wid, int testid) const return wid.id = testid; ;The importance of inheritance from one of the two base class templates becomes apparent when elementary function objects are composed to form complex expressions using negators and binders, which are explained in Section.2.2 To Improve ExecutionA second reason to consider using function objects instead of functions is faster code. The difference between a function and an object of a class that defines the member operator()() is that functions are converted to pointers when passed as arguments to other functions, thus incurring the overhead of a function call even if they are declared inline. In many cases an invocation of a function object, as in the examples on std:transform() in Section, can be expanded in-line, eliminating the overhead of a function call. To Access or Set State InformationThe third major reason to use a function object in place of a function is when each invocation of the function must remember some state set by earlier invocations. An example of this occurs in the creation of a generator, to be used with the generic algorithm std:generate(). A generator is simply a function object that takes no arguments and returns a new (and possibly different) value each time it is invoked. The most commonly used form of generator is a random number generator, but there are other uses for the concept. A sequence generator simply returns the values of an increasing sequence of natural numbers (1, 2, 3, 4 and so on). We can call this object iotaGen after the similar operation in the programming language APL, and define it as follows:class iotaGen public:iotaGen (int start = 0) : current(start) int operator() () return current+; private:int current;An iota object maintains a current value, which can be set by the constructor, or defaults to zero. Each time the function-call operator is invoked, the current value is returned, and also incremented. Using this object, the following call on the C+ Standard Library function std:generate() initializes a vector of 20 elements with the values 1 through 20:std:vector aVec(20);std:generate(aVec.begin(), aVec.end(), iotaGen(1);A more complex example of using a function object occurs in the radix sorting example program, which is given as an example of using the list datatype in Section6.3. In this program references are initialized in the function object, so that during the sequence of invocations the function object can access and modify local values in the calling program.Introduction to IteratorsThe concept of iterators is fundamental to using the container classes and the associated algorithms provided by the C+ Standard Library. An iterator is a pointer-like object used to cycle through all the elements stored in a container. Because different algorithms need to traverse containers in a variety of fashions, there are different forms of iterators. Each container class in the C+ Standard Library can generate an iterator with functionality appropriate to the storage technique used in implementing the container. It is the category of iterators required as arguments that chiefly distinguishes which algorithms in the C+ Standard Library can be used with which container classes. Just as pointers can be used in a variety of ways in traditional programming, iterators can be used for a number of different purposes. An iterator can be used to denote a specific value, just as a pointer can be used to reference a specific memory location. A pair of iterators can be used to define a range or sequence of values held in a container, just as two pointers can be used to describe a contiguous region of memory. With iterators, however, the values being described may be only logically rather than physically in sequence because they are derived from the same container, and the second follows the first in the order in which the elements are maintained by the container.Conventional pointers can sometimes be null, meaning they point at nothing. Iterators, as well, can fail to denote any specific value. Just as it is a logical error to dereference a null pointer, it is an error to dereference an iterator that is not denoting a value. When two pointers that describe a region in memory are used in a C+ program, it is conventional that the ending pointer not be considered part of the region. For example, an array named x of length 10 is sometimes described as extending from x to x+10, even though the element at x+10 is not part of the array. Instead, the pointer value x+10 is the past-the-end value after the end of the range being described. Iterators are used similarly to describe a range. The second value is not considered part of the range being denoted. Instead, the second value is a past-the-end element describing the next value in sequence after the final value of the range. Sometimes, as with pointers to memory, this will be an actual value in the container. Other times it may be a special value, specifically constructed for the purpose. In either case, it is not proper to dereference an iterator that is being used to specify the end of a range. Just as with conventional pointers, the fundamental operation used to modify an iterator is the increment operator, operator+(). When the increment operator is applied to an iterator that denotes the final value in a sequence, the iterator is changed to the past-the-end value. An iterator j is said to be reachable from an iterator i if, after a finite sequence of applications of the expression +i, the iterator i becomes equal to j.Ranges can be used to describe the entire contents of a container by constructing an iterator to the initial element and a special ending iterator. Ranges can also be used to describe sub-sequences within a single container by employing two iterators to specific values. NOTE - Whenever two iterators are used to describe a range, it is assumed that the second iterator is reachable from the first, but this is not verified. Errors can occur if this expectation is not satisfied. Input IteratorsInput iterators are the simplest form of iterator. To understand their capabilities, consider an example algorithm: template InputIterator find (InputIterator first, InputIterator last, const T& value) while (first != last & *first != value) +first; return first;This algorithm, which is similar to the std:find() generic algorithm (Section13.3.1), performs a simple linear search, looking for a specific value being held within a container. The contents of the container are described using two iterators, first and last. While first is not equal to last, the element denoted by first is compared to the test value. If this element is equal to the test value, the iterator, which now denotes the located element, is returned. If it is not equal, the first iterator is incremented and the loop cycles once more. If the entire region of memory is examined without finding the desired value, then the algorithm returns the end-of-range iterator.This algorithm illustrates three features of an input iterator: An input iterator can be compared for equality to another iterator. They are equal when they point to the same position, and not equal otherwise. An input iterator can be dereferenced, using operator*() to obtain the value being denoted by the iterator. An input iterator can be incremented, so that it refers to the next element in sequence, using operator+().Notice that these features can all be provided with new meanings in a C+ program, since the behavior of the given functions can all be modified by overloading the appropriate operators. Because of this overloading, iterators are possible. Kinds of Input IteratorsThere are three main kinds of input iterators: ordinary pointers, container iterators, and input streams iterators.Ordinary pointers. Ordinary pointers can be used as input iterators. In fact, since we can subscript and add to ordinary pointers, they are random access values, and thus can be used either as input or output iterators. The end-of-range pointer describes the end of a contiguous region of memory, and the dereference and increment operators have their conventional meanings. For example, the following code searches for the value 7 in an array of integers:int data100; .int* where = std:find(data, data+100, 7);Note that constant pointers, which do not permit the underlying array to be modified, can be created by simply placing the keyword const in a declaration:const int* first = data;const int* last = data + 100; / cant modify location returned by the followingconst int* where = std:find(first, last, 7);Because ordinary pointers have the same functionality as random access iterators, most of the generic algorithms in the C+ Standard Library can be used with conventional C+ arrays, as well as with the containers provided by the C+ Standard Library.Container iterators. All of the iterators constructed for the various containers provided by the C+ Standard Library are at least as general as input iterators. The iterator for the first element in a collection is always constructed by the member function begin(), while the iterator that denotes the past-the-end location is generated by the member function end(). For example, the following iterator searches for the value 7 in a list of integers:std:list:iterator where = std:find(aList.begin(), aList.end(), 7);Each container that supports iterators provides a type with the name iterator within the class declaration. Using this type, iterators can uniformly be declared in the fashion shown. If the container being accessed is constant, or if the description const_iterator is used, then the iterator is a constant iterator.Input stream iterators. The C+ Standard Library provides a mechanism to operate on an input stream using an input iterator. This ability is provided by the class istream_iterator, described in more detail in Section.2.2 Output IteratorsAn output iterator has the opposite function of an input iterator. Output iterators can be used to assign values in a sequence, but cannot be used to access values. For example, we can use an output iterator in a generic algorithm that copies values from one sequence into another:template OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result) while (first != last) *result+ = *first+; return result;A number of the generic algorithms manipulate two parallel sequences. Frequently the second sequence is described using only a beginning iterator, rather than an iterator pair. It is assumed, but not checked, that the second sequence has at least as many elements as the first.In the algorithm shown here, two ranges are being manipulated: the range of source values specified by a pair of input iterators, and the destination range. The latter, however, is specified by only a single argument. It is assumed that the destination is large enough to include all values, and errors will ensue if this is not the case.As illustrated by this algorithm, an output iterator can modify the element to which it points by being used as the target for an assignment. Output iterators can use the dereference operator only in this fashion; they cannot be used to return or to access the elements they denote. As we noted earlier, ordinary pointers, like all iterators co

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论