Chapter 4 Composite Type

0.Before Everything

Before Everything

这是一份四年没有学过C++的菜鸡由于工作岗位需求重拾C++的复习记录,因为工作需要在练习英文,加上Ubuntu下输入法与IDE的冲突,所以写纯英文的笔记,顺便练习一下自己英文的水平,如果有英语的表达不当或者对C++的理解错误,还请看到的大佬提醒我,共勉,谢谢。


4.1 Array

1.Array declaration

//typeName arrayName[arraySize]
short months[12];

Key points:

  1. The type of data stored in the array.
  2. The name of the array.
  3. The size of the array. It must be a constant value.

2.Initialization

Array initialized by a serial of elements wrapped by braces and split by comma. The length of element list must
be shorter than the size defined in declaration, and the default element will be initialized to a default value 0.
If there is not a explicit declaration of size, compiler will count the number of elements in the list(but it’s not safe)
The array initialization is only available when creating an array.

Examples:

    int cards[4] = {1,2,3,4};int arr[4] = {1,2,3}; // the last element will be 0.int hand[3];int noSize[] = {1,2,3,4,5,6}; //compiler calculate size as 6hand = {1,2,3}; //not allowed, use initialization after declarationhand = cards; //not allowed, can not assign an array to another one

C++11 array initialization:

    int noEqual[3] {2,3,4};int noElem[4] {}; // all element set to 0long narrow[3] {25, 35, 3.0}; // not allowed, narrowing assignment

4.2 String

1.String in C++

String => a serial of characters stored in mem sequentially.

There are two methods for C++ to proceed string:

1.C-style string

2.C++ string (in string lib)

2.C-style string

C-style string is a char array ended with an empty character (\0)

    char notString[] = {'a', 'b', 'c'};char aString[] = {'a', 'b', 'c', '\0'};

The role of \0 is to mark the end of string. There are many functions that proceed string in C++.
They proceed string char by char until meet a \0. If there was no \0
at the end of a char array, those functions will proceed the succeeding data in memory
which is not belong to the string, until next \0.

There is another form of string declaration:

    char bird[11] = "Mr. Cheeps"; //the \0 is understoodchar fish[] = "fish"; // compiler will count 5 for the array

A string wrapped by quotations will contain a \0 automatically.

3.The string class

string is a class provided by header file string, in namespace std.
It requires “using” order or “std::string”.

The string class supports any standard input and output including cin and cout.
And it allows using array notation to access any character in it, and using c-style string
to init it.

int main(){using namespace std;string myStr = "abc";cout << myStr << endl;cout << myStr[2] <<endl; // print cstring cpp11 {"C++ 11 style string"};string strJoin = myStr + cpp11; // joined stringmyStr += cpp11; // append string
}

4.Some operations of string

  1. strcat and strcpy(C function): Using strcpy(dest, src) to copy src to dest. Or using strcat(dest, src) to append src after dest and return a pointer towards dest.
  2. string input: Using cin >> str to assign input string to str. Or using cin.getline(string dest,int length) or getline(cin, dest) to read input to dest string.
  3. string output: Using cout << str to read str to output.
  4. raw string: Wrapping string by R"( as start and )" as end, we can use quotations in string instead of using escape character

4.3 Introduction of struct

1.Declaration of struct

struct inflatalbe{char name[20];float volume;double price;
};

2.Using struct in program

struct inflatable{char name[20];float volume;double price;
};int main(){using namespace std;inflatable guest = {"Gloria",1.88,29.99};cout << "Name of guest is: " << guest.name << endl;cout << "Total price: " << guest.price << endl;
}
//===============================================
struct {char name[20];float volume;double price;
}inflatable;int main(){using namespace std;auto guests = inflatable;guests = {"Gloria",1.88,29.99};cout << "Name of guest is: " << guest.name << endl;cout << "Total price: " << guest.price << endl;
}

4.4 Union

1.What is Union

Union is a data format like struct but can store only one type.

2.Union declaration

union Token{char cval;int ival;double dval;
};

3.Using union

Union’s values are mutually exclusive. The previous value will lose when assign a value to another member.

{Token token;token.cval = '1';token.ival = 1; // cval lost
}

4.5 Enum

The enum is a tool to generate symbol constant value.

The value of enumerators start from 0.

If an enumerator has not been initialized, it will have a default
value = previous value + 1

    //red = 0, orange = 1 ...enum color{red, orange, yellow, green};//Set value of enumerators explicitlyenum bits{one = 1, two = 2, four = 4, eight = 8};

4.6 Pointer and free memory

1.Pointer basic introduction.

Core operators:

& => get the address of a variable

* => use this to define a pointer

    int var = 6; //declare a variableint* p; // declare a pointer to an intp = &var; //assign address of var to p, the value of p is a hex addresscout << *p << endl; // in fact cout << varcout << &var << endl; // in fact cout << p

The * must write before the pointer.

Statement int * p, x declare a pointer “p” and an int variable “x”.

2.Assign a address to a pointer

    int * pt;pt = 0xB8000000; // mismatchpt = (int *) 0xB8000000; //type match

3.Using “new” to allocate memory

    int *pt = new int; // allocate memory for an int*pt = 1001; // store a value

4.Using “delete” to free memory

This statement will free memory a pointer refers to, but not the pointer.

delete only available on a pointer. And it’s safe on a null pointer.

    int * pt = new int;...delete pt;

5.Using “new” to create a dynamic array.

This statement creates an int array which can store 10 elements. And the operator “new” will
return the address of first element.

When we need free the memory, use delete [] arr.

    int * arr = new int[10];delete [] arr;

