欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    CC++:从基础语法到优化策略 (4).pdf

    • 资源ID:67731787       资源大小:8.78MB        全文页数:28页
    • 资源格式: PDF        下载积分:10金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    CC++:从基础语法到优化策略 (4).pdf

    C/C+Program DesignCS205Week 4Content Arrays Array-style strings string-class strings Structures Unions EnumerationsArrayThe slides are based on the book Data TypesFundamental types Integer Typeboolshort,int,long Char Type:char Floating-point Type:float,doubleCompound typesArrayArray-style string String classStructureArrays An array is a data form that can hold several values,all of one typeone type To define:The type of value to be stored in each elementThe name of the arrayThe number of elements in the array must be an integer constant,such as 10 or a const value,MICROS,or a constant expressionSquare brackets Why?Arrays Some statements for an arrayDeclaring an arrayAssigning values to array elementsInitializing an array Run program example/arrayone.cpp-small arrays of integersNote that if you use the sizeof operator with an array name,you get the number of bytes in the whole arrayFirst element index is 0Error:if subscript is equal or greater than the number of elements Initialization Rules for Arrays Several rules about initializing arrays Able to Use the initialization form only when defining the array Use subscripts and assign values to the elements of an array individually Partially initialize an array,the compiler sets the remaining elements to zeroCannot Use initialization later Assign one array wholesale to anotherC+11 Array Initialization Rules in C+11Can drop the=signCannot convert from a floating-point type to an integer type(narrowing)Cannot assign int type to char type(Outside the range of a char)StringThe slides are based on the book Strings A string is a series of characters stored in consecutivebytes of memoryC-style(array)stringstring class library Store a stringin an array of char(C-style)The last character of every string is the null characterThis null character is written 0The character is with ASCII code 0It serves to mark the strings endStrings Using a double quoted stringCalled a string constant or string literalInclude the terminating null character implicitlyMake sure the array is large enough to hold all the charactersNote that a string constant(with double quotes”)is not interchangeable with a character constant(with single quotes )Example cstrinit.cppConcatenating String Literals C+enables to concatenate string literalsAny two string constants separated only by whitespace Run program exampleUsing Strings in an Array/strings.cpp-storing strings in an array Shortening a string with 0 Beware of memory overflow(Problem)Adventures in String Input Run program example/instr1.cpp-reading more than one stringThe cin technique is to use whitespacespaces,tabs,and newlines(0)to delineate a stringThe input string might turn out to be longer than the destination array(buffer)A white space causes a problemReading String Input a Line at a Time(solved)To solve the problem:Line-oriented input with getline()See program example/instr2.cpp-reading more than one word with getline()Two argumentsOther Forms of String Literals Beside char,we have more following typeswchar_tchar16_tchar32_tchar8_t /c+2a char8_t name=u8SUSTech”;/UINT8 example wstr.cppstring-class stringsThe slides are based on the book string class The ISO/ANSI C+98 Standard expanded the C+library Include the string header file:#include Run program example strtype1.cppInitialize a string object,in a similar way as a C-style stringUse cin to store keyboard input in a string objectUse cout to display a string objectUse array notation to access individual characters stored in a string object DifferencesTreat object as a simple variable,not as an arrayAllow the program to handle the sizing automaticallyC+11 String Initialization C+11 enables 4 kinds of initialization Array-style String class Assign one string object to another Array assignment Use the+and+=operatorsMore string Class Operations Three functions for array-style stringstrcpy():copy a string to a character array =strcat():append a string to a character array +=strlen():calculate the length of a character array *.size()See three operations in program example strtype3.cpp Conclusionsstring objects tends to be simplersimplerthan using the C string functionsstring objects tends to be more safesafethan that of the CMore on string Class I/O See length of string in program example strtype4.cpp The difference and problems of array-style stringstrlen()reaches a null characterstring object is automatically set to zero sizeArray-style string has fixed size of inputcin.getline(charr,20);/Array-style string getline(cin,str);/string classStructures,Unions and EnumerationsThe slides are based on the book Introducing Structures Why structures?Almost all previous types are those you can directly useA structure is a more versatile data form than an arrayA structure is a user-definable type The keyword struct make a new typeUsing a Structure in a Program How to create a structure?Where to place the structure declaration?Inside or outside of mainCan a structure use a string class member?YesAssignment:use a comma-separated list of values enclosed in a pair of bracesIn C+11,the=sign is optionalEmpty braces result in the individual members being set to 0 See assignment and member access in program example stracture.cppOther Structure Properties What actions you can do for structures?Pass structures as arguments(multiple)to a function Have a function use a structure as a return value(multiple)Combine the definition of a structure form with the creation of structure variables Have member functions in addition to member variables Run program example assgn_st.cpp Member-wise assignment:use the assignment operator(=)to assign one structure to another of the same typeMore Structure Properties:Array Arrays of Structures Create arrays whose elements are structures An examplegifts itself is an array,not a structuregifts0 is a structureUnions A union is a data format Can hold different data types but only one type at a time Can use two or more formats but neversimultaneously Save memory union Keyword make a new typeprogram example union.cppEnumerations The C+enum facility provides an alternative to const for creating symbolic constants(#define)enum spectrum red,orange,yellow,green,blue,violet;It makes spectrum the name of a new typeIt establishes the members as symbolic constants for the integer values 05 By default,enumerators are assigned integer values starting with 0 for the first enumerator,1 for the second enumerator,and so forth The assigned values must be integers enum Keyword make a new typeEnumerations What operations can you do for enumerations?Assign it using the member You can set enumerator values explicitly Assign other variables using it Typecast values within the range Beware of the value ranges for enumerations

    注意事项

    本文(CC++:从基础语法到优化策略 (4).pdf)为本站会员(奉***)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开