6.Conventions about “new” and “delete”

  • Don’t use “delete” to free memory which was not created by “new”
  • Don’t use “delete” twice on same memory
  • Use “delete []” if the memory of an array which was created by “new []”
  • Use “delete” on a null pointer is safe

4.7 “vector” and template array

1.vector

vector is an encapsulation class of dynamic array. It is defined in namespace std.

It can be resized,or append and insert data anytime.

    vector<int> vi; //create a zero-size array of intvector<doble> vd(10); // create a array of double with 10 capability

2.Template array (C++ 11)

    array<int, 5> darr;//create an array which can store 5 ints

关于我年久失修的C++的康复记录4相关推荐

  1. 关于我年久失修的C++的康复记录3

    Chapter 3 0.Before Everything Before Everything 这是一份四年没有学过C++的菜鸡由于工作岗位需求重拾C++的复习记录,因为工作需要在练习英文,加上Ubu ...

  2. 关于我年久失修的C++的康复记录9

    Chapter 9 Memory Model and Namespace 0.Before Everything 这是一份四年没有学过C++的菜鸡由于公司业务需求重拾C++的复习记录,因为工作需要在练 ...

  3. 【Mind】角膜上皮脱落康复记录

    健康无价,请珍惜自己的身体.亲身经历全记录. 08.09 DAY 1. 从18:00睡到21:30,发现外面大雨,担心女朋友回淋雨,准备去接女朋友回家.同时,此时左眼有些不舒服,但症状并不严重,只是轻 ...

  4. springboot老年康复中心信息管理系统的设计与实现毕业设计-附源码250859

    摘 要 随着互联网趋势的到来,各行各业都在考虑利用互联网将自己推广出去,最好方式就是建立自己的互联网系统,并对其进行维护和管理.在现实运用中,应用软件的工作规则和开发步骤,采用Springboot框架 ...

  5. 每日新闻:百度首个无人驾驶运营项目落户武汉;微软叫停Linux专利战;网易携手芬兰电信Elisa;瑞星华为联合发布云安全解决方案...

    关注中国软件网 最新鲜的企业级干货聚集地 今日热点 Gartner发布2018年第二季度全球服务器市场报告 日前,国际权威研究机构Gartner发布了2018年第二季度全球服务器市场报告,报告显示,在 ...

  6. 脑机接口:从基础科学到神经康复

    本文转自公众号:脑机接口社区 大家好 ,我是米格尔·尼科莱利斯,美国杜克大学神经生物学.神经学和生物医学工程教授.今天我将为大家介绍脑机接口和这一技术从基础科学到应用于神经康复的研究历程. 首先,我要 ...

  7. Nature子刊:Neuropixels 探针单神经元分辨率的大规模神经记录

    近年来,多电极阵列技术的发展使得在动物模型中以细胞分辨率监测大规模神经元集群成为可能.不过在人类中,目前的方法限制每个穿透电极只能记录几个神经元的信号,或者在局部场电位(LFP)记录中结合数千个神经元 ...

  8. 奥运会上刷新亚洲记录的211高校副教授苏炳添论文被扒出,网友:膜拜大神!...

    8月1日,在东京奥运会上,苏炳添跑出中国体育历史的新篇章! >>>> 中国速度惊艳世界! 在男子100米半决赛中,他以个人最好成绩9秒83,创造新的亚洲纪录.决赛中,他以9秒9 ...

  9. 【稀疏向量技术是什么?】差分隐私系统学习记录(六)

    The Algorithmic Foundations of Differential Privacy (六) 写在前面的话 Remarks on composition Weak Quantific ...

最新文章

  1. 图论分析方法gretna_基于磁共振的多模态分析对血管性认知障碍患者脑网络的研究...
  2. 计算机使用DHCP动态分配参数,某单位采用DHCP进行IP地址自动分配,用户收到()消息后方可使用其中分配的IP - 信管网...
  3. 如何查询一个表中除某几个字段外其他所有的字段_一个小故事告诉你:如何写好数据分析报告?...
  4. TCP拥塞控制算法 — CUBIC的补丁(三)
  5. 自定义会话状态存储提供程序
  6. python基于web可视化_python可视化(转载)
  7. [Erlang危机](4.4)命名管道
  8. Asset Store 下载的package存在什么地方?
  9. 1.2、获取、创建 ApplicationContextInitializer
  10. [Andriod设计模式之旅]——Builder模式
  11. 365RSS.cn = Web3.0?
  12. OC_UISlider
  13. jsp数据库连接大全和数据库操作封装到Javabean
  14. 万能html5视频播放器安卓,XPlayer万能视频播放器
  15. 欧姆龙485通讯示例程序_PLC程序结构设计和技巧
  16. python f 格式字符串输出
  17. PS学习笔记(88天和我一起学会PS)(5/88)
  18. 成功解决tensorflow.python.framework.errors_impl.InvalidArgumentError报错问题
  19. latex用法与论文投稿
  20. 网站命名规范大全:CSS规范便于交流

热门文章

  1. 基于linux的mp3播放实现代码
  2. 诊所预约管理系统-电子病例功能
  3. 霍金已经不是我们认识的那个霍金了!
  4. 插画零基础教程,没有基础也不要怕
  5. Linux系统之dd命令详解
  6. python安装pywin32_在python虚拟环境中安装pywin32
  7. java comparable方法_JAVA 泛型 - ComparableT
  8. C语言系列之炫酷新年烟花秀(带新年祝福)
  9. html2canvas截图时 不能使用跨域图片的解决方案
  10. 中小企业的繁殖技巧:项目发展模式(转